From e1a41b305fc430bbb4b2233aae3f045e17067e08 Mon Sep 17 00:00:00 2001 From: magnolia1234 <7676006-magnolia1234@users.noreply.gitlab.com> Date: Sat, 31 Oct 2020 20:06:38 +0100 Subject: [PATCH] Save options to storage.local (quota exceeded) Add-on sync disabled --- background.js | 43 +++++++++++++++++++++++++++++++--------- bpc_count_daily_users.js | 11 ++++++++-- changelog.txt | 1 + manifest.json | 2 +- optin/opt-in.js | 12 +++++------ options.js | 4 ++-- options_custom.js | 18 ++++++++--------- 7 files changed, 62 insertions(+), 29 deletions(-) diff --git a/background.js b/background.js index 6e7bffa2..4707a763 100644 --- a/background.js +++ b/background.js @@ -277,16 +277,39 @@ var customSites = {}; var customSites_domains = []; function setDefaultOptions() { - ext_api.storage.sync.set({ + ext_api.storage.local.set({ sites: defaultSites }, function () { ext_api.runtime.openOptionsPage(); }); } +// copy storage.sync to storage.local (quota exceeded) +ext_api.storage.sync.get({ + sites: {}, + sites_custom: {}, + daily_users: {}, + optIn: {}, + optInShown: {}, + customShown: {} +}, function (items) { + if (Object.keys(items.sites).length > 0) { + ext_api.storage.local.set({ + sites: items.sites, + sites_custom: items.sites_custom, + daily_users: items.daily_users, + optIn: items.optIn, + optInShown: items.optInShown, + customShown: items.customShown + }, function () { + ext_api.storage.sync.remove(['sites', 'sites_custom']); + }); + } +}); + // Get the enabled sites (from local storage) & add to allow/remove_cookies (if not already in one of these arrays) // Add googlebot- and block_javascript-settings for custom sites -ext_api.storage.sync.get({ +ext_api.storage.local.get({ sites: {}, sites_custom: {} }, function (items) { @@ -363,6 +386,8 @@ ext_api.storage.sync.get({ // Listen for changes to options ext_api.storage.onChanged.addListener(function (changes, namespace) { + if (namespace === 'sync') + return; for (var key in changes) { var storageChange = changes[key]; if (key === 'sites') { @@ -407,7 +432,7 @@ ext_api.storage.onChanged.addListener(function (changes, namespace) { var sites_custom_added = Object.keys(sites_custom).filter(x => !Object.keys(sites_custom_old).includes(x) && !defaultSites.hasOwnProperty(x)); var sites_custom_removed = Object.keys(sites_custom_old).filter(x => !Object.keys(sites_custom).includes(x) && !defaultSites.hasOwnProperty(x)); - chrome.storage.sync.get({ + ext_api.storage.local.get({ sites: {} }, function (items) { var sites = items.sites; @@ -416,7 +441,7 @@ ext_api.storage.onChanged.addListener(function (changes, namespace) { for (var key of sites_custom_removed) delete sites[key]; - chrome.storage.sync.set({ + ext_api.storage.local.set({ sites: sites }, function () { true; @@ -769,7 +794,7 @@ function site_switch() { removed_site.push(site_title); else added_site.push(site_title); - chrome.storage.sync.get({ + ext_api.storage.local.get({ sites: {} }, function (items) { var sites = items.sites; @@ -778,7 +803,7 @@ function site_switch() { for (var key of removed_site) delete sites[key]; - chrome.storage.sync.set({ + ext_api.storage.local.set({ sites: sites }, function () { true; @@ -844,7 +869,7 @@ var chrome_scheme = 'light'; ext_api.runtime.onMessage.addListener(function (message, sender) { // check storage for opt in if (message.request === 'optin') { - ext_api.storage.sync.get("optIn", function (result) { + ext_api.storage.local.get("optIn", function (result) { // send message back to content script with value of opt in ext_api.tabs.sendMessage( sender.tab.id, { @@ -877,12 +902,12 @@ ext_api.runtime.onMessage.addListener(function (message, sender) { }); // show the tab if we haven't registered the user reacting to the prompt. -ext_api.storage.sync.get(["optInShown", "customShown"], function (result) { +ext_api.storage.local.get(["optInShown", "customShown"], function (result) { if (!result.optInShown || !result.customShown) { ext_api.tabs.create({ url: "optin/opt-in.html" }); - ext_api.storage.sync.set({ + ext_api.storage.local.set({ "optInShown": true, "customShown": true }); diff --git a/bpc_count_daily_users.js b/bpc_count_daily_users.js index d5fb7215..c04793ea 100644 --- a/bpc_count_daily_users.js +++ b/bpc_count_daily_users.js @@ -2,13 +2,18 @@ var ext_api = (typeof browser === 'object') ? browser : chrome; // daily users counter function bpc_count_daily_users(dateStr) { + ext_api.storage.local.get({ + daily_users: {}, + }, function (items_local) { + daily_users = items_local.daily_users; ext_api.storage.sync.get({ daily_users: {}, }, function (items) { - var daily_users = items.daily_users; + if (!items_local.daily_users.date) + daily_users = items.daily_users; if (daily_users.date !== dateStr) { daily_users.date = dateStr; - chrome.storage.sync.set({ + ext_api.storage.local.set({ daily_users: daily_users }, function () { true; @@ -17,6 +22,7 @@ function bpc_count_daily_users(dateStr) { fetch(count_json, {mode: 'no-cors'}); } }); + }); } function currentDateStr() { @@ -25,4 +31,5 @@ function currentDateStr() { return dateStr; } var last_date_str = currentDateStr(); +var daily_users; bpc_count_daily_users(last_date_str); diff --git a/changelog.txt b/changelog.txt index 0e88d57c..c6024397 100644 --- a/changelog.txt +++ b/changelog.txt @@ -6,6 +6,7 @@ Add Deutsche Wirtschafts Nachrichten Add WirtschaftsWoche (Germany) Fix-update Quartz (newsletter) Icon for dark/incognito mode (Chrome) +Save options to storage.local (quota exceeded) * v1.9.2.0 (2020-10-25) Add Foreign Affairs diff --git a/manifest.json b/manifest.json index 05b70e97..1e2170c5 100644 --- a/manifest.json +++ b/manifest.json @@ -338,5 +338,5 @@ "webRequest", "webRequestBlocking" ], - "version": "1.9.2.3" + "version": "1.9.2.4" } \ No newline at end of file diff --git a/optin/opt-in.js b/optin/opt-in.js index f7308e42..ab98f8a4 100644 --- a/optin/opt-in.js +++ b/optin/opt-in.js @@ -3,14 +3,14 @@ var ext_api = chrome || browser; window.addEventListener("load", function () { var opt_in_enabled = document.getElementById('opt-in-enabled'); - ext_api.storage.sync.get("optIn", function (result) { + ext_api.storage.local.get("optIn", function (result) { opt_in_enabled.innerText = result.optIn ? 'YES' : 'NO'; }); document.getElementById("optin-enable").addEventListener( "click", function () { - ext_api.storage.sync.set({ + ext_api.storage.local.set({ "optIn": true, "optInShown": true }); @@ -23,7 +23,7 @@ window.addEventListener("load", function () { document.getElementById("optin-disable").addEventListener( "click", function () { - ext_api.storage.sync.set({ + ext_api.storage.local.set({ "optIn": false, "optInShown": true }); @@ -36,7 +36,7 @@ window.addEventListener("load", function () { document.getElementById("button-close").addEventListener( "click", function () { - ext_api.storage.sync.set({ + ext_api.storage.local.set({ "optInShown": true, "customShown": true }); @@ -63,7 +63,7 @@ window.addEventListener("load", function () { } else { custom_enabled.innerText = 'NO'; } - ext_api.storage.sync.set({ + ext_api.storage.local.set({ "customShown": true }); }); @@ -76,7 +76,7 @@ window.addEventListener("load", function () { if (removed) { custom_enabled.innerText = 'NO'; } else {} - ext_api.storage.sync.set({ + ext_api.storage.local.set({ "customShown": true }); }); diff --git a/options.js b/options.js index bd59acaa..505ca294 100644 --- a/options.js +++ b/options.js @@ -15,7 +15,7 @@ function save_options() { return memo; }, {}); - ext_api.storage.sync.set({ + ext_api.storage.local.set({ sites: sites }, function() { // Update status to let user know options were saved. @@ -42,7 +42,7 @@ function save_options() { // Restores checkbox input states using the preferences stored in ext_api.storage. function renderOptions() { - ext_api.storage.sync.get({ + ext_api.storage.local.get({ sites: {}, sites_custom: {} }, function(items) { var sites = items.sites; diff --git a/options_custom.js b/options_custom.js index e849d316..8822e2b7 100644 --- a/options_custom.js +++ b/options_custom.js @@ -19,7 +19,7 @@ function save_options() { var sites_custom = {}; if (textareaEl.value !== '') var sites_custom = JSON.parse(textareaEl.value); - ext_api.storage.sync.set({ + ext_api.storage.local.set({ sites_custom: sites_custom }, function () { // Update status to let user know custom sites were saved. @@ -47,7 +47,7 @@ function sort_options() { // Export custom sites to file function export_options() { - ext_api.storage.sync.get({ + ext_api.storage.local.get({ sites_custom: {} }, function (items) { var result = JSON.stringify(items.sites_custom); @@ -71,7 +71,7 @@ function import_options(e) { function _imp() { let sites_custom = JSON.parse(this.result); - ext_api.storage.sync.set({ + ext_api.storage.local.set({ sites_custom: sites_custom }, function () { // Update status to let user know custom sites were imported. @@ -109,7 +109,7 @@ function add_options() { sites_custom[title]['domain'] = sites_custom[title]['domain'].replace('www.', '').toLowerCase(); // add new site to local storage - ext_api.storage.sync.get({ + ext_api.storage.local.get({ sites_custom: {} }, function (items) { var sites_custom_old = items.sites_custom; @@ -118,7 +118,7 @@ function add_options() { sites_custom_old[key] = sites_custom[key]; } - ext_api.storage.sync.set({ + ext_api.storage.local.set({ sites_custom: sites_custom_old }, function () { // Update status to let user know new custom site was added. @@ -140,13 +140,13 @@ function delete_options() { var remove_key = selectEl.value; // delete site from local storage - ext_api.storage.sync.get({ + ext_api.storage.local.get({ sites_custom: {} }, function (items) { var sites_custom_old = items.sites_custom; delete sites_custom_old[remove_key]; - ext_api.storage.sync.set({ + ext_api.storage.local.set({ sites_custom: sites_custom_old }, function () { // Update status to let user know custom site was deleted. @@ -167,7 +167,7 @@ function edit_options() { var title = selectEl.value; // copy site to add-fields - ext_api.storage.sync.get({ + ext_api.storage.local.get({ sites_custom: {} }, function (items) { sites_custom = items.sites_custom; @@ -182,7 +182,7 @@ function edit_options() { // Restores checkbox input states using the preferences stored in ext_api.storage. function renderOptions() { - ext_api.storage.sync.get({ + ext_api.storage.local.get({ sites_custom: {} }, function (items) { var sites_custom = items.sites_custom;