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

Revert to correct values when whitelisted

Fixes #397
This commit is contained in:
kkapsner 2019-09-13 00:49:34 +02:00
parent f3d1ca80f5
commit 506f062c07
2 changed files with 79 additions and 64 deletions

View File

@ -21,6 +21,13 @@
scope.protect = function protect(window, wrappedWindow, singleCallback, allCallback){ scope.protect = function protect(window, wrappedWindow, singleCallback, allCallback){
function changeProperty(object, name, type, changed){
const descriptor = Object.getOwnPropertyDescriptor(object, name);
const original = descriptor[type];
descriptor[type] = changed;
Object.defineProperty(object, name, descriptor);
registerChangedProperty(object, name, descriptor, type, original);
}
const changedProperties = []; const changedProperties = [];
// eslint-disable-next-line max-params // eslint-disable-next-line max-params
function registerChangedProperty(object, name, descriptor, type, original){ function registerChangedProperty(object, name, descriptor, type, original){
@ -61,13 +68,10 @@
return window; return window;
} }
}; };
contentWindowDescriptor.get = exportFunction( changeProperty(wrappedConstructor.prototype, "contentWindow", "get", exportFunction(
Object.getOwnPropertyDescriptor(contentWindowTemp, "contentWindow").get, Object.getOwnPropertyDescriptor(contentWindowTemp, "contentWindow").get,
window window
); ));
Object.defineProperty(wrappedConstructor.prototype, "contentWindow", contentWindowDescriptor);
registerChangedProperty(wrappedConstructor.prototype, "contentWindow", contentWindowDescriptor,
"get", originalContentWindowGetter);
const contentDocumentDescriptor = Object.getOwnPropertyDescriptor( const contentDocumentDescriptor = Object.getOwnPropertyDescriptor(
constructor.prototype, constructor.prototype,
@ -83,13 +87,10 @@
return document; return document;
} }
}; };
contentDocumentDescriptor.get = exportFunction( changeProperty(wrappedConstructor.prototype, "contentDocument", "get", exportFunction(
Object.getOwnPropertyDescriptor(contentDocumentTemp, "contentDocument").get, Object.getOwnPropertyDescriptor(contentDocumentTemp, "contentDocument").get,
window 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 // useless as length could be obtained before the iframe is created and window.frames === window
@ -123,19 +124,15 @@
protectionDefinition.methods.forEach(function(method){ protectionDefinition.methods.forEach(function(method){
const descriptor = Object.getOwnPropertyDescriptor(object, method); const descriptor = Object.getOwnPropertyDescriptor(object, method);
const original = descriptor.value; const original = descriptor.value;
descriptor.value = exportFunction(eval(`(function ${method}(){ changeProperty(object, method, "value", exportFunction(eval(`(function ${method}(){
const value = arguments.length? const value = arguments.length?
original.apply(this, window.Array.from(arguments)): original.apply(this, window.Array.from(arguments)):
original.call(this); original.call(this);
allCallback(); allCallback();
return value; return value;
})`), window); })`), window));
Object.defineProperty(object, method, descriptor);
registerChangedProperty(object, method, descriptor, "value", original);
}); });
protectionDefinition.getters.forEach(function(property){ protectionDefinition.getters.forEach(function(property){
const descriptor = Object.getOwnPropertyDescriptor(object, property);
const getter = descriptor.get;
const temp = eval(`({ const temp = eval(`({
get ${property}(){ get ${property}(){
const ret = this.${property}; const ret = this.${property};
@ -143,9 +140,10 @@
return ret; return ret;
} }
})`); })`);
descriptor.get = exportFunction(Object.getOwnPropertyDescriptor(temp, property).get, window); changeProperty(object, property, "get", exportFunction(
Object.defineProperty(object, property, descriptor); Object.getOwnPropertyDescriptor(temp, property).get,
registerChangedProperty(object, property, descriptor, "get", getter); window
));
}); });
protectionDefinition.setters.forEach(function(property){ protectionDefinition.setters.forEach(function(property){
const descriptor = Object.getOwnPropertyDescriptor(object, property); const descriptor = Object.getOwnPropertyDescriptor(object, property);
@ -158,9 +156,9 @@
return ret; return ret;
} }
})`); })`);
descriptor.set = exportFunction(Object.getOwnPropertyDescriptor(temp, property).set, window); changeProperty(object, property, "set", exportFunction(
Object.defineProperty(object, property, descriptor); Object.getOwnPropertyDescriptor(temp, property).set, window
registerChangedProperty(object, property, descriptor, "set", setter); ));
}); });
}); });
@ -188,63 +186,72 @@
}(); }();
// MutationObserver does not trigger fast enough when document.write is used // MutationObserver does not trigger fast enough when document.write is used
const documentWriteDescriptor = Object.getOwnPropertyDescriptor( const documentWriteDescriptorOnHTMLDocument = Object.getOwnPropertyDescriptor(
wrappedWindow.HTMLDocument.prototype, wrappedWindow.HTMLDocument.prototype,
"write" "write"
) || Object.getOwnPropertyDescriptor( );
const documentWriteDescriptor = documentWriteDescriptorOnHTMLDocument || Object.getOwnPropertyDescriptor(
wrappedWindow.Document.prototype, wrappedWindow.Document.prototype,
"write" "write"
); );
const documentWrite = documentWriteDescriptor.value; const documentWrite = documentWriteDescriptor.value;
documentWriteDescriptor.value = exportFunction(function write(markup){ changeProperty(
for (let i = 0, l = arguments.length; i < l; i += 1){ documentWriteDescriptorOnHTMLDocument?
const str = "" + arguments[i]; wrappedWindow.HTMLDocument.prototype:
// weird problem with waterfox and google docs wrappedWindow.Document.prototype,
const parts = ( "write", "value", exportFunction(function write(markup){
str.match(/^\s*<!doctype/i) && for (let i = 0, l = arguments.length; i < l; i += 1){
!str.match(/frame/i) const str = "" + arguments[i];
)? [str]: str.split(/(?=<)/); // weird problem with waterfox and google docs
const length = parts.length; const parts = (
const scripts = window.document.getElementsByTagName("script"); str.match(/^\s*<!doctype/i) &&
for (let i = 0; i < length; i += 1){ !str.match(/frame/i)
documentWrite.call(this, parts[i]); )? [str]: str.split(/(?=<)/);
allCallback(); const length = parts.length;
if (scripts.length && scripts[scripts.length - 1].src){ const scripts = window.document.getElementsByTagName("script");
observe(); for (let i = 0; i < length; i += 1){
documentWrite.call(this, parts[i]);
allCallback();
if (scripts.length && scripts[scripts.length - 1].src){
observe();
}
} }
} }
} }, window)
}, window); );
Object.defineProperty(wrappedWindow.HTMLDocument.prototype, "write", documentWriteDescriptor);
registerChangedProperty(wrappedWindow.HTMLDocument.prototype, "write", documentWriteDescriptor,
"value", documentWrite);
const documentWritelnDescriptor = Object.getOwnPropertyDescriptor( const documentWritelnDescriptorOnHTMLDocument = Object.getOwnPropertyDescriptor(
wrappedWindow.HTMLDocument.prototype, wrappedWindow.HTMLDocument.prototype,
"writeln" "writeln"
) || Object.getOwnPropertyDescriptor( );
const documentWritelnDescriptor = documentWritelnDescriptorOnHTMLDocument || Object.getOwnPropertyDescriptor(
wrappedWindow.Document.prototype, wrappedWindow.Document.prototype,
"writeln" "writeln"
); );
const documentWriteln = documentWritelnDescriptor.value; const documentWriteln = documentWritelnDescriptor.value;
documentWritelnDescriptor.value = exportFunction(function writeln(markup){ changeProperty(
for (let i = 0, l = arguments.length; i < l; i += 1){ documentWritelnDescriptorOnHTMLDocument?
const str = "" + arguments[i]; wrappedWindow.HTMLDocument.prototype:
const parts = str.split(/(?=<)/); wrappedWindow.Document.prototype,
const length = parts.length; "writeln", "value", exportFunction(
const scripts = window.document.getElementsByTagName("script"); function writeln(markup){
for (let i = 0; i < length; i += 1){ for (let i = 0, l = arguments.length; i < l; i += 1){
documentWrite.call(this, parts[i]); const str = "" + arguments[i];
allCallback(); const parts = str.split(/(?=<)/);
if (scripts.length && scripts[scripts.length - 1].src){ const length = parts.length;
observe(); const scripts = window.document.getElementsByTagName("script");
for (let i = 0; i < length; i += 1){
documentWrite.call(this, parts[i]);
allCallback();
if (scripts.length && scripts[scripts.length - 1].src){
observe();
}
}
} }
} documentWriteln.call(this, "");
} },
documentWriteln.call(this, ""); window
}, window); )
Object.defineProperty(wrappedWindow.HTMLDocument.prototype, "writeln", documentWritelnDescriptor); );
registerChangedProperty(wrappedWindow.HTMLDocument.prototype, "writeln", documentWritelnDescriptor,
"value", documentWriteln);
}; };
}()); }());

View File

@ -61,6 +61,14 @@
{ {
"version": "0.5.14Alpha20190821", "version": "0.5.14Alpha20190821",
"update_link": "https://canvasblocker.kkapsner.de/versions/canvasblocker_beta-0.5.14Alpha20190821-an+fx.xpi" "update_link": "https://canvasblocker.kkapsner.de/versions/canvasblocker_beta-0.5.14Alpha20190821-an+fx.xpi"
},
{
"version": "0.5.14Alpha20190911",
"update_link": "https://canvasblocker.kkapsner.de/versions/canvasblocker_beta-0.5.14Alpha20190911-an+fx.xpi"
},
{
"version": "0.5.14Alpha20190912",
"update_link": "https://canvasblocker.kkapsner.de/versions/canvasblocker_beta-0.5.14Alpha20190912-an+fx.xpi"
} }
] ]
} }