1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 20:46:39 +02:00

Centralized settings management

This commit is contained in:
kkapsner 2017-11-07 00:36:44 +01:00
parent 18df3f97fe
commit 2193313980
24 changed files with 917 additions and 912 deletions

View file

@ -4,23 +4,16 @@
(function(){
"use strict";
const settings = require("./settings");
const logging = require("./logging");
const {error, warning, message, notice, verbose, } = logging;
const lists = require("./lists");
logging.setPrefix("main script");
message("start");
message("loading storage");
browser.storage.local.get().then(function(data){
Object.keys(data).forEach(function(key){
settings[key] = data[key];
});
settings.isStillDefault = false;
logging.clearQueue();
return settings;
}).then(function(settings){
message("start of background script");
message("waiting for settings to be loaded");
settings.onloaded(function(){
notice("everything loaded");
const lists = require("./lists");
lists.updateAll();
notice("build persistent storage");
var persistentRnd = Object.create(null);
@ -66,11 +59,17 @@
verbose("got new domain rnd", data["canvasBlocker-new-domain-rnd"]);
data["canvasBlocker-set-domain-rnd"] = data["canvasBlocker-new-domain-rnd"];
persistentRnd[data["canvasBlocker-new-domain-rnd"].domain] = data["canvasBlocker-new-domain-rnd"].rnd;
browser.storage.local.get("storePersistentRnd").then(function(prefs){
if (prefs.storePersistentRnd){
browser.storage.local.set({persistentRndStorage: JSON.stringify(persistentRnd)});
}
});
if (settings.storePersistentRnd){
settings.persistentRndStorage = JSON.stringify(persistentRnd);
}
updateContentScripts();
}
if (data["canvasBlocker-clear-domain-rnd"]){
verbose("domain rnd cleared");
persistentRnd = Object.create(null);
if (settings.storePersistentRnd){
settings.persistentRndStorage = JSON.stringify(persistentRnd);
}
updateContentScripts();
}
notice("pass the message to the tabs");
@ -86,54 +85,40 @@
notice("got port", port);
verbose("send back the tab id", port.sender.tab.id);
verbose("send back the persistent random seeds", persistentRnd);
verbose("send back the settings", settings);
port.postMessage({
tabId: port.sender.tab.id,
persistentRnd: persistentRnd,
settings: settings
persistentRnd: persistentRnd
});
var url = new URL(port.sender.url);
port.onMessage.addListener(function(data){
browser.storage.local.get("showNotifications").then(function(data){
if (data.hasOwnProperty("canvasBlocker-notify")){
if (
(
!data.hasOwnProperty("showNotifications") ||
data.showNotifications
) &&
settings.showNotifications &&
!lists.get("ignore").match(url)
){
browser.pageAction.show(port.sender.tab.id);
}
});
}
verbose("got data", data, "from port", port);
});
});
message("register storage change event listener");
browser.storage.onChanged.addListener(function(change, area){
if (area === "local"){
notice("settings changed", change);
notice("update settings object");
Object.keys(change).forEach(function(key){
settings[key] = change[key].newValue;
settings.on("any", updateContentScripts);
settings.on("showNotifications", function({newValue}){
if (!newValue){
message("notifications were disabled -> hide all page actions");
browser.tabs.query({}).then(function(tabs){
tabs.forEach(function(tab){
browser.pageAction.hide(tab.id);
});
});
updateContentScripts();
if (change.hasOwnProperty("showNotifications") && !change.showNotifications.newValue){
message("notifications were disabled -> hide all page actions");
browser.tabs.query({}).then(function(tabs){
tabs.forEach(function(tab){
browser.pageAction.hide(tab.id);
});
});
}
if (change.hasOwnProperty("storePersistentRnd")){
browser.storage.local.set({
persistentRndStorage: change.storePersistentRnd.newValue? JSON.stringify(persistentRnd): ""
});
}
}
});
settings.on("storePersistentRnd", function({newValue}){
settings.persistentRndStorage = newValue? JSON.stringify(persistentRnd): "";
});
// hide page action when a tab is refreshed
browser.tabs.onUpdated.addListener(function(tabId, data){