1
0
Fork 0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-07-04 04:26:35 +02:00

Hide function replacement

Fixes #206
This commit is contained in:
kkapsner 2018-07-13 16:58:13 +02:00
parent 9711c67c3f
commit 26529a3653
6 changed files with 375 additions and 281 deletions

View file

@ -158,9 +158,107 @@
});
}
};
let extensionID = browser.extension.getURL("");
scope.intercept = function intercept({subject: window}, {check, checkStack, ask, notify, prefs}){
function getDataURL(object, prefs){
return (
this &&
prefs("storeImageForInspection") &&
prefs("showNotifications")?
(
this instanceof HTMLCanvasElement?
this.toDataURL():
(
this.canvas instanceof HTMLCanvasElement?
this.canvas.toDataURL():
false
)
):
false
);
}
function generateChecker(name, changedFunction, siteStatus, original){
return function checker(callingDepth = 2){
var url = getURL(window);
if (!url){
return undef;
}
var error = new Error();
try {
// return original if the extension itself requested the function
if (
error.stack
.split("\n", callingDepth + 2)[callingDepth + 1]
.split("@", callingDepth + 1)[1]
.startsWith(extensionID)
){
return {allow: true, original, window};
}
}
catch (e) {
// stack had an unknown form
}
if (checkStack(error.stack)){
return {allow: true, original, window};
}
var funcStatus = changedFunction.getStatus(this, siteStatus);
function notifyCallback(messageId){
notify({
url,
errorStack: error.stack,
messageId,
timestamp: new Date(),
functionName: name,
api: changedFunction.api,
dataURL: getDataURL(this, prefs)
});
}
if (funcStatus.active && !prefs("apiWhiteList")[name]){
if (funcStatus.mode === "ask"){
funcStatus.mode = ask({
window: window,
type: changedFunction.type,
api: changedFunction.api,
canvas: this instanceof HTMLCanvasElement?
this:
(
this &&
(this.canvas instanceof HTMLCanvasElement)?
this.canvas:
false
),
errorStack: error.stack
});
}
switch (funcStatus.mode){
case "allow":
return {allow: true, original, window};
case "fake":
setRandomSupplyByType(prefs("rng"));
return {
allow: "fake",
prefs,
notify: notifyCallback,
window,
original
};
//case "block":
default:
return {
allow: false,
notify: notifyCallback
};
}
}
else {
return {allow: true, original, window};
}
};
}
var siteStatus = check({url: getURL(window)});
logging.verbose("status for page", window, siteStatus);
if (siteStatus.mode !== "allow"){
@ -177,117 +275,25 @@
var constructor = getWrapped(window)[object];
if (constructor){
var original = constructor.prototype[name];
Object.defineProperty(
constructor.prototype,
name,
{
enumerable: true,
configureable: true,
get: exportFunction(function(){
var url = getURL(window);
if (!url){
return undef;
}
var error = new Error();
try {
// return original if the extension itself requested the function
if (error.stack.split("\n", 3)[1].split("@", 2)[1].startsWith(extensionID)){
return original;
}
}
catch (e) {
// stack had an unknown form
}
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,
api: changedFunction.api,
dataURL:
this &&
prefs("storeImageForInspection") &&
prefs("showNotifications")?
(
this instanceof HTMLCanvasElement?
this.toDataURL():
(
this.canvas instanceof HTMLCanvasElement?
this.canvas.toDataURL():
false
)
):
false
});
}
if (funcStatus.active && !prefs("apiWhiteList")[name]){
if (funcStatus.mode === "ask"){
funcStatus.mode = ask({
window: window,
type: changedFunction.type,
api: changedFunction.api,
canvas: this instanceof HTMLCanvasElement?
this:
(
this &&
(this.canvas instanceof HTMLCanvasElement)?
this.canvas:
false
),
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)
const checker = generateChecker(name, changedFunction, siteStatus, original);
var descriptor = Object.getOwnPropertyDescriptor(constructor.prototype, name);
if (descriptor.hasOwnProperty("value")){
if (changedFunction.fakeGenerator){
descriptor.value = exportFunction(
changedFunction.fakeGenerator(checker),
window
);
}
);
else {
descriptor.value = null;
}
}
else {
descriptor.get = exportFunction(function(){
return changedFunction.fakeGenerator(checker);
}, window);
}
Object.defineProperty(constructor.prototype, name, descriptor);
}
});
}