Skip to content

Commit

Permalink
Using localStorage reduce storage.sync request
Browse files Browse the repository at this point in the history
  • Loading branch information
wtser committed Mar 22, 2020
1 parent 2c85e81 commit 6da96e3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
15 changes: 1 addition & 14 deletions background.js
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -84,6 +73,4 @@ chrome.webRequest.onBeforeRequest.addListener(

updateRollres();

chrome.storage.onChanged.addListener(function() {
updateRollres();
});
window.addEventListener("storage", updateRollres, false);
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
11 changes: 8 additions & 3 deletions page.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand All @@ -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") {
Expand Down

0 comments on commit 6da96e3

Please sign in to comment.