mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-07-04 20:46:39 +02:00
Added logging lib with setting to control log level.
This commit is contained in:
parent
65ad3a5814
commit
9715eb09d2
13 changed files with 299 additions and 108 deletions
54
lib/main.js
54
lib/main.js
|
@ -5,25 +5,25 @@
|
|||
(function(){
|
||||
"use strict";
|
||||
|
||||
function log(...args){
|
||||
args.unshift("main script:");
|
||||
args.unshift(new Date());
|
||||
console.log.apply(console, args);
|
||||
}
|
||||
const logging = require("./logging");
|
||||
const {error, warning, message, notice, verbose, } = logging;
|
||||
logging.setPrefix("main script");
|
||||
|
||||
log("start");
|
||||
log("loading storage");
|
||||
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){
|
||||
log("everything loaded");
|
||||
notice("everything loaded");
|
||||
const lists = require("./lists");
|
||||
lists.updateAll();
|
||||
|
||||
log("build persistent storage");
|
||||
notice("build persistent storage");
|
||||
var persistentRnd = Object.create(null);
|
||||
try {
|
||||
let storedData = JSON.parse(settings.persistentRndStorage);
|
||||
|
@ -41,8 +41,8 @@
|
|||
catch(e){}
|
||||
|
||||
function updateContentScripts(){
|
||||
log("update content scripts");
|
||||
log("build settings blob");
|
||||
message("update content scripts");
|
||||
notice("build settings blob");
|
||||
var settingsBlob = new Blob(
|
||||
[
|
||||
"var settings = " + JSON.stringify(settings) + ";",
|
||||
|
@ -52,15 +52,15 @@
|
|||
type: "text/javascript"
|
||||
}
|
||||
);
|
||||
log("TODO: register content scripts -> have to wait for the API to be released");
|
||||
warning("TODO: register content scripts -> have to wait for the API to be released");
|
||||
}
|
||||
updateContentScripts();
|
||||
|
||||
log("register non port message listener");
|
||||
message("register non port message listener");
|
||||
browser.runtime.onMessage.addListener(function(data){
|
||||
log("got data without port", data);
|
||||
notice("got data without port", data);
|
||||
if (data["canvasBlocker-new-domain-rnd"]){
|
||||
log("got new domain rnd");
|
||||
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){
|
||||
|
@ -70,7 +70,7 @@
|
|||
});
|
||||
updateContentScripts();
|
||||
}
|
||||
log("pass the message to the tabs");
|
||||
notice("pass the message to the tabs");
|
||||
browser.tabs.query({}).then(function(tabs){
|
||||
tabs.forEach(function(tab){
|
||||
browser.tabs.sendMessage(tab.id, data);
|
||||
|
@ -78,10 +78,10 @@
|
|||
});
|
||||
});
|
||||
|
||||
log("register port listener");
|
||||
message("register port listener");
|
||||
browser.runtime.onConnect.addListener(function(port){
|
||||
log("got port", port);
|
||||
log("send back the tab id", port.sender.tab.id);
|
||||
notice("got port", port);
|
||||
verbose("send back the tab id", port.sender.tab.id);
|
||||
port.postMessage({tabId: port.sender.tab.id});
|
||||
var url = new URL(port.sender.url);
|
||||
port.onMessage.addListener(function(data){
|
||||
|
@ -96,22 +96,22 @@
|
|||
browser.pageAction.show(port.sender.tab.id);
|
||||
}
|
||||
})
|
||||
log("got data", data, "from port", port);
|
||||
verbose("got data", data, "from port", port);
|
||||
});
|
||||
});
|
||||
|
||||
log("register storage change event listener");
|
||||
message("register storage change event listener");
|
||||
browser.storage.onChanged.addListener(function(change, area){
|
||||
if (area === "local"){
|
||||
log("settings changed", change);
|
||||
log("update settings object");
|
||||
notice("settings changed", change);
|
||||
notice("update settings object");
|
||||
Object.keys(change).forEach(function(key){
|
||||
settings[key] = change[key].newValue;
|
||||
});
|
||||
updateContentScripts();
|
||||
|
||||
if (change.hasOwnProperty("showNotifications") && !change.showNotifications.newValue){
|
||||
log("notifications were disabled -> hide all page actions");
|
||||
message("notifications were disabled -> hide all page actions");
|
||||
browser.tabs.query({}).then(function(tabs){
|
||||
tabs.forEach(function(tab){
|
||||
browser.pageAction.hide(tab.id);
|
||||
|
@ -134,7 +134,7 @@
|
|||
});
|
||||
});
|
||||
|
||||
// log("TODO: register unload events - do not know how - there seems to be no way with a WebExtension");
|
||||
// warning("TODO: register unload events - do not know how - there seems to be no way with a WebExtension");
|
||||
// old code
|
||||
// const {when: unload} = require("sdk/system/unload");
|
||||
// unload(function(){
|
||||
|
@ -142,7 +142,7 @@
|
|||
// });
|
||||
|
||||
browser.runtime.onInstalled.addListener(function(){
|
||||
log("CanvasBlocker installed");
|
||||
message("CanvasBlocker installed");
|
||||
browser.storage.local.get("storageVersion").then(function(data){
|
||||
if (data.storageVersion !== 0.1){
|
||||
browser.storage.local.set({
|
||||
|
@ -152,5 +152,5 @@
|
|||
});
|
||||
});
|
||||
|
||||
log("end");
|
||||
message("end");
|
||||
}());
|
Loading…
Add table
Add a link
Reference in a new issue