diff --git a/lib/iframeProtection.js b/lib/iframeProtection.js index 56da871..9064386 100644 --- a/lib/iframeProtection.js +++ b/lib/iframeProtection.js @@ -21,6 +21,13 @@ 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 = []; // eslint-disable-next-line max-params function registerChangedProperty(object, name, descriptor, type, original){ @@ -61,13 +68,10 @@ return window; } }; - contentWindowDescriptor.get = exportFunction( + changeProperty(wrappedConstructor.prototype, "contentWindow", "get", exportFunction( Object.getOwnPropertyDescriptor(contentWindowTemp, "contentWindow").get, window - ); - Object.defineProperty(wrappedConstructor.prototype, "contentWindow", contentWindowDescriptor); - registerChangedProperty(wrappedConstructor.prototype, "contentWindow", contentWindowDescriptor, - "get", originalContentWindowGetter); + )); const contentDocumentDescriptor = Object.getOwnPropertyDescriptor( constructor.prototype, @@ -83,13 +87,10 @@ return document; } }; - contentDocumentDescriptor.get = exportFunction( + changeProperty(wrappedConstructor.prototype, "contentDocument", "get", exportFunction( Object.getOwnPropertyDescriptor(contentDocumentTemp, "contentDocument").get, 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 @@ -123,19 +124,15 @@ protectionDefinition.methods.forEach(function(method){ const descriptor = Object.getOwnPropertyDescriptor(object, method); const original = descriptor.value; - descriptor.value = exportFunction(eval(`(function ${method}(){ + changeProperty(object, method, "value", exportFunction(eval(`(function ${method}(){ const value = arguments.length? original.apply(this, window.Array.from(arguments)): original.call(this); allCallback(); return value; - })`), window); - Object.defineProperty(object, method, descriptor); - registerChangedProperty(object, method, descriptor, "value", original); + })`), window)); }); protectionDefinition.getters.forEach(function(property){ - const descriptor = Object.getOwnPropertyDescriptor(object, property); - const getter = descriptor.get; const temp = eval(`({ get ${property}(){ const ret = this.${property}; @@ -143,9 +140,10 @@ return ret; } })`); - descriptor.get = exportFunction(Object.getOwnPropertyDescriptor(temp, property).get, window); - Object.defineProperty(object, property, descriptor); - registerChangedProperty(object, property, descriptor, "get", getter); + changeProperty(object, property, "get", exportFunction( + Object.getOwnPropertyDescriptor(temp, property).get, + window + )); }); protectionDefinition.setters.forEach(function(property){ const descriptor = Object.getOwnPropertyDescriptor(object, property); @@ -158,9 +156,9 @@ return ret; } })`); - descriptor.set = exportFunction(Object.getOwnPropertyDescriptor(temp, property).set, window); - Object.defineProperty(object, property, descriptor); - registerChangedProperty(object, property, descriptor, "set", setter); + changeProperty(object, property, "set", exportFunction( + Object.getOwnPropertyDescriptor(temp, property).set, window + )); }); }); @@ -188,63 +186,72 @@ }(); // MutationObserver does not trigger fast enough when document.write is used - const documentWriteDescriptor = Object.getOwnPropertyDescriptor( + const documentWriteDescriptorOnHTMLDocument = Object.getOwnPropertyDescriptor( wrappedWindow.HTMLDocument.prototype, "write" - ) || Object.getOwnPropertyDescriptor( + ); + const documentWriteDescriptor = documentWriteDescriptorOnHTMLDocument || Object.getOwnPropertyDescriptor( wrappedWindow.Document.prototype, "write" ); const documentWrite = documentWriteDescriptor.value; - documentWriteDescriptor.value = exportFunction(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 - const parts = ( - str.match(/^\s*