Skip to content

Commit b49a7e5

Browse files
authored
Merge pull request #9 from roymckenzie/bugfix/runtime-error
Update for Google Chrome Extensions manifest v3
2 parents 319d36d + 695174f commit b49a7e5

13 files changed

+279
-164
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.DS_Store
1+
.DS_Store
2+
node_modules

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ Install [the extension](https://chrome.google.com/webstore/detail/detect-zero-wi
1111

1212
## License
1313

14-
Detect Zero-Width Characters Chrome Extension is an Open Source project covered by the [GNU General Public License version 2](LICENSE).
14+
Detect Zero-Width Characters Chrome Extension is an Open Source project covered by the [GNU General Public License version 3](LICENSE).

jsconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"module": "ESNext",
4+
"target": "ES2020",
5+
"checkJs": true,
6+
"strictNullChecks": true,
7+
"strictFunctionTypes": true,
8+
}
9+
}

manifest.json

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,12 @@
11
{
22
"name": "Detect Zero-Width Characters",
3-
"version": "0.0.3",
4-
"manifest_version": 2,
3+
"version": "0.0.4",
4+
"manifest_version": 3,
55
"description": "Detects zero-width characters, highlights the characters and containing DOM element, and allows sanitization and copying of text.",
66
"homepage_url": "https://github.com/roymckenzie/detect-zero-width-characters-chrome-extension",
7-
"permissions": [
8-
"contextMenus",
9-
"clipboardWrite"
10-
],
7+
"permissions": ["contextMenus", "clipboardWrite"],
118
"background": {
12-
"scripts": [
13-
"src/constants.js",
14-
"src/utils.js",
15-
"src/background/background.js"
16-
]
9+
"service_worker": "src/background/service-worker.js"
1710
},
1811
"icons": {
1912
"16": "src/icon/16x16.png",
@@ -22,23 +15,12 @@
2215
},
2316
"content_scripts": [
2417
{
25-
"matches": [
26-
"http://*/*",
27-
"https://*/*"
28-
],
29-
"css": [
30-
"src/inject/inject.css"
31-
]
18+
"matches": ["http://*/*", "https://*/*"],
19+
"css": ["src/inject/inject.css"]
3220
},
3321
{
34-
"matches": [
35-
"http://*/*",
36-
"https://*/*"
37-
],
38-
"js": [
39-
"src/constants.js",
40-
"src/inject/inject.js"
41-
]
22+
"matches": ["http://*/*", "https://*/*"],
23+
"js": ["src/helpers/constants.js", "src/helpers/utils.js", "src/inject/inject.js"]
4224
}
4325
]
44-
}
26+
}

package-lock.json

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"devDependencies": {
3+
"@types/chrome": "^0.0.248"
4+
}
5+
}

src/background/background.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/background/service-worker.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Builds a context menu in Chrome.
3+
*
4+
* @param {SanitizeAndCopyContextMenuMessage} message
5+
*
6+
* @since 0.0.2
7+
*/
8+
const handleContextMenu = (message) => {
9+
if (message.type !== "sanitizeAndCopyContextMenu") return;
10+
11+
chrome.contextMenus.removeAll();
12+
13+
if (!message.shouldSanitizeSelection) {
14+
return;
15+
}
16+
17+
chrome.contextMenus.create({
18+
id: "sanitizeAndCopyContextMenuItem",
19+
title: "Sanitize and copy",
20+
type: "normal",
21+
contexts: ["selection"],
22+
});
23+
24+
chrome.contextMenus.onClicked.addListener((info, tab) => {
25+
if (!tab) return;
26+
if (!tab.id) return;
27+
if (info.menuItemId !== "sanitizeAndCopyContextMenuItem") return;
28+
29+
chrome.tabs.sendMessage(tab.id, {
30+
type: "sanitizeAndCopyContextMenuItemAction",
31+
text: message.textSelection,
32+
});
33+
});
34+
};
35+
36+
chrome.runtime.onMessage.addListener(handleContextMenu);

src/constants.js renamed to src/helpers/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
*
44
* @since 0.0.1
55
*/
6-
const zeroWidthCharacterCodes = [ 8203, 8204, 8205, 8288 ];
6+
const zeroWidthCharacterCodes = [8203, 8204, 8205, 8288];

src/helpers/utils.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Removes zero-width characters from `text`.
3+
*
4+
* @param {string} text - String to sanitize.
5+
* @returns Sanitized string.
6+
*/
7+
const sanitize = (text) => {
8+
return [...text]
9+
.filter((char) => {
10+
const unicodeCode = char.codePointAt(0);
11+
return !zeroWidthCharacterCodes.includes(unicodeCode);
12+
})
13+
.join("");
14+
};
15+
16+
// https://stackoverflow.com/a/18455088/6591929
17+
/**
18+
* Copies `text` to clipboard.
19+
*
20+
* @param {string} text - String to copy to clipboard.
21+
*/
22+
const copyTextToClipboard = (text) => {
23+
const textArea = document.createElement("textarea");
24+
textArea.setAttribute("name", "copyTextArea");
25+
textArea.textContent = text;
26+
27+
const body = document.body;
28+
body.appendChild(textArea);
29+
30+
textArea.select();
31+
document.execCommand("copy");
32+
33+
body.removeChild(textArea);
34+
};

0 commit comments

Comments
 (0)