Skip to content

Commit 85877b1

Browse files
committed
Improve prevent-window-open scriptlet
Support triggering a `debugger` statement when `window.open()` is called. Related feedback: uBlockOrigin/uAssets#25510 (comment)
1 parent 58bfe4c commit 85877b1

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

assets/resources/scriptlets.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,13 +2952,45 @@ function preventXhr(...args) {
29522952
return preventXhrFn(false, ...args);
29532953
}
29542954

2955-
/******************************************************************************/
2955+
/**
2956+
* @scriptlet prevent-window-open
2957+
*
2958+
* @description
2959+
* Prevent a webpage from opening new tabs through `window.open()`.
2960+
*
2961+
* @param pattern
2962+
* A plain string or regex to match against the `url` argument for the
2963+
* prevention to be triggered. If not provided, all calls to `window.open()`
2964+
* are prevented.
2965+
* If set to the special value `debug` *and* the logger is opened, the scriptlet
2966+
* will trigger a `debugger` statement and the prevention will not occur.
2967+
*
2968+
* @param [delay]
2969+
* If provided, a decoy will be created or opened, and this parameter states
2970+
* the number of seconds to wait for before the decoy is terminated, i.e.
2971+
* either removed from the DOM or closed.
2972+
*
2973+
* @param [decoy]
2974+
* A string representing the type of decoy to use:
2975+
* - `blank`: replace the `url` parameter with `about:blank`
2976+
* - `object`: create and append an `object` element to the DOM, and return
2977+
* its `contentWindow` property.
2978+
* - `frame`: create and append an `iframe` element to the DOM, and return
2979+
* its `contentWindow` property.
2980+
*
2981+
* @example
2982+
* ##+js(prevent-window-open, ads.example.com/)
2983+
*
2984+
* @example
2985+
* ##+js(prevent-window-open, ads.example.com/, 1, iframe)
2986+
*
2987+
* */
29562988

29572989
builtinScriptlets.push({
2958-
name: 'no-window-open-if.js',
2990+
name: 'prevent-window-open.js',
29592991
aliases: [
29602992
'nowoif.js',
2961-
'prevent-window-open.js',
2993+
'no-window-open-if.js',
29622994
'window.open-defuser.js',
29632995
],
29642996
fn: noWindowOpenIf,
@@ -2994,6 +3026,10 @@ function noWindowOpenIf(
29943026
};
29953027
const noopFunc = function(){};
29963028
proxyApplyFn('open', function open(context) {
3029+
if ( pattern === 'debug' && safe.logLevel !== 0 ) {
3030+
debugger; // eslint-disable-line no-debugger
3031+
return context.reflect();
3032+
}
29973033
const { callArgs } = context;
29983034
const haystack = callArgs.join(' ');
29993035
if ( rePattern.test(haystack) !== targetMatchResult ) {

0 commit comments

Comments
 (0)