From 6fb7622feca500f922d1336269ff601b13ed381e Mon Sep 17 00:00:00 2001 From: kkapsner Date: Fri, 13 Dec 2019 17:34:14 +0100 Subject: [PATCH] Always use exportFunctionWithName --- lib/frame.js | 4 +-- lib/iframeProtection.js | 77 +++++++++++++++++++---------------------- lib/intercept.js | 6 ++-- test/detectionTest.js | 4 +++ 4 files changed, 44 insertions(+), 47 deletions(-) diff --git a/lib/frame.js b/lib/frame.js index 5f610d9..ea7e13c 100644 --- a/lib/frame.js +++ b/lib/frame.js @@ -169,7 +169,7 @@ const matchMediaDescriptor = Object.getOwnPropertyDescriptor(wrappedWindow, "matchMedia"); const originalMatchMedia = matchMediaDescriptor.value; - matchMediaDescriptor.value = exportFunction(function matchMedia(query){ + matchMediaDescriptor.value = extension.exportFunctionWithName(function matchMedia(query){ if (query === extensionSecret[0]){ return extensionSecret[1]; } @@ -178,7 +178,7 @@ originalMatchMedia.apply(this, wrappedWindow.Array.from(arguments)): originalMatchMedia.call(this, query); } - }, window); + }, window, originalMatchMedia.name); Object.defineProperty(wrappedWindow, "matchMedia", matchMediaDescriptor); interceptedWindows.set(wrappedWindow, true); diff --git a/lib/iframeProtection.js b/lib/iframeProtection.js index d4187e6..eb20215 100644 --- a/lib/iframeProtection.js +++ b/lib/iframeProtection.js @@ -24,6 +24,9 @@ function changeProperty(object, name, type, changed){ const descriptor = Object.getOwnPropertyDescriptor(object, name); const original = descriptor[type]; + if ((typeof changed) === "function"){ + changed = extension.exportFunctionWithName(changed, window, original.name); + } descriptor[type] = changed; Object.defineProperty(object, name, descriptor); registerChangedProperty(object, name, descriptor, type, original); @@ -71,10 +74,9 @@ return window; } }; - changeProperty(wrappedConstructor.prototype, "contentWindow", "get", exportFunction( - Object.getOwnPropertyDescriptor(contentWindowTemp, "contentWindow").get, - window - )); + changeProperty(wrappedConstructor.prototype, "contentWindow", "get", + Object.getOwnPropertyDescriptor(contentWindowTemp, "contentWindow").get + ); const contentDocumentDescriptor = Object.getOwnPropertyDescriptor( constructor.prototype, @@ -90,10 +92,9 @@ return document; } }; - changeProperty(wrappedConstructor.prototype, "contentDocument", "get", exportFunction( - Object.getOwnPropertyDescriptor(contentDocumentTemp, "contentDocument").get, - window - )); + changeProperty(wrappedConstructor.prototype, "contentDocument", "get", + Object.getOwnPropertyDescriptor(contentDocumentTemp, "contentDocument").get + ); }); } @@ -130,16 +131,15 @@ protectionDefinition.methods.forEach(function(method){ const descriptor = Object.getOwnPropertyDescriptor(object, method); const original = descriptor.value; - changeProperty(object, method, "value", extension.exportFunctionWithName(function method(){ + changeProperty(object, method, "value", function method(){ const value = arguments.length? original.apply(this, window.Array.from(arguments)): original.call(this); allCallback(); return value; - }, window, method)); + }); }); protectionDefinition.getters.forEach(function(property){ - const descriptor = Object.getOwnPropertyDescriptor(object, property); const temp = { get [property](){ const ret = this[property]; @@ -147,11 +147,9 @@ return ret; } }; - changeProperty(object, property, "get", extension.exportFunctionWithName( - Object.getOwnPropertyDescriptor(temp, property).get, - window, - descriptor.get - )); + changeProperty(object, property, "get", + Object.getOwnPropertyDescriptor(temp, property).get + ); }); protectionDefinition.setters.forEach(function(property){ const descriptor = Object.getOwnPropertyDescriptor(object, property); @@ -164,9 +162,9 @@ return ret; } }; - changeProperty(object, property, "set", extension.exportFunctionWithName( - Object.getOwnPropertyDescriptor(temp, property).set, window, setter.name - )); + changeProperty(object, property, "set", + Object.getOwnPropertyDescriptor(temp, property).set + ); }); }); } @@ -207,8 +205,7 @@ documentWriteDescriptorOnHTMLDocument? wrappedWindow.HTMLDocument.prototype: wrappedWindow.Document.prototype, - // eslint-disable-next-line no-unused-vars - "write", "value", exportFunction(function write(markup){ + "write", "value", function write(markup){ for (let i = 0, l = arguments.length; i < l; i += 1){ const str = "" + arguments[i]; // weird problem with waterfox and google docs @@ -226,7 +223,7 @@ } } } - }, window) + } ); const documentWritelnDescriptorOnHTMLDocument = Object.getOwnPropertyDescriptor( @@ -242,26 +239,22 @@ documentWritelnDescriptorOnHTMLDocument? 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]; - const parts = str.split(/(?=<)/); - const length = parts.length; - 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(); - } + "writeln", "value", function writeln(markup){ + for (let i = 0, l = arguments.length; i < l; i += 1){ + const str = "" + arguments[i]; + const parts = str.split(/(?=<)/); + const length = parts.length; + 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, ""); - }, - window - ) + } + documentWriteln.call(this, ""); + } ); } @@ -277,7 +270,7 @@ ).get; changeProperty( wrappedWindow, - "open", "value", exportFunction(function open(){ + "open", "value", function open(){ const newWindow = arguments.length? windowOpen.apply(this, window.Array.from(arguments)): windowOpen.call(this); @@ -287,7 +280,7 @@ singleCallback(getDocument.call(newWindow).defaultView); } return newWindow; - }, window) + } ); } diff --git a/lib/intercept.js b/lib/intercept.js index ee614ca..8746f03 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -149,7 +149,7 @@ { enumerable: true, configurable: true, - get: exportFunction(function(){ + get: extension.exportFunctionWithName(function(){ if (forceLoad){ logging.warning("force load the settings. Calling stack:", (new Error()).stack); undoPreIntercept(); @@ -175,8 +175,8 @@ }); return undef; } - }, windowToProcess), - set: exportFunction(function(){}, windowToProcess) + }, windowToProcess, `get ${name}`), + set: extension.exportFunctionWithName(function(){}, windowToProcess, `set ${name}`) } ); }); diff --git a/test/detectionTest.js b/test/detectionTest.js index 25b29ea..a337696 100644 --- a/test/detectionTest.js +++ b/test/detectionTest.js @@ -203,6 +203,10 @@ addTest("function name", function(log){ func: HTMLIFrameElement.prototype.__lookupGetter__("contentDocument"), expectedName: "get contentDocument" }, + { + func: HTMLIFrameElement.prototype.__lookupGetter__("contentWindow"), + expectedName: "get contentWindow" + }, ].map(checkName).some(function(b){return b;}); }); addTest("exposed getters or setters", function(log){