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

Big linting

This commit is contained in:
kkapsner 2019-11-28 01:26:35 +01:00
parent b5e6d049ce
commit aef6bd3d59
58 changed files with 2074 additions and 1856 deletions

View file

@ -19,8 +19,7 @@
return lists.get("white").match(url) || settings.get("blockMode", url).startsWith("allow");
}
scope.protect = function protect(window, wrappedWindow, singleCallback, allCallback){
function createChangeProperty(window){
function changeProperty(object, name, type, changed){
const descriptor = Object.getOwnPropertyDescriptor(object, name);
const original = descriptor[type];
@ -49,7 +48,10 @@
return;
}
}
return changeProperty;
}
function protectFrameProperties({window, wrappedWindow, changeProperty, singleCallback}){
["HTMLIFrameElement", "HTMLFrameElement"].forEach(function(constructorName){
const constructor = window[constructorName];
const wrappedConstructor = wrappedWindow[constructorName];
@ -61,7 +63,7 @@
const originalContentWindowGetter = contentWindowDescriptor.get;
const contentWindowTemp = {
get contentWindow(){
var window = originalContentWindowGetter.call(this);
const window = originalContentWindowGetter.call(this);
if (window){
singleCallback(window);
}
@ -80,7 +82,7 @@
const originalContentDocumentGetter = contentDocumentDescriptor.get;
const contentDocumentTemp = {
get contentDocument(){
var document = originalContentDocumentGetter.call(this);
const document = originalContentDocumentGetter.call(this);
if (document){
singleCallback(document.defaultView);
}
@ -92,6 +94,9 @@
window
));
});
}
function protectDOMModifications({window, wrappedWindow, changeProperty, allCallback}){
[
// useless as length could be obtained before the iframe is created and window.frames === window
// {
@ -161,31 +166,31 @@
));
});
});
// MutationObserver to intercept iFrames while generating the DOM.
const observe = function(){
var observer = new MutationObserver(allCallback);
var observing = false;
function observe(){
if (
!observing &&
window.document
){
observer.observe(window.document, {subtree: true, childList: true});
observing = true;
}
}
function enableMutationObserver({window, allCallback}){
const observer = new MutationObserver(allCallback);
let observing = false;
function observe(){
if (
!observing &&
window.document
){
observer.observe(window.document, {subtree: true, childList: true});
observing = true;
}
observe();
window.document.addEventListener("DOMContentLoaded", function(){
if (observing){
observer.disconnect();
observing = false;
}
});
return observe;
}();
// MutationObserver does not trigger fast enough when document.write is used
}
observe();
window.document.addEventListener("DOMContentLoaded", function(){
if (observing){
observer.disconnect();
observing = false;
}
});
return observe;
}
function protectDocumentWrite({window, wrappedWindow, changeProperty, observe, allCallback}){
const documentWriteDescriptorOnHTMLDocument = Object.getOwnPropertyDescriptor(
wrappedWindow.HTMLDocument.prototype,
"write"
@ -199,6 +204,7 @@
documentWriteDescriptorOnHTMLDocument?
wrappedWindow.HTMLDocument.prototype:
wrappedWindow.Document.prototype,
// eslint-disable-next-line no-unused-vars
"write", "value", exportFunction(function write(markup){
for (let i = 0, l = arguments.length; i < l; i += 1){
const str = "" + arguments[i];
@ -234,6 +240,7 @@
wrappedWindow.HTMLDocument.prototype:
wrappedWindow.Document.prototype,
"writeln", "value", exportFunction(
// eslint-disable-next-line no-unused-vars
function writeln(markup){
for (let i = 0, l = arguments.length; i < l; i += 1){
const str = "" + arguments[i];
@ -253,5 +260,21 @@
window
)
);
}
scope.protect = function protect(window, wrappedWindow, singleCallback, allCallback){
const changeProperty = createChangeProperty(window);
const api = {window, wrappedWindow, changeProperty, singleCallback, allCallback};
protectFrameProperties(api);
protectDOMModifications(api);
// MutationObserver to intercept iFrames while generating the DOM.
api.observe = enableMutationObserver(api);
// MutationObserver does not trigger fast enough when document.write is used
protectDocumentWrite(api);
};
}());