From 2cf1237160cc1d8cbaf759f6bc70f53fca84b6cb Mon Sep 17 00:00:00 2001 From: Teal Dulcet Date: Mon, 25 Sep 2023 04:16:07 -0700 Subject: [PATCH] Simplified content script and fixed typo. --- src/common/modules/PageHandler.js | 2 +- src/content_scripts/autocorrect.js | 32 ++++++++++++++++++------------ 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/src/common/modules/PageHandler.js b/src/common/modules/PageHandler.js index 0bb5a9b..71a599c 100644 --- a/src/common/modules/PageHandler.js +++ b/src/common/modules/PageHandler.js @@ -18,7 +18,7 @@ export async function insertIntoPage(text) { active: true }); - const promises = tabs.map(tab) => { + const promises = tabs.map((tab) => { // send request to insert emoji // This will not work in Manifest V3: https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#executing-arbitrary-strings return browser.tabs.executeScript(tab.id, { diff --git a/src/content_scripts/autocorrect.js b/src/content_scripts/autocorrect.js index 8b55dbf..350c302 100644 --- a/src/content_scripts/autocorrect.js +++ b/src/content_scripts/autocorrect.js @@ -214,8 +214,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 { @@ -227,9 +228,10 @@ function autocorrect(event) { const text = value.slice(caretposition < length ? 0 : caretposition - length, caretposition) + inserted; const regexResult = re.exec(text); if (regexResult) { - const aregexResult = Object.keys(emojiShortcodes).filter((item) => item.indexOf(regexResult[0]) === 0); - if (aregexResult.length >= 1 && (regexResult[0].length > 2 || aregexResult[0].length === 3)) { - const ainsert = aregexResult[0].slice(regexResult[0].length); + const [shortcode] = regexResult; + const aregexResult = Object.keys(emojiShortcodes).filter((item) => item.indexOf(shortcode) === 0); + if (aregexResult.length >= 1 && (shortcode.length > 2 || aregexResult[0].length === 3)) { + const ainsert = aregexResult[0].slice(shortcode.length); if (autocompleteSelect || aregexResult.length > 1) { event.preventDefault(); @@ -317,14 +319,18 @@ function handleResponse(message, sender) { if (message.type !== AUTOCORRECT_CONTENT) { return; } - enabled = message.enabled; - autocomplete = message.autocomplete; - autocompleteSelect = message.autocompleteSelect; - 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; - emojiShortcodes = message.emojiShortcodes; + ({ + enabled, + autocomplete, + autocompleteSelect, + autocorrections, + longest, + symbolpatterns, + antipatterns, + emojiShortcodes + } = message); + symbolpatterns = IS_CHROME ? new RegExp(symbolpatterns, "u") : symbolpatterns; + antipatterns = IS_CHROME ? new RegExp(antipatterns, "u") : antipatterns; if (enabled) { addEventListener("beforeinput", undoAutocorrect, true);