From 433149e6cdd1fba1ccb04278f46fb7cf79dd4187 Mon Sep 17 00:00:00 2001 From: Hangjit Rai Date: Tue, 15 Oct 2024 15:49:28 +1100 Subject: [PATCH] cleanup --- src/content.js | 78 ++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/src/content.js b/src/content.js index 549f771..1558cac 100644 --- a/src/content.js +++ b/src/content.js @@ -23,7 +23,7 @@ console.log(errorMsg); browser.storage.local .get([constantsKeyVal, namesKeyVal, acronymsKeyVal, locationsKeyVal]) - .then(() => { + .then((localDict) => { browser.storage.sync .get([ sitesToIgnore, @@ -34,9 +34,47 @@ browser.storage.local wordsToExclude, wordsToInclude, ]) - .then(processResponse, utils.onError); + .then((remoteDict) => { + processResponse({ ...localDict, ...remoteDict }); + }, utils.onError); }, utils.onError); +function processResponse(item) { + if (item.sitesToIgnore) { + sitesToExclude = sitesToExclude.concat(item.sitesToIgnore); + } + + setOptions(item); + setKeyValues(item); + + if (item && sitesToExclude) { + //https://stackoverflow.com/questions/406192/get-current-url-with-jquery + var currentUrlDomain = window.location.origin; + + try { + var shouldEnableCapitalisingOnCurrentSite = true; + + $.each(sitesToExclude, function (_i, siteToExclude) { + if (currentUrlDomain.includes(siteToExclude)) { + shouldEnableCapitalisingOnCurrentSite = false; + } + }); + + if (shouldEnableCapitalisingOnCurrentSite) { + hookupEventHandlers(); + + throw new Error(errorMsg); + } + } catch (e) { + if (e.message !== errorMsg) { + throw e; + } + } + } else { + hookupEventHandlers(); + } +} + /* Updating the value of this sync storage variable in settings.js happens AFTER content.js. * The browser doesn't register the change and doesn't capitalise I by default after installing the extension. * This block will capture the event and update the value of 'shouldCapitaliseI'. @@ -119,42 +157,6 @@ function setKeyValues(item) { utils.setWordsToExclude(item.wordsToExclude); } -function processResponse(item) { - if (item.sitesToIgnore) { - sitesToExclude = sitesToExclude.concat(item.sitesToIgnore); - } - - setOptions(item); - setKeyValues(item); - - if (item && sitesToExclude) { - //https://stackoverflow.com/questions/406192/get-current-url-with-jquery - var currentUrlDomain = window.location.origin; - - try { - var shouldEnableCapitalisingOnCurrentSite = true; - - $.each(sitesToExclude, function (_i, siteToExclude) { - if (currentUrlDomain.includes(siteToExclude)) { - shouldEnableCapitalisingOnCurrentSite = false; - } - }); - - if (shouldEnableCapitalisingOnCurrentSite) { - hookupEventHandlers(); - - throw new Error(errorMsg); - } - } catch (e) { - if (e.message !== errorMsg) { - throw e; - } - } - } else { - hookupEventHandlers(); - } -} - /*eslint no-debugger: "error"*/ function observeHtmlBody() { var target = document.querySelector('body');