Skip to content

Commit

Permalink
Using chrome storage sync data
Browse files Browse the repository at this point in the history
  • Loading branch information
wtser committed Mar 20, 2020
1 parent f2d47cf commit 2c85e81
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
16 changes: 15 additions & 1 deletion background.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
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 @@ -72,4 +84,6 @@ chrome.webRequest.onBeforeRequest.addListener(

updateRollres();

window.addEventListener("storage", updateRollres, false);
chrome.storage.onChanged.addListener(function() {
updateRollres();
});
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"manifest_version": 2,
"name": "RollRes - Change the response of the request",
"version": "0.0.1",
"version": "0.0.2",
"permissions": [
"tabs",
"webRequest",
"webRequestBlocking",
"<all_urls>",
"unlimitedStorage"
"storage"
],
"description": "Change the response of the request.",
"icons": {
Expand Down
15 changes: 9 additions & 6 deletions page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
(function() {
const rollres = JSON.parse(localStorage.getItem("rollres")) || [];
chrome.storage.sync.get("rollres", function(content) {
let rollres = content.rollres || [];
if (!rollres) {
chrome.storage.sync.set({ rollres: [] });
}

var $list = document.getElementById("list");
var $addBtn = document.getElementById("addBtn");
Expand Down Expand Up @@ -49,7 +52,7 @@
} else {
rollres[updateIndex] = obj;
}
localStorage.setItem("rollres", JSON.stringify(rollres));
chrome.storage.sync.set({ rollres: rollres });

renderList(rollres);
}
Expand All @@ -69,7 +72,7 @@
confirm(`Are you sure to remove ${rollres[updateIndex].responseUrl}?`)
) {
rollres.splice(updateIndex, 1);
localStorage.setItem("rollres", JSON.stringify(rollres));
chrome.storage.sync.set({ rollres: rollres });
$dialog.close();
renderList(rollres);
}
Expand All @@ -80,7 +83,7 @@

if (e.target.type === "checkbox") {
rollres[updateIndex].enable = e.target.checked;
localStorage.setItem("rollres", JSON.stringify(rollres));
chrome.storage.sync.set({ rollres: rollres });
}

if (e.target.type === "button") {
Expand All @@ -92,4 +95,4 @@
$deleteBtn.removeAttribute("hidden");
}
});
})();
});

0 comments on commit 2c85e81

Please sign in to comment.