From d544543ab580930733c4def8817fbff251ad10ce Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Mon, 17 Aug 2020 09:47:40 -0400 Subject: [PATCH] Add argument to nowoif scriptlet When a 3rd argument was provided, the scriplet would log information related to window popup operations, regardless of the value of the argument. The 3rd argument is now parsed in a formal manner. It is meant to be one or more space-separated tokens which are used to fine tune the behavior of the scriptlet. Tokens: log: Cause the scriptlet to log information regarding how window.open() is used by the page on which the scriptlet is used. Useful only to filter creators. obj: Use an `object` element instead of `iframe` element (default) as a decoy to be used in place of a popup window, when the page requires a valid `window` instance to be returned. --- .../window.open-defuser.js | 34 +++++++++++-------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/src/web_accessible_resources/window.open-defuser.js b/src/web_accessible_resources/window.open-defuser.js index b89542a80..54027d76a 100644 --- a/src/web_accessible_resources/window.open-defuser.js +++ b/src/web_accessible_resources/window.open-defuser.js @@ -27,7 +27,7 @@ if ( arg2 === '{{2}}' ) { arg2 = ''; } let arg3 = '{{3}}'; if ( arg3 === '{{3}}' ) { arg3 = ''; } - const log = arg3 !== '' + const log = /\blog\b/.test(arg3) ? console.log.bind(console) : ( ) => { }; const newSyntax = /^[01]?$/.test(arg1) === false; @@ -77,21 +77,25 @@ return target.apply(thisArg, args); } if ( autoRemoveAfter < 0 ) { return null; } - const decoy1 = createDecoy('iframe', 'src', url); - const decoy2 = createDecoy('object', 'data', url); - const popup = decoy1.contentWindow || decoy2.contentWindow; + const decoy = /\bobj\b/.test(arg3) + ? createDecoy('object', 'data', url) + : createDecoy('iframe', 'src', url); + let popup = decoy.contentWindow; Object.defineProperty(popup, 'closed', { value: false }); - if ( arg3 === '' ) { return popup; } - return new Proxy(popup, { - get: function(target, prop) { - log('window.open / get', prop, '===', target[prop]); - return target[prop]; - }, - set: function(target, prop, value) { - log('window.open / set', prop, '=', value); - target[prop] = value; - }, - }); + if ( /\blog\b/.test(arg3) ) { + popup = new Proxy(popup, { + get: function(target, prop) { + log('window.open / get', prop, '===', target[prop]); + if ( prop === 'closed' ) { return false; } + return target[prop]; + }, + set: function(target, prop, value) { + log('window.open / set', prop, '=', value); + target[prop] = value; + }, + }); + } + return popup; } }); })();