1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2025-05-19 19:02:21 +02:00

Protect reopened documents.

This commit is contained in:
kkapsner 2019-05-24 18:30:57 +02:00
parent b6e5384945
commit c8c311a82f

View File

@ -114,34 +114,61 @@
}); });
}); });
// MutationObserver to intercept iframes while generating the DOM. // MutationObserver to intercept iFrames while generating the DOM.
const observe = function(){
var observer = new MutationObserver(allCallback); var observer = new MutationObserver(allCallback);
var observing = false;
function observe(){
if (!observing){
observer.observe(window.document.documentElement, {subtree: true, childList: true}); observer.observe(window.document.documentElement, {subtree: true, childList: true});
observing = true;
}
}
observe();
window.document.addEventListener("DOMContentLoaded", function(){ window.document.addEventListener("DOMContentLoaded", function(){
if (observing){
observer.disconnect(); observer.disconnect();
observing = false;
}
}); });
return observe;
}();
// 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(wrappedWindow.HTMLDocument.prototype, "write"); const documentWriteDescriptor = Object.getOwnPropertyDescriptor(
wrappedWindow.HTMLDocument.prototype,
"write"
);
const documentWrite = documentWriteDescriptor.value; const documentWrite = documentWriteDescriptor.value;
documentWriteDescriptor.value = exportFunction(function write(str){ documentWriteDescriptor.value = exportFunction(function write(str){
const parts = str.split(/(?=>)/); const parts = str.split(/(?=<)/);
const length = parts.length; const length = parts.length;
const scripts = window.document.getElementsByTagName("script");
for (let i = 0; i < length; i += 1){ for (let i = 0; i < length; i += 1){
documentWrite.call(this, parts[i]); documentWrite.call(this, parts[i]);
allCallback(); allCallback();
if (scripts.length && scripts[scripts.length - 1].src){
observe();
}
} }
}, window); }, window);
Object.defineProperty(wrappedWindow.HTMLDocument.prototype, "write", documentWriteDescriptor); Object.defineProperty(wrappedWindow.HTMLDocument.prototype, "write", documentWriteDescriptor);
const documentWritelnDescriptor = Object.getOwnPropertyDescriptor(wrappedWindow.HTMLDocument.prototype, "writeln"); const documentWritelnDescriptor = Object.getOwnPropertyDescriptor(
wrappedWindow.HTMLDocument.prototype,
"writeln"
);
const documentWriteln = documentWritelnDescriptor.value; const documentWriteln = documentWritelnDescriptor.value;
documentWritelnDescriptor.value = exportFunction(function writeln(str){ documentWritelnDescriptor.value = exportFunction(function writeln(str){
const parts = str.split(/(?=>)/); const parts = str.split(/(?=<)/);
const length = parts.length - 1; const length = parts.length - 1;
const scripts = window.document.getElementsByTagName("script");
for (let i = 0; i < length; i += 1){ for (let i = 0; i < length; i += 1){
documentWrite.call(this, parts[i]); documentWrite.call(this, parts[i]);
allCallback(); allCallback();
if (scripts.length && scripts[scripts.length - 1].src){
observe();
}
} }
documentWriteln.call(this, parts[length]); documentWriteln.call(this, parts[length]);
}, window); }, window);