Skip to content

Commit 0af817d

Browse files
committed
AG-50540 Improve prevent-window-open — match '_blank'. #549
Squashed commit of the following: commit ebf2e31 Merge: 2385c99 1d852ea Author: Adam Wróblewski <adam@adguard.com> Date: Tue Mar 3 08:52:49 2026 +0100 Merge branch 'master' into feature/AG-50540 commit 2385c99 Author: Adam Wróblewski <adam@adguard.com> Date: Fri Feb 27 14:29:04 2026 +0100 Fix changelog commit c9a9af4 Author: Adam Wróblewski <adam@adguard.com> Date: Fri Feb 27 14:23:18 2026 +0100 Add combineArgs helper commit 268a376 Author: Adam Wróblewski <adam@adguard.com> Date: Fri Feb 27 14:12:55 2026 +0100 Update `prevent-window-open` to check all parameters
1 parent 1d852ea commit 0af817d

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
1818
across `abort-on-property-read`, `abort-on-property-write`, `abort-on-stack-trace`,
1919
`abort-current-inline-script`, `debug-on-property-write`, `debug-on-property-read`,
2020
`debug-current-inline-script` and `log-on-stack-trace` scriptlets [#513].
21+
- `prevent-window-open` now checks all parameters [#549].
2122

2223
### Fixed
2324

@@ -30,6 +31,7 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
3031
[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v2.2.16...HEAD
3132
[#513]: https://github.com/AdguardTeam/Scriptlets/issues/513
3233
[#545]: https://github.com/AdguardTeam/Scriptlets/issues/545
34+
[#549]: https://github.com/AdguardTeam/Scriptlets/issues/549
3335

3436
## [v2.2.16] - 2026-02-19
3537

src/scriptlets/prevent-window-open.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import {
2222
* @scriptlet prevent-window-open
2323
*
2424
* @description
25-
* Prevents `window.open` calls when URL either matches or not matches the specified string/regexp.
25+
* Prevents `window.open` calls when URL or any other parameter either matches or not matches the specified string/regexp.
2626
* Using it without parameters prevents all `window.open` calls.
2727
*
2828
* Related UBO scriptlet:
@@ -102,6 +102,20 @@ export function preventWindowOpen(source, match = '*', delay, replacement) {
102102
const nativeOpen = window.open;
103103
const isNewSyntax = match !== '0' && match !== '1';
104104

105+
/**
106+
* Combine `url` and additional `args` into a single string for matching.
107+
*
108+
* @param {string} url - The URL or first argument passed to `window.open`.
109+
* @param {Array<string>|undefined} args - Additional arguments to include.
110+
* @returns {string} The concatenated string used for matching.
111+
*/
112+
const combineArgs = (url, args) => {
113+
if (args && args.length > 0) {
114+
return `${url} ${args.join(' ')}`;
115+
}
116+
return url;
117+
};
118+
105119
const oldOpenWrapper = (str, ...args) => {
106120
match = Number(match) > 0;
107121
// 'delay' was 'search' prop for matching in old syntax
@@ -110,7 +124,11 @@ export function preventWindowOpen(source, match = '*', delay, replacement) {
110124
return nativeOpen.apply(window, [str, ...args]);
111125
}
112126
const searchRegexp = toRegExp(delay);
113-
if (match !== searchRegexp.test(str)) {
127+
128+
// Check all arguments of window.open, not only the first one
129+
// https://github.com/AdguardTeam/Scriptlets/issues/549
130+
const argsToCheck = combineArgs(str, args);
131+
if (match !== searchRegexp.test(argsToCheck)) {
114132
return nativeOpen.apply(window, [str, ...args]);
115133
}
116134
hit(source);
@@ -133,7 +151,10 @@ export function preventWindowOpen(source, match = '*', delay, replacement) {
133151
shouldPrevent = true;
134152
} else if (isValidMatchStr(match)) {
135153
const { isInvertedMatch, matchRegexp } = parseMatchArg(match);
136-
shouldPrevent = matchRegexp.test(url) !== isInvertedMatch;
154+
// Check all arguments of window.open, not only the first one
155+
// https://github.com/AdguardTeam/Scriptlets/issues/549
156+
const argsToCheck = combineArgs(url, args);
157+
shouldPrevent = matchRegexp.test(argsToCheck) !== isInvertedMatch;
137158
} else {
138159
logMessage(source, `Invalid parameter: ${match}`);
139160
shouldPrevent = false;

tests/scriptlets/prevent-window-open.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ test('old syntax: does not work - invalid regexp pattern', (assert) => {
9696
assert.ok(window[CHECK_PROP], 'window.open has been executed');
9797
});
9898

99+
test('old syntax: target parameter ', (assert) => {
100+
const scriptletArgs = ['1', '_blank'];
101+
runScriptlet(name, scriptletArgs);
102+
window.open('test url', '_blank');
103+
assert.equal(window.hit, 'value', 'Hit function was executed');
104+
});
105+
99106
test('new syntax: no args', (assert) => {
100107
runScriptlet(name);
101108
window.open('some url');
@@ -252,3 +259,10 @@ test('new syntax: invalid regexp', (assert) => {
252259
window.open('test url', 'some target');
253260
assert.equal(window.hit, undefined, 'Hit function was executed');
254261
});
262+
263+
test('new syntax: target parameter ', (assert) => {
264+
const scriptletArgs = ['_blank'];
265+
runScriptlet(name, scriptletArgs);
266+
window.open('foo bar baz', '_blank');
267+
assert.equal(window.hit, 'value', 'Hit function was executed');
268+
});

0 commit comments

Comments
 (0)