From 5ab7322dd040f7819a562be29434b870bbc9be1e Mon Sep 17 00:00:00 2001 From: Teal Dulcet Date: Wed, 4 Oct 2023 06:08:29 -0700 Subject: [PATCH] Removed use of tabs.executeScript and updated manifest files. --- .eslintrc | 4 +- scripts/manifests/chromemanifest.json | 7 +-- scripts/manifests/dev.json | 8 +-- scripts/manifests/firefox.json | 8 +-- scripts/manifests/thunderbirdmanifest.json | 8 +-- src/background/background.html | 2 +- src/background/modules/AutocorrectHandler.js | 8 +-- src/background/modules/ContextMenu.js | 8 +-- src/common/modules/Notifications.js | 4 +- .../modules/data/BrowserCommunicationTypes.js | 3 +- src/content_scripts/autocorrect.js | 58 +++++++++++-------- src/content_scripts/chrome.js | 2 +- src/manifest.json | 8 +-- src/options/modules/CustomOptionTriggers.js | 6 +- src/options/options.html | 2 +- 15 files changed, 72 insertions(+), 64 deletions(-) diff --git a/.eslintrc b/.eslintrc index e361a86..c360d7d 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,7 +1,7 @@ { "root": true, "parserOptions": { - "ecmaVersion": 12, + "ecmaVersion": 14, "sourceType": "module", "ecmaFeatures": { "impliedStrict": true @@ -10,7 +10,7 @@ "env": { "browser": true, "webextensions": true, - "es2021": true + "es2023": true }, "extends": "eslint:recommended", "rules": { diff --git a/scripts/manifests/chromemanifest.json b/scripts/manifests/chromemanifest.json index 3bce396..9763385 100644 --- a/scripts/manifests/chromemanifest.json +++ b/scripts/manifests/chromemanifest.json @@ -9,13 +9,12 @@ "homepage_url": "https://github.com/rugk/unicodify", "options_ui": { - "page": "options/options.html", - "browser_style": true, - "chrome_style": true + "page": "options/options.html" }, "background": { - "page": "background/background.html" + "scripts": ["browser-polyfill.js", "background/modules/InstallUpgrade.js", "background/background.js"], + "type": "module" }, "content_scripts": [ { diff --git a/scripts/manifests/dev.json b/scripts/manifests/dev.json index edc24ee..b1f677e 100644 --- a/scripts/manifests/dev.json +++ b/scripts/manifests/dev.json @@ -9,12 +9,12 @@ "homepage_url": "https://github.com/rugk/unicodify", "options_ui": { - "page": "options/options.html", - "browser_style": true + "page": "options/options.html" }, "background": { - "page": "background/background.html" + "scripts": ["background/modules/InstallUpgrade.js", "background/background.js"], + "type": "module" }, "content_scripts": [ { @@ -45,7 +45,7 @@ "browser_specific_settings": { "gecko": { "id": "unicodify@rugk.github.io", - "strict_min_version": "87.0" + "strict_min_version": "112.0" } } } diff --git a/scripts/manifests/firefox.json b/scripts/manifests/firefox.json index 059bc83..072bcfe 100644 --- a/scripts/manifests/firefox.json +++ b/scripts/manifests/firefox.json @@ -9,12 +9,12 @@ "homepage_url": "https://github.com/rugk/unicodify", "options_ui": { - "page": "options/options.html", - "browser_style": true + "page": "options/options.html" }, "background": { - "page": "background/background.html" + "scripts": ["background/modules/InstallUpgrade.js", "background/background.js"], + "type": "module" }, "content_scripts": [ { @@ -43,7 +43,7 @@ "browser_specific_settings": { "gecko": { "id": "unicodify@rugk.github.io", - "strict_min_version": "87.0" + "strict_min_version": "112.0" } } } diff --git a/scripts/manifests/thunderbirdmanifest.json b/scripts/manifests/thunderbirdmanifest.json index 30a2119..d324a51 100644 --- a/scripts/manifests/thunderbirdmanifest.json +++ b/scripts/manifests/thunderbirdmanifest.json @@ -9,12 +9,12 @@ "homepage_url": "https://github.com/rugk/unicodify", "options_ui": { - "page": "options/options.html", - "browser_style": true + "page": "options/options.html" }, "background": { - "page": "background/background.html" + "scripts": ["background/modules/InstallUpgrade.js", "background/background.js"], + "type": "module" }, "content_scripts": [ { @@ -47,7 +47,7 @@ "browser_specific_settings": { "gecko": { "id": "unicodify@rugk.github.io", - "strict_min_version": "91.0" + "strict_min_version": "112.0" } } } diff --git a/src/background/background.html b/src/background/background.html index f7d179d..a5d88a2 100644 --- a/src/background/background.html +++ b/src/background/background.html @@ -7,7 +7,7 @@ - + diff --git a/src/background/modules/AutocorrectHandler.js b/src/background/modules/AutocorrectHandler.js index a588864..288a354 100644 --- a/src/background/modules/AutocorrectHandler.js +++ b/src/background/modules/AutocorrectHandler.js @@ -213,8 +213,8 @@ function sendSettings(autocorrect) { enabled: settings.enabled, quotes: settings.quotes, fracts: settings.fracts, - autocorrections: autocorrections, - longest: longest, + autocorrections, + longest, symbolpatterns: IS_CHROME ? symbolpatterns.source : symbolpatterns, antipatterns: IS_CHROME ? antipatterns.source : antipatterns } @@ -260,8 +260,8 @@ browser.runtime.onMessage.addListener((message) => { enabled: settings.enabled, quotes: settings.quotes, fracts: settings.fracts, - autocorrections: autocorrections, - longest: longest, + autocorrections, + longest, symbolpatterns: IS_CHROME ? symbolpatterns.source : symbolpatterns, antipatterns: IS_CHROME ? antipatterns.source : antipatterns }; diff --git a/src/background/modules/ContextMenu.js b/src/background/modules/ContextMenu.js index f1b3de1..a3af1e4 100644 --- a/src/background/modules/ContextMenu.js +++ b/src/background/modules/ContextMenu.js @@ -61,10 +61,10 @@ function handleMenuChoosen(info, tab) { return; } - browser.tabs.executeScript(tab.id, { - code: `insertIntoPage(${JSON.stringify(output)});`, - frameId: info.frameId - }); + browser.tabs.sendMessage(tab.id, { + type: COMMUNICATION_MESSAGE_TYPE.INSERT, + text: output + }, { frameId: info.frameId }); } /** diff --git a/src/common/modules/Notifications.js b/src/common/modules/Notifications.js index d7e31bd..dd9f65a 100644 --- a/src/common/modules/Notifications.js +++ b/src/common/modules/Notifications.js @@ -41,7 +41,7 @@ export function showNotification(title, content, substitutions, requiredNotifica browser.notifications.create({ type: "basic", iconUrl: browser.runtime.getURL(ICON), - title: title, + title, message: content }); } @@ -59,7 +59,7 @@ async function init() { init(); -BrowserCommunication.addListener(COMMUNICATION_MESSAGE_TYPE.NOTIFICATIONS, async (request) => { +BrowserCommunication.addListener(COMMUNICATION_MESSAGE_TYPE.NOTIFICATIONS, (request) => { const notifications = request.optionValue; SEND = notifications.send; diff --git a/src/common/modules/data/BrowserCommunicationTypes.js b/src/common/modules/data/BrowserCommunicationTypes.js index 1dd9397..9b1cebc 100644 --- a/src/common/modules/data/BrowserCommunicationTypes.js +++ b/src/common/modules/data/BrowserCommunicationTypes.js @@ -17,5 +17,6 @@ export const COMMUNICATION_MESSAGE_TYPE = Object.freeze({ AUTOCORRECT_CONTENT: "autocorrectContent", UNICODE_FONT: "unicodeFont", UPDATE_CONTEXT_MENU: "updateContextMenu", - NOTIFICATIONS: "notifications" + NOTIFICATIONS: "notifications", + INSERT: "insert" }); diff --git a/src/content_scripts/autocorrect.js b/src/content_scripts/autocorrect.js index 2c3bdae..d1ffa1b 100644 --- a/src/content_scripts/autocorrect.js +++ b/src/content_scripts/autocorrect.js @@ -29,6 +29,7 @@ const constants = Object.freeze({ // communication type // directly include magic constant as a workaround as we cannot import modules in content scripts due to https://bugzilla.mozilla.org/show_bug.cgi?id=1451545 const AUTOCORRECT_CONTENT = "autocorrectContent"; +const INSERT = "insert"; let insertedText; // Last insert text let deletedText; // Last deleted text @@ -272,7 +273,7 @@ function firstDifferenceIndex(a, b) { */ function autocorrect(event) { // console.log('beforeinput', event.inputType, event.data); - if (!(event.inputType === "insertText" || event.inputType === "insertCompositionText" || event.inputType === "insertParagraph" || event.inputType === "insertLineBreak")) { + if (!["insertText", "insertCompositionText", "insertParagraph", "insertLineBreak"].includes(event.inputType)) { return; } if (!symbolpatterns) { @@ -287,7 +288,7 @@ function autocorrect(event) { if (caretposition) { const value = target.value || target.innerText; let deletecount = 0; - let insert = event.inputType === "insertLineBreak" || event.inputType === "insertParagraph" ? "\n" : event.data; + let insert = ["insertLineBreak", "insertParagraph"].includes(event.inputType) ? "\n" : event.data; const inserted = insert; let output = false; // Use Unicode smart quotes @@ -311,8 +312,9 @@ function autocorrect(event) { const aregexResult = symbolpatterns.exec(text); const aaregexResult = antipatterns.exec(text); if (!aaregexResult && (!aregexResult || (caretposition <= longest ? regexResult.index < aregexResult.index : regexResult.index <= aregexResult.index))) { - insert = autocorrections[regexResult[0]] + inserted; - deletecount = regexResult[0].length; + const [autocorrection] = regexResult; + insert = autocorrections[autocorrection] + inserted; + deletecount = autocorrection.length; output = true; } } else { @@ -327,11 +329,12 @@ function autocorrect(event) { const text = value.slice(0, caretposition) + inserted; const aregexResult = numberRegex.exec(text); if (!aregexResult) { - const label = outputLabel(regexResult[0], regexResult.groups.fractionpart); - const index = firstDifferenceIndex(label, regexResult[0]); + const [number] = regexResult; + const label = outputLabel(number, regexResult.groups.fractionpart); + const index = firstDifferenceIndex(label, number); if (index >= 0) { insert = label.slice(index) + inserted; - deletecount = regexResult[0].length - index; + deletecount = number.length - index; output = true; } } @@ -404,24 +407,29 @@ function undoAutocorrect(event) { * @returns {void} */ function handleResponse(message, sender) { - if (message.type !== AUTOCORRECT_CONTENT) { - return; - } - enabled = message.enabled; - quotes = message.quotes; - fracts = message.fracts; - autocorrections = message.autocorrections; - longest = message.longest; - symbolpatterns = IS_CHROME ? new RegExp(message.symbolpatterns, "u") : message.symbolpatterns; - antipatterns = IS_CHROME ? new RegExp(message.antipatterns, "u") : message.antipatterns; - // console.log(message); - - if (enabled) { - addEventListener("beforeinput", undoAutocorrect, true); - addEventListener("beforeinput", autocorrect, true); - } else { - removeEventListener("beforeinput", undoAutocorrect, true); - removeEventListener("beforeinput", autocorrect, true); + if (message.type === AUTOCORRECT_CONTENT) { + ({ + enabled, + quotes, + fracts, + autocorrections, + longest, + symbolpatterns, + antipatterns + } = message); + symbolpatterns = IS_CHROME ? new RegExp(symbolpatterns, "u") : symbolpatterns; + antipatterns = IS_CHROME ? new RegExp(antipatterns, "u") : antipatterns; + // console.log(message); + + if (enabled) { + addEventListener("beforeinput", undoAutocorrect, true); + addEventListener("beforeinput", autocorrect, true); + } else { + removeEventListener("beforeinput", undoAutocorrect, true); + removeEventListener("beforeinput", autocorrect, true); + } + } else if (message.type === INSERT) { + insertIntoPage(message.text); } } diff --git a/src/content_scripts/chrome.js b/src/content_scripts/chrome.js index aecae04..cd9c492 100644 --- a/src/content_scripts/chrome.js +++ b/src/content_scripts/chrome.js @@ -5,6 +5,6 @@ document.addEventListener("selectionchange", () => { const selection = document.getSelection().toString(); browser.runtime.sendMessage({ type: UPDATE_CONTEXT_MENU, - selection: selection + selection }); }); diff --git a/src/manifest.json b/src/manifest.json index edc24ee..b1f677e 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -9,12 +9,12 @@ "homepage_url": "https://github.com/rugk/unicodify", "options_ui": { - "page": "options/options.html", - "browser_style": true + "page": "options/options.html" }, "background": { - "page": "background/background.html" + "scripts": ["background/modules/InstallUpgrade.js", "background/background.js"], + "type": "module" }, "content_scripts": [ { @@ -45,7 +45,7 @@ "browser_specific_settings": { "gecko": { "id": "unicodify@rugk.github.io", - "strict_min_version": "87.0" + "strict_min_version": "112.0" } } } diff --git a/src/options/modules/CustomOptionTriggers.js b/src/options/modules/CustomOptionTriggers.js index 14aa2b6..2780fe3 100644 --- a/src/options/modules/CustomOptionTriggers.js +++ b/src/options/modules/CustomOptionTriggers.js @@ -30,7 +30,7 @@ function applyAutocorrectPermissions(optionValue, option, event) { // trigger update for current session browser.runtime.sendMessage({ type: COMMUNICATION_MESSAGE_TYPE.AUTOCORRECT_BACKGROUND, - optionValue: optionValue + optionValue }); } @@ -47,7 +47,7 @@ function applyUnicodeFontSettings(optionValue) { // trigger update for current session browser.runtime.sendMessage({ type: COMMUNICATION_MESSAGE_TYPE.UNICODE_FONT, - optionValue: optionValue + optionValue }); } @@ -64,7 +64,7 @@ function applyNotificationSettings(optionValue) { // trigger update for current session browser.runtime.sendMessage({ type: COMMUNICATION_MESSAGE_TYPE.NOTIFICATIONS, - optionValue: optionValue + optionValue }); } diff --git a/src/options/options.html b/src/options/options.html index ce61da7..b1495d7 100644 --- a/src/options/options.html +++ b/src/options/options.html @@ -6,7 +6,7 @@ - +