1
0
Fork 0
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:
kkapsner 2017-07-27 19:14:04 +02:00
parent 65ad3a5814
commit 9715eb09d2
13 changed files with 299 additions and 108 deletions

View file

@ -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;
});
}