mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-07-04 12:36:37 +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
|
@ -20,6 +20,7 @@
|
|||
const prefs = preferences.prefs;
|
||||
const {parseErrorStack} = require("./callingStack");
|
||||
const {URL} = require("sdk/url");
|
||||
const logging = require("./logging");
|
||||
|
||||
scope.check = function check({url, errorStack}){
|
||||
var match = checkBoth(errorStack, url, prefs.blockMode).match(/^(block|allow|fake|ask)(|Readout|Everything|Context|Input|Internal)$/);
|
||||
|
@ -50,14 +51,18 @@
|
|||
}
|
||||
|
||||
function checkURL(url, blockMode){
|
||||
logging.message("check url %s for block mode %s", url, blockMode);
|
||||
url = new URL(url || "about:blank");
|
||||
switch (url.protocol){
|
||||
case "about:":
|
||||
if (url.href === "about:blank"){
|
||||
logging.message("use regular mode on about:blank");
|
||||
break;
|
||||
}
|
||||
logging.message("allow internal URLs");
|
||||
return "allowInternal";
|
||||
case "chrome:":
|
||||
logging.message("allow internal URLs");
|
||||
return "allowInternal";
|
||||
}
|
||||
|
||||
|
@ -96,7 +101,7 @@
|
|||
mode = "allow";
|
||||
break;
|
||||
default:
|
||||
// console.log("Unknown blocking mode (" + blockMode + "). Default to block everything.");
|
||||
logging.warning("Unknown blocking mode (" + blockMode + "). Default to block everything.");
|
||||
}
|
||||
return mode;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
"use strict";
|
||||
|
||||
var settings = {
|
||||
logLevel: 100,
|
||||
whiteList: "",
|
||||
blackList: "",
|
||||
blockMode: "fakeReadout",
|
||||
|
@ -17,5 +18,6 @@ var settings = {
|
|||
showCallingFile: false,
|
||||
showCompleteCallingStack: false,
|
||||
enableStackList: false,
|
||||
stackList: ""
|
||||
stackList: "",
|
||||
isStillDefault: true
|
||||
};
|
47
lib/frame.js
47
lib/frame.js
|
@ -4,33 +4,18 @@
|
|||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
(function(){
|
||||
"use strict";
|
||||
function log(...args){
|
||||
function leftPad(str, char, pad){
|
||||
str = "" + str;
|
||||
return char.repeat(pad - str.length) + str;
|
||||
}
|
||||
args.unshift("frame script:");
|
||||
var now = new Date();
|
||||
args.unshift(
|
||||
now.getFullYear() + "-" +
|
||||
leftPad(now.getMonth() + 1, "0", 2) + "-" +
|
||||
leftPad(now.getDate(), "0", 2) + " " +
|
||||
leftPad(now.getHours(), "0", 2) + ":" +
|
||||
leftPad(now.getMinutes(), "0", 2) + ":" +
|
||||
leftPad(now.getSeconds(), "0", 2) + "." +
|
||||
leftPad(now.getMilliseconds(), "0", 3)
|
||||
);
|
||||
console.log.apply(console, args);
|
||||
}
|
||||
|
||||
const {intercept} = require("./intercept.js");
|
||||
const {ask} = require("./askForPermission.js");
|
||||
const {check: originalCheck, checkStack: originalCheckStack} = require("./check.js");
|
||||
|
||||
const {error, warning, message, notice, verbose, setPrefix: setLogPrefix} = require("./logging");
|
||||
setLogPrefix("frame script");
|
||||
|
||||
// Variable to "unload" the script
|
||||
var enabled = true;
|
||||
|
||||
log("starting", location.href);
|
||||
message("starting", location.href);
|
||||
|
||||
function check(message){
|
||||
if (enabled){
|
||||
|
@ -56,12 +41,12 @@
|
|||
});
|
||||
}
|
||||
|
||||
log("open port to background script");
|
||||
message("open port to background script");
|
||||
var port = browser.runtime.connect();
|
||||
var tabId;
|
||||
port.onMessage.addListener(function(data){
|
||||
if (data.hasOwnProperty("tabId")){
|
||||
log("my tab id is", data.tabId);
|
||||
notice("my tab id is", data.tabId);
|
||||
tabId = data.tabId;
|
||||
}
|
||||
});
|
||||
|
@ -89,16 +74,16 @@
|
|||
catch (e){
|
||||
// we are unable to read the location due to SOP
|
||||
// therefore we also can not intercept anything.
|
||||
log("NOT intercepting window du to SOP", window);
|
||||
warning("NOT intercepting window du to SOP", window);
|
||||
return false;
|
||||
}
|
||||
|
||||
log("intercepting window", window);
|
||||
message("intercepting window", window);
|
||||
intercept(
|
||||
{subject: window},
|
||||
{check, checkStack, ask: askWrapper, notify, prefs}
|
||||
);
|
||||
log("prepare to intercept (i)frames.");
|
||||
message("prepare to intercept (i)frames.");
|
||||
|
||||
[window.HTMLIFrameElement, window.HTMLFrameElement].forEach(function(constructor){
|
||||
var oldContentWindowGetter = constructor.prototype.__lookupGetter__("contentWindow");
|
||||
|
@ -137,34 +122,34 @@
|
|||
|
||||
interceptWindow(window);
|
||||
|
||||
log("register listener for messages from background script");
|
||||
message("register listener for messages from background script");
|
||||
browser.runtime.onMessage.addListener(function(data){
|
||||
if (data["canvasBlocker-unload"]){
|
||||
enabled = false;
|
||||
}
|
||||
if (data.hasOwnProperty("canvasBlocker-sendNotifications") && data["canvasBlocker-sendNotifications"] === tabId){
|
||||
log("sending notifications:", notifications);
|
||||
notice("sending notifications:", notifications);
|
||||
browser.runtime.sendMessage({
|
||||
sender: tabId,
|
||||
"canvasBlocker-notifications": notifications
|
||||
});
|
||||
log("notifications sent");
|
||||
notice("notifications sent");
|
||||
}
|
||||
});
|
||||
|
||||
log("load settings");
|
||||
message("load settings");
|
||||
browser.storage.local.get().then(function(data){
|
||||
Object.keys(data).forEach(function(key){
|
||||
log("loaded setting:", key, ":", data[key]);
|
||||
notice("loaded setting:", key, ":", data[key]);
|
||||
settings[key] = data[key];
|
||||
});
|
||||
});
|
||||
|
||||
log("register listener for settings changes");
|
||||
message("register listener for settings changes");
|
||||
browser.storage.onChanged.addListener(function(change, area){
|
||||
if (area === "local"){
|
||||
Object.keys(change).forEach(function(key){
|
||||
log("setting changed:", key, ":", change[key].newValue);
|
||||
notice("setting changed:", key, ":", change[key].newValue);
|
||||
settings[key] = change[key].newValue;
|
||||
});
|
||||
}
|
||||
|
|
88
lib/logging.js
Normal file
88
lib/logging.js
Normal file
|
@ -0,0 +1,88 @@
|
|||
/* jslint moz: true */
|
||||
/* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
||||
(function(){
|
||||
"use strict";
|
||||
|
||||
var scope;
|
||||
if ((typeof exports) !== "undefined"){
|
||||
scope = exports;
|
||||
}
|
||||
else if (window.scope){
|
||||
window.scope.logging = {};
|
||||
scope = window.scope.logging;
|
||||
}
|
||||
else {
|
||||
window.logging = {};
|
||||
scope = window.logging;
|
||||
}
|
||||
|
||||
var prefix = "";
|
||||
|
||||
function leftPad(str, char, pad){
|
||||
str = "" + str;
|
||||
return char.repeat(pad - str.length) + str;
|
||||
}
|
||||
|
||||
var queue = [];
|
||||
function performLog(level, args, date){
|
||||
if (!date){
|
||||
date = new Date();
|
||||
}
|
||||
if (settings.isStillDefault){
|
||||
queue.push({level, args, date});
|
||||
}
|
||||
else {
|
||||
if (settings.logLevel >= level){
|
||||
if (prefix){
|
||||
args.unshift(prefix + ":");
|
||||
}
|
||||
args.unshift(
|
||||
"[" +
|
||||
date.getFullYear() + "-" +
|
||||
leftPad(date.getMonth() + 1, "0", 2) + "-" +
|
||||
leftPad(date.getDate(), "0", 2) + " " +
|
||||
leftPad(date.getHours(), "0", 2) + ":" +
|
||||
leftPad(date.getMinutes(), "0", 2) + ":" +
|
||||
leftPad(date.getSeconds(), "0", 2) + "." +
|
||||
leftPad(date.getMilliseconds(), "0", 3) +
|
||||
"]"
|
||||
);
|
||||
console.log.apply(console, args);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function error (...args){performLog( 1, args);}
|
||||
function warning(...args){performLog( 25, args);}
|
||||
function message(...args){performLog( 50, args);}
|
||||
function notice (...args){performLog( 75, args);}
|
||||
function verbose(...args){performLog(100, args);}
|
||||
function metaLog(...args){performLog(999, args);}
|
||||
|
||||
scope.setPrefix = function(newPrefix){
|
||||
if (!prefix){
|
||||
prefix = newPrefix;
|
||||
}
|
||||
else {
|
||||
warning("logging prefix already set (%s) cannot be set to %s", prefix, newPrefix);
|
||||
}
|
||||
};
|
||||
scope.clearQueue = function(){
|
||||
if (queue.length){
|
||||
metaLog("clear logging queue");
|
||||
var tmp = queue;
|
||||
queue = [];
|
||||
tmp.forEach(function(item){
|
||||
performLog(item.level, item.args, item.date);
|
||||
});
|
||||
metaLog("logging queue cleared");
|
||||
}
|
||||
};
|
||||
scope.error = error;
|
||||
scope.warning = warning;
|
||||
scope.message = message;
|
||||
scope.notice = notice;
|
||||
scope.verbose = verbose;
|
||||
}());
|
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