1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-11 07:38:58 +01:00
CanvasBlocker/lib/intercept.js

275 lines
7.4 KiB
JavaScript
Raw Normal View History

2016-02-13 12:28:36 +01:00
/* 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 {
window.scope.intercept = {};
scope = window.scope.intercept;
}
2016-08-06 19:17:36 +02:00
const {changedFunctions, setRandomSupply} = require("./modifiedAPI");
const randomSupplies = require("./randomSupplies");
const getWrapped = require("sdk/getWrapped");
2017-11-07 00:36:44 +01:00
const logging = require("./logging");
const settings = require("./settings");
2016-08-06 19:17:36 +02:00
setRandomSupply(randomSupplies.nonPersistent);
2016-02-13 12:28:36 +01:00
var apiNames = Object.keys(changedFunctions);
var undef;
2016-08-06 19:17:36 +02:00
function setRandomSupplyByType(type){
switch (type){
case "persistent":
setRandomSupply(randomSupplies.persistent);
break;
2017-08-07 17:43:57 +02:00
case "constant":
setRandomSupply(randomSupplies.constant);
break;
2017-11-24 17:06:43 +01:00
case "white":
setRandomSupply(randomSupplies.white);
break;
2016-08-06 19:17:36 +02:00
default:
setRandomSupply(randomSupplies.nonPersistent);
}
}
scope.setRandomSupplyByType = setRandomSupplyByType;
function getURL(window){
var href;
try {
href = window.location.href;
}
catch (e){
// unable to read location due to SOP
// since we are not able to do anything in that case we can allow everything
return "about:SOP";
}
if (!href || href === "about:blank"){
if (window !== window.parent){
return getURL(window.parent);
}
else if (window.opener){
return getURL(window.opener);
}
}
return window.location.href;
}
scope.preIntercept = function preIntercept({subject: window}, apis){
settings.forceLoad();
if (!settings.isStillDefault){
scope.intercept({subject: window}, apis);
}
else {
2017-12-15 01:00:52 +01:00
let forceLoad = true;
let preIntercepted = false;
let intercepted = false;
const forEachFunction = function(callback){
apiNames.forEach(function(name){
const changedFunction = changedFunctions[name];
(
Array.isArray(changedFunction.object)?
changedFunction.object:
[changedFunction.object]
).forEach(function(object){
var constructor = getWrapped(window)[object];
if (constructor){
callback({name, changedFunction, constructor});
}
});
});
};
let originalPropertyDescriptors = {};
const doPreIntercept = function(){
if (!preIntercepted){
forEachFunction(function({name, constructor}){
var map = originalPropertyDescriptors[name] || new WeakMap();
originalPropertyDescriptors[name] = map;
map.set(constructor, Object.getOwnPropertyDescriptor(constructor.prototype, name));
Object.defineProperty(
constructor.prototype,
name,
{
enumerable: true,
configureable: true,
get: exportFunction(function(){
if (forceLoad){
undoPreIntercept();
settings.forceLoad();
doRealIntercept();
var descriptor = Object.getOwnPropertyDescriptor(constructor.prototype, name);
return descriptor.value || descriptor.get();
}
else {
var url = getURL(window);
if (!url){
return undef;
}
var error = new Error();
apis.notify({
url,
errorStack: error.stack,
messageId: "preBlock",
timestamp: new Date(),
functionName: name,
dataURL: false
});
return;
}
}, window),
set: exportFunction(function(){}, window)
}
);
});
preIntercepted = true;
}
};
const undoPreIntercept = function(){
if (preIntercepted){
preIntercepted = false;
forEachFunction(function({name, constructor}){
Object.defineProperty(
constructor.prototype,
name,
originalPropertyDescriptors[name].get(constructor)
);
});
}
};
const doRealIntercept = function(){
if (!intercepted){
scope.intercept({subject: window}, apis);
intercepted = true;
}
};
logging.verbose("pre intercept until settings are loaded");
doPreIntercept();
settings.onloaded(function(){
undoPreIntercept();
doRealIntercept();
});
}
};
scope.intercept = function intercept({subject: window}, {check, checkStack, ask, notify, prefs}){
var siteStatus = check({url: getURL(window)});
2017-11-07 00:36:44 +01:00
logging.verbose("status for page", window, siteStatus);
if (siteStatus.mode !== "allow"){
apiNames.forEach(function(name){
var changedFunction = changedFunctions[name];
var functionStatus = changedFunction.getStatus(undefined, siteStatus);
logging.verbose("status for", name, ":", functionStatus);
if (functionStatus.active){
2017-10-05 19:00:00 +02:00
(
Array.isArray(changedFunction.object)?
changedFunction.object:
[changedFunction.object]
).forEach(function(object){
var constructor = getWrapped(window)[object];
if (constructor){
var original = constructor.prototype[name];
Object.defineProperty(
constructor.prototype,
name,
{
enumerable: true,
configureable: true,
2017-06-25 22:33:12 +02:00
get: exportFunction(function(){
var url = getURL(window);
if (!url){
return undef;
}
var error = new Error();
if (checkStack(error.stack)){
return original;
}
var funcStatus = changedFunction.getStatus(this, siteStatus);
function notifyCallback(messageId){
notify({
url,
errorStack: error.stack,
messageId,
timestamp: new Date(),
functionName: name,
dataURL:
prefs("storeImageForInspection") &&
prefs("showNotifications")?
(
this instanceof HTMLCanvasElement?
this.toDataURL():
(
this.canvas instanceof HTMLCanvasElement?
this.canvas.toDataURL():
false
)
):
false
});
}
if (funcStatus.active){
if (funcStatus.mode === "ask"){
2017-10-05 19:00:00 +02:00
funcStatus.mode = ask({
window: window,
type: changedFunction.type,
canvas: this,
errorStack: error.stack
});
}
switch (funcStatus.mode){
case "allow":
return original;
case "fake":
setRandomSupplyByType(prefs("rng"));
var fake = changedFunction.fakeGenerator(
prefs,
notifyCallback,
window,
original
);
switch (fake){
case true:
return original;
case false:
return undef;
default:
return exportFunction(fake, getWrapped(window));
}
//case "block":
default:
return undef;
}
}
else {
return original;
}
}, window),
set: exportFunction(function(value){
Object.defineProperty(
constructor.prototype,
name,
{
value,
writable: true,
configurable: true,
enumerable: true
}
);
}, window)
}
);
}
});
}
});
}
2016-02-13 12:48:22 +01:00
};
2016-02-13 12:28:36 +01:00
}());