From 46d39b182bf17228984b6117986dd77eaf28ab9c Mon Sep 17 00:00:00 2001 From: hrai <4055444+hrai@users.noreply.github.com> Date: Mon, 14 Oct 2024 11:28:29 +1100 Subject: [PATCH] Using sync instead Of Local Storage (#269) * SYNC Using instead Of Local Storage * cleanup --- distribution/manifest_v2.json | 14 +++---------- src/background.js | 2 +- src/content.js | 38 ++++++++++++++++++----------------- src/settings.js | 14 ++++++------- 4 files changed, 31 insertions(+), 37 deletions(-) diff --git a/distribution/manifest_v2.json b/distribution/manifest_v2.json index 8f6cda9..a53f296 100644 --- a/distribution/manifest_v2.json +++ b/distribution/manifest_v2.json @@ -10,17 +10,12 @@ "default_popup": "popup/settings.html" }, "background": { - "scripts": [ - "dependencies/jquery.min.js", - "lib/background.bundle.js" - ] + "scripts": ["dependencies/jquery.min.js", "lib/background.bundle.js"] }, "content_scripts": [ { "run_at": "document_end", - "matches": [ - "" - ], + "matches": [""], "js": [ "dependencies/jquery.min.js", "lib/main.bundle.js", @@ -33,10 +28,7 @@ "48": "icons/auto-capitalise-sentence.png", "128": "icons/auto-capitalise-sentence.png" }, - "permissions": [ - "storage", - "tabs" - ], + "permissions": ["storage", "tabs"], "browser_specific_settings": { "gecko": { "id": "{680e06ed-65ed-4e11-a9c0-0e6f80b9a347}" diff --git a/src/background.js b/src/background.js index ad2e3af..5176a8a 100644 --- a/src/background.js +++ b/src/background.js @@ -12,7 +12,7 @@ import { } from './plugin-constants'; import browser from 'webextension-polyfill'; -browser.storage.local.set({ +browser.storage.sync.set({ [constantsKeyVal]: constantsKeyValuePairs, [namesKeyVal]: namesKeyValuePairs, [acronymsKeyVal]: acronymsKeyValuePairs, diff --git a/src/content.js b/src/content.js index 1fc51bf..2f9b790 100644 --- a/src/content.js +++ b/src/content.js @@ -19,7 +19,7 @@ import { const errorMsg = 'breaking loop'; let sitesToExclude = ['aws.amazon.com', 'whatsapp.com', 'messenger.com']; -browser.storage.local +browser.storage.sync .get([ sitesToIgnore, shouldCapitaliseI, @@ -39,26 +39,28 @@ browser.storage.local * 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'. */ -browser.storage.onChanged.addListener(function ( - changes, // object - areaName // string -) { - if (areaName === 'local') { - utils.toggleOptionsValue(changes, shouldCapitaliseI); - utils.toggleOptionsValue(changes, shouldCapitaliseNames); - utils.toggleOptionsValue(changes, shouldCapitaliseAcronyms); - utils.toggleOptionsValue(changes, shouldCapitaliseLocations); - - if (changes.wordsToExclude != null) { - const newValue = changes.wordsToExclude.newValue; - - if (newValue != null) { - utils.setWordsToExclude(newValue); +browser.storage.onChanged.addListener( + function ( + changes, // object + areaName // string + ) { + if (areaName === 'local') { + utils.toggleOptionsValue(changes, shouldCapitaliseI); + utils.toggleOptionsValue(changes, shouldCapitaliseNames); + utils.toggleOptionsValue(changes, shouldCapitaliseAcronyms); + utils.toggleOptionsValue(changes, shouldCapitaliseLocations); + + if (changes.wordsToExclude != null) { + const newValue = changes.wordsToExclude.newValue; + + if (newValue != null) { + utils.setWordsToExclude(newValue); + } } + //browser.runtime.reload() - reload browser } - //browser.runtime.reload() - reload browser } -}); +); function hookupEventHandlers() { observeInputTags(); diff --git a/src/settings.js b/src/settings.js index d488cbf..5b95a86 100644 --- a/src/settings.js +++ b/src/settings.js @@ -10,7 +10,7 @@ import { shouldCapitaliseLocations, } from './plugin-constants'; -browser.storage.local +browser.storage.sync .get([sitesToIgnore, wordsToExclude, wordsToInclude]) .then(updateIgnoreLists, onError); @@ -47,7 +47,7 @@ $(document).on(`click.${pluginNamespace}`, '#ignoreSiteButton', function () { var sites = getExcludedSites(); sites.push(hostname); - browser.storage.local.set({ + browser.storage.sync.set({ sitesToIgnore: sites, }); @@ -60,7 +60,7 @@ $(document).on(`click.${pluginNamespace}`, '#ignoreSiteButton', function () { $(document).on(`click.${pluginNamespace}`, '#submitButton', function () { var sites = getExcludedSites(); - browser.storage.local.set({ + browser.storage.sync.set({ sitesToIgnore: sites, }); @@ -74,7 +74,7 @@ $(document).on( function () { var excludedWords = getExcludedWords(); - browser.storage.local.set({ + browser.storage.sync.set({ wordsToExclude: excludedWords, }); @@ -89,7 +89,7 @@ $(document).on( function () { var includedWords = getIncludedWords(); - browser.storage.local.set({ + browser.storage.sync.set({ wordsToInclude: includedWords, }); @@ -104,7 +104,7 @@ loadFlagValuesFromBrowserStorage(shouldCapitaliseAcronyms); loadFlagValuesFromBrowserStorage(shouldCapitaliseLocations); function loadFlagValuesFromBrowserStorage(flagName) { - browser.storage.local.get(flagName).then((items) => { + browser.storage.sync.get(flagName).then((items) => { const flagValue = items[flagName]; if (flagValue === true || flagValue === undefined) { @@ -134,7 +134,7 @@ function setupCheckboxChangeEventHandlers(flagName) { } function setShouldCapitaliseVariable(variableName, value) { - browser.storage.local.set({ + browser.storage.sync.set({ [variableName]: value, }); }