mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2024-12-22 21:00:23 +01:00
157 lines
4.1 KiB
JavaScript
157 lines
4.1 KiB
JavaScript
/* 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";
|
|
|
|
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;
|
|
|
|
message("starting", location.href);
|
|
|
|
function check(message){
|
|
if (enabled){
|
|
return originalCheck(message);
|
|
}
|
|
else {
|
|
return {type: [], mode: "allow"};
|
|
}
|
|
}
|
|
function checkStack(stack){
|
|
if (enabled){
|
|
return originalCheckStack(stack);
|
|
}
|
|
else {
|
|
return true;
|
|
}
|
|
}
|
|
const _ = require("sdk/l10n").get;
|
|
function askWrapper(data){
|
|
return ask(data, {
|
|
_,
|
|
prefs
|
|
});
|
|
}
|
|
|
|
message("open port to background script");
|
|
var port = browser.runtime.connect();
|
|
var tabId;
|
|
port.onMessage.addListener(function(data){
|
|
if (data.hasOwnProperty("tabId")){
|
|
notice("my tab id is", data.tabId);
|
|
tabId = data.tabId;
|
|
}
|
|
});
|
|
var notifications = [];
|
|
function notify(data){
|
|
notifications.push(data);
|
|
port.postMessage({"canvasBlocker-notify": data});
|
|
}
|
|
|
|
const preferences = require("sdk/simple-prefs");
|
|
function prefs(name){
|
|
return preferences.prefs[name];
|
|
}
|
|
|
|
|
|
var interceptedWindows = new WeakMap();
|
|
function interceptWindow(window){
|
|
if (!enabled || interceptedWindows.get(window)){
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
var href = window.location.href;
|
|
}
|
|
catch (e){
|
|
// we are unable to read the location due to SOP
|
|
// therefore we also can not intercept anything.
|
|
warning("NOT intercepting window du to SOP", window);
|
|
return false;
|
|
}
|
|
|
|
message("intercepting window", window);
|
|
intercept(
|
|
{subject: window},
|
|
{check, checkStack, ask: askWrapper, notify, prefs}
|
|
);
|
|
message("prepare to intercept (i)frames.");
|
|
|
|
[window.HTMLIFrameElement, window.HTMLFrameElement].forEach(function(constructor){
|
|
var oldContentWindowGetter = constructor.prototype.__lookupGetter__("contentWindow");
|
|
Object.defineProperty(
|
|
constructor.prototype.wrappedJSObject,
|
|
"contentWindow",
|
|
{
|
|
enumerable: true,
|
|
configureable: true,
|
|
get: exportFunction(function(){
|
|
var window = oldContentWindowGetter.call(this);
|
|
interceptWindow(window);
|
|
return window;
|
|
}, window)
|
|
}
|
|
);
|
|
var oldContentDocumentGetter = constructor.prototype.__lookupGetter__("contentDocument");
|
|
Object.defineProperty(
|
|
constructor.prototype.wrappedJSObject,
|
|
"contentDocument",
|
|
{
|
|
enumerable: true,
|
|
configureable: true,
|
|
get: exportFunction(function(){
|
|
var document = oldContentDocumentGetter.call(this);
|
|
interceptWindow(document.defaultView);
|
|
return document;
|
|
}, window)
|
|
}
|
|
);
|
|
});
|
|
|
|
interceptedWindows.set(window, true);
|
|
return true;
|
|
};
|
|
|
|
interceptWindow(window);
|
|
|
|
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){
|
|
notice("sending notifications:", notifications);
|
|
browser.runtime.sendMessage({
|
|
sender: tabId,
|
|
"canvasBlocker-notifications": notifications
|
|
});
|
|
notice("notifications sent");
|
|
}
|
|
});
|
|
|
|
message("load settings");
|
|
browser.storage.local.get().then(function(data){
|
|
Object.keys(data).forEach(function(key){
|
|
notice("loaded setting:", key, ":", data[key]);
|
|
settings[key] = data[key];
|
|
});
|
|
});
|
|
|
|
message("register listener for settings changes");
|
|
browser.storage.onChanged.addListener(function(change, area){
|
|
if (area === "local"){
|
|
Object.keys(change).forEach(function(key){
|
|
notice("setting changed:", key, ":", change[key].newValue);
|
|
settings[key] = change[key].newValue;
|
|
});
|
|
}
|
|
});
|
|
}()); |