Skip to content

Commit

Permalink
Merge pull request #9 from roymckenzie/bugfix/runtime-error
Browse files Browse the repository at this point in the history
Update for Google Chrome Extensions manifest v3
  • Loading branch information
roymckenzie authored Oct 31, 2023
2 parents 319d36d + 695174f commit b49a7e5
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 164 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
.DS_Store
node_modules
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ Install [the extension](https://chrome.google.com/webstore/detail/detect-zero-wi

## License

Detect Zero-Width Characters Chrome Extension is an Open Source project covered by the [GNU General Public License version 2](LICENSE).
Detect Zero-Width Characters Chrome Extension is an Open Source project covered by the [GNU General Public License version 3](LICENSE).
9 changes: 9 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"module": "ESNext",
"target": "ES2020",
"checkJs": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
}
}
36 changes: 9 additions & 27 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
{
"name": "Detect Zero-Width Characters",
"version": "0.0.3",
"manifest_version": 2,
"version": "0.0.4",
"manifest_version": 3,
"description": "Detects zero-width characters, highlights the characters and containing DOM element, and allows sanitization and copying of text.",
"homepage_url": "https://github.com/roymckenzie/detect-zero-width-characters-chrome-extension",
"permissions": [
"contextMenus",
"clipboardWrite"
],
"permissions": ["contextMenus", "clipboardWrite"],
"background": {
"scripts": [
"src/constants.js",
"src/utils.js",
"src/background/background.js"
]
"service_worker": "src/background/service-worker.js"
},
"icons": {
"16": "src/icon/16x16.png",
Expand All @@ -22,23 +15,12 @@
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"css": [
"src/inject/inject.css"
]
"matches": ["http://*/*", "https://*/*"],
"css": ["src/inject/inject.css"]
},
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": [
"src/constants.js",
"src/inject/inject.js"
]
"matches": ["http://*/*", "https://*/*"],
"js": ["src/helpers/constants.js", "src/helpers/utils.js", "src/inject/inject.js"]
}
]
}
}
76 changes: 76 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"devDependencies": {
"@types/chrome": "^0.0.248"
}
}
41 changes: 0 additions & 41 deletions src/background/background.js

This file was deleted.

36 changes: 36 additions & 0 deletions src/background/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Builds a context menu in Chrome.
*
* @param {SanitizeAndCopyContextMenuMessage} message
*
* @since 0.0.2
*/
const handleContextMenu = (message) => {
if (message.type !== "sanitizeAndCopyContextMenu") return;

chrome.contextMenus.removeAll();

if (!message.shouldSanitizeSelection) {
return;
}

chrome.contextMenus.create({
id: "sanitizeAndCopyContextMenuItem",
title: "Sanitize and copy",
type: "normal",
contexts: ["selection"],
});

chrome.contextMenus.onClicked.addListener((info, tab) => {
if (!tab) return;
if (!tab.id) return;
if (info.menuItemId !== "sanitizeAndCopyContextMenuItem") return;

chrome.tabs.sendMessage(tab.id, {
type: "sanitizeAndCopyContextMenuItemAction",
text: message.textSelection,
});
});
};

chrome.runtime.onMessage.addListener(handleContextMenu);
2 changes: 1 addition & 1 deletion src/constants.js → src/helpers/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
*
* @since 0.0.1
*/
const zeroWidthCharacterCodes = [ 8203, 8204, 8205, 8288 ];
const zeroWidthCharacterCodes = [8203, 8204, 8205, 8288];
34 changes: 34 additions & 0 deletions src/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Removes zero-width characters from `text`.
*
* @param {string} text - String to sanitize.
* @returns Sanitized string.
*/
const sanitize = (text) => {
return [...text]
.filter((char) => {
const unicodeCode = char.codePointAt(0);
return !zeroWidthCharacterCodes.includes(unicodeCode);
})
.join("");
};

// https://stackoverflow.com/a/18455088/6591929
/**
* Copies `text` to clipboard.
*
* @param {string} text - String to copy to clipboard.
*/
const copyTextToClipboard = (text) => {
const textArea = document.createElement("textarea");
textArea.setAttribute("name", "copyTextArea");
textArea.textContent = text;

const body = document.body;
body.appendChild(textArea);

textArea.select();
document.execCommand("copy");

body.removeChild(textArea);
};
Loading

0 comments on commit b49a7e5

Please sign in to comment.