1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-06-01 02:48:06 +02:00
CanvasBlocker/lib/frame.js

184 lines
5.0 KiB
JavaScript
Raw Normal View History

/* 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";
2017-11-07 00:36:44 +01:00
const settings = require("./settings");
const {intercept} = require("./intercept.js");
const {ask} = require("./askForPermission.js");
const lists = require("./lists.js");
const {check: originalCheck, checkStack: originalCheckStack} = require("./check.js");
const getWrapped = require("sdk/getWrapped");
const logging = require("./logging");
const {error, warning, message, notice, verbose, setPrefix: setLogPrefix} = 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;
}
}
function askWrapper(data){
return ask(data, {
_: browser.i18n.getMessage,
prefs
});
}
2017-07-16 00:25:19 +02:00
message("open port to background script");
var port = browser.runtime.connect();
if (window === window.top){
message("Is top level window -> tab had navigation -> clear page action");
port.postMessage({"canvasBlocker-clear-page-action": true});
}
var tabId;
port.onMessage.addListener(function(data){
2017-11-07 00:36:44 +01:00
message("Got data from port", data);
if (data.hasOwnProperty("tabId")){
notice("my tab id is", data.tabId);
tabId = data.tabId;
}
if (data.hasOwnProperty("persistentRnd")){
notice("got persistent random data", data.persistentRnd);
const {persistent: persistentRnd} = require("./randomSupplies.js");
Object.keys(data.persistentRnd).forEach(function(domain){
verbose("random data for", domain, data.persistentRnd[domain]);
persistentRnd.setDomainRnd(domain, data.persistentRnd[domain]);
});
}
if (settings.isStillDefault && data.hasOwnProperty("settings")){
notice("got settings from background script");
Object.keys(data.settings).forEach(function(key){
settings[key] = data.settings[key];
});
settings.isStillDefault = false;
}
});
var notifications = [];
function notify(data){
notifications.push(data);
port.postMessage({"canvasBlocker-notify": data});
}
function prefs(...args){
return settings.get(...args);
}
2017-06-25 22:33:12 +02:00
var interceptedWindows = new WeakMap();
function interceptWindow(window){
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 due to SOP", window);
return false;
}
2017-10-10 21:11:50 +02:00
if (!enabled || interceptedWindows.get(getWrapped(window))){
return false;
}
message("intercepting window", window);
2017-06-25 22:33:12 +02:00
intercept(
{subject: window},
{check, checkStack, ask: askWrapper, notify, prefs}
);
message("prepare to intercept (i)frames.");
2017-06-25 22:33:12 +02:00
[window.HTMLIFrameElement, window.HTMLFrameElement].forEach(function(constructor){
var oldContentWindowGetter = constructor.prototype.__lookupGetter__("contentWindow");
Object.defineProperty(
getWrapped(constructor.prototype),
2017-06-25 22:33:12 +02:00
"contentWindow",
{
enumerable: true,
configureable: true,
get: exportFunction(function(){
var window = oldContentWindowGetter.call(this);
2017-10-10 21:11:50 +02:00
if (window){
interceptWindow(window);
}
2017-06-25 22:33:12 +02:00
return window;
}, window)
}
);
var oldContentDocumentGetter = constructor.prototype.__lookupGetter__("contentDocument");
Object.defineProperty(
getWrapped(constructor.prototype),
2017-06-25 22:33:12 +02:00
"contentDocument",
{
enumerable: true,
configureable: true,
get: exportFunction(function(){
var document = oldContentDocumentGetter.call(this);
2017-10-10 21:11:50 +02:00
if (document){
interceptWindow(document.defaultView);
}
2017-06-25 22:33:12 +02:00
return document;
}, window)
}
);
});
2017-06-25 22:33:12 +02:00
2017-10-10 21:11:50 +02:00
interceptedWindows.set(getWrapped(window), true);
2017-06-25 22:33:12 +02:00
return true;
2017-10-03 15:35:31 +02:00
}
2017-06-25 22:33:12 +02:00
message("register listener for messages from background script");
2017-06-25 22:33:12 +02:00
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");
}
});
// need to wait for the settings to arrive!
while (settings.isStillDefault){
logging.message("Starting synchronous request to wait for settings.");
try {
let xhr = new XMLHttpRequest();
xhr.open("GET", "https://[::]", false);
xhr.send();
xhr = null;
}
catch (e){
logging.verbose("Error in XHR:", e);
}
logging.message("settings still default?", settings.isStillDefault);
}
settings.onloaded(function(){
interceptWindow(window);
});
}());