Skip to content

Commit

Permalink
Using sync instead Of Local Storage (#269)
Browse files Browse the repository at this point in the history
* SYNC Using  instead Of Local Storage

* cleanup
  • Loading branch information
hrai authored Oct 14, 2024
1 parent 51d941d commit 46d39b1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
14 changes: 3 additions & 11 deletions distribution/manifest_v2.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
"<all_urls>"
],
"matches": ["<all_urls>"],
"js": [
"dependencies/jquery.min.js",
"lib/main.bundle.js",
Expand All @@ -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}"
Expand Down
2 changes: 1 addition & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
38 changes: 20 additions & 18 deletions src/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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();
Expand Down
14 changes: 7 additions & 7 deletions src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
shouldCapitaliseLocations,
} from './plugin-constants';

browser.storage.local
browser.storage.sync
.get([sitesToIgnore, wordsToExclude, wordsToInclude])
.then(updateIgnoreLists, onError);

Expand Down Expand Up @@ -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,
});

Expand All @@ -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,
});

Expand All @@ -74,7 +74,7 @@ $(document).on(
function () {
var excludedWords = getExcludedWords();

browser.storage.local.set({
browser.storage.sync.set({
wordsToExclude: excludedWords,
});

Expand All @@ -89,7 +89,7 @@ $(document).on(
function () {
var includedWords = getIncludedWords();

browser.storage.local.set({
browser.storage.sync.set({
wordsToInclude: includedWords,
});

Expand All @@ -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) {
Expand Down Expand Up @@ -134,7 +134,7 @@ function setupCheckboxChangeEventHandlers(flagName) {
}

function setShouldCapitaliseVariable(variableName, value) {
browser.storage.local.set({
browser.storage.sync.set({
[variableName]: value,
});
}
Expand Down

0 comments on commit 46d39b1

Please sign in to comment.