1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 12:50:36 +01:00

Remove iframe protection from whitelisted pages

Fixes #397
This commit is contained in:
kkapsner 2019-09-11 23:57:00 +02:00
parent 069165e8d6
commit f3d1ca80f5
2 changed files with 42 additions and 1 deletions

View File

@ -12,8 +12,37 @@
scope = require.register("./iframeProtection", {});
}
const settings = require("./settings");
const lists = require("./lists");
function isWhitelisted(url){
return lists.get("white").match(url) || settings.get("blockMode", url).startsWith("allow");
}
scope.protect = function protect(window, wrappedWindow, singleCallback, allCallback){
const changedProperties = [];
// eslint-disable-next-line max-params
function registerChangedProperty(object, name, descriptor, type, original){
changedProperties.push({object, name, descriptor, type, original});
}
if (settings.isStillDefault){
settings.onloaded(function(){
if (isWhitelisted(window.location)){
changedProperties.forEach(function({object, name, descriptor, type, original}){
descriptor[type] = original;
Object.defineProperty(object, name, descriptor);
});
changedProperties.length = 0;
}
});
}
else {
if (isWhitelisted(window.location)){
return;
}
}
["HTMLIFrameElement", "HTMLFrameElement"].forEach(function(constructorName){
const constructor = window[constructorName];
const wrappedConstructor = wrappedWindow[constructorName];
@ -37,6 +66,8 @@
window
);
Object.defineProperty(wrappedConstructor.prototype, "contentWindow", contentWindowDescriptor);
registerChangedProperty(wrappedConstructor.prototype, "contentWindow", contentWindowDescriptor,
"get", originalContentWindowGetter);
const contentDocumentDescriptor = Object.getOwnPropertyDescriptor(
constructor.prototype,
@ -57,6 +88,8 @@
window
);
Object.defineProperty(wrappedConstructor.prototype, "contentDocument", contentDocumentDescriptor);
registerChangedProperty(wrappedConstructor.prototype, "contentDocument", contentDocumentDescriptor,
"get", originalContentDocumentGetter);
});
[
// useless as length could be obtained before the iframe is created and window.frames === window
@ -98,9 +131,11 @@
return value;
})`), window);
Object.defineProperty(object, method, descriptor);
registerChangedProperty(object, method, descriptor, "value", original);
});
protectionDefinition.getters.forEach(function(property){
const descriptor = Object.getOwnPropertyDescriptor(object, property);
const getter = descriptor.get;
const temp = eval(`({
get ${property}(){
const ret = this.${property};
@ -110,6 +145,7 @@
})`);
descriptor.get = exportFunction(Object.getOwnPropertyDescriptor(temp, property).get, window);
Object.defineProperty(object, property, descriptor);
registerChangedProperty(object, property, descriptor, "get", getter);
});
protectionDefinition.setters.forEach(function(property){
const descriptor = Object.getOwnPropertyDescriptor(object, property);
@ -124,6 +160,7 @@
})`);
descriptor.set = exportFunction(Object.getOwnPropertyDescriptor(temp, property).set, window);
Object.defineProperty(object, property, descriptor);
registerChangedProperty(object, property, descriptor, "set", setter);
});
});
@ -179,6 +216,8 @@
}
}, window);
Object.defineProperty(wrappedWindow.HTMLDocument.prototype, "write", documentWriteDescriptor);
registerChangedProperty(wrappedWindow.HTMLDocument.prototype, "write", documentWriteDescriptor,
"value", documentWrite);
const documentWritelnDescriptor = Object.getOwnPropertyDescriptor(
wrappedWindow.HTMLDocument.prototype,
@ -205,5 +244,7 @@
documentWriteln.call(this, "");
}, window);
Object.defineProperty(wrappedWindow.HTMLDocument.prototype, "writeln", documentWritelnDescriptor);
registerChangedProperty(wrappedWindow.HTMLDocument.prototype, "writeln", documentWritelnDescriptor,
"value", documentWriteln);
};
}());

View File

@ -3,7 +3,7 @@ Version 0.5.14:
- reevaluated logging message levels
new features:
-
- remove iframe protection from whitelisted pages
fixes:
- improved option pages on mobile