From 6da96e3bc95f26a51a0fd58fe45f3ad9d4d42d92 Mon Sep 17 00:00:00 2001 From: wtser Date: Sun, 22 Mar 2020 21:42:15 +0800 Subject: [PATCH] Using localStorage reduce storage.sync request --- background.js | 15 +-------------- manifest.json | 2 +- page.js | 11 ++++++++--- 3 files changed, 10 insertions(+), 18 deletions(-) diff --git a/background.js b/background.js index 2a95146..c088514 100644 --- a/background.js +++ b/background.js @@ -1,18 +1,7 @@ var rollres = []; -const getSyncFile = function() { - return new Promise(function(reslove, reject) { - chrome.storage.sync.get("rollres", function(content) { - reslove(content.rollres); - }); - }); -}; - function updateRollres() { rollres = JSON.parse(localStorage.getItem("rollres")) || []; - getSyncFile().then(function(syncRollres) { - rollres = syncRollres; - }); } var typeMap = { @@ -84,6 +73,4 @@ chrome.webRequest.onBeforeRequest.addListener( updateRollres(); -chrome.storage.onChanged.addListener(function() { - updateRollres(); -}); +window.addEventListener("storage", updateRollres, false); diff --git a/manifest.json b/manifest.json index 98848a1..d22f841 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 2, "name": "RollRes - Change the response of the request", - "version": "0.0.2", + "version": "0.0.3", "permissions": [ "tabs", "webRequest", diff --git a/page.js b/page.js index 1f86fea..01e62a5 100644 --- a/page.js +++ b/page.js @@ -1,3 +1,8 @@ +function saveData(rollres) { + localStorage.setItem("rollres", JSON.stringify(rollres)); + chrome.storage.sync.set({ rollres }); +} + chrome.storage.sync.get("rollres", function(content) { let rollres = content.rollres || []; if (!rollres) { @@ -52,7 +57,7 @@ chrome.storage.sync.get("rollres", function(content) { } else { rollres[updateIndex] = obj; } - chrome.storage.sync.set({ rollres: rollres }); + saveData(rollres); renderList(rollres); } @@ -72,7 +77,7 @@ chrome.storage.sync.get("rollres", function(content) { confirm(`Are you sure to remove ${rollres[updateIndex].responseUrl}?`) ) { rollres.splice(updateIndex, 1); - chrome.storage.sync.set({ rollres: rollres }); + saveData(rollres); $dialog.close(); renderList(rollres); } @@ -83,7 +88,7 @@ chrome.storage.sync.get("rollres", function(content) { if (e.target.type === "checkbox") { rollres[updateIndex].enable = e.target.checked; - chrome.storage.sync.set({ rollres: rollres }); + saveData(rollres); } if (e.target.type === "button") {