Skip to content

Commit e753384

Browse files
committed
AG-40699 Fix 'disable-newtab-links' — not preventing ad popups. #483
Squashed commit of the following: commit 2381b4d Merge: d45c599 6a878bf Author: Adam Wróblewski <adam@adguard.com> Date: Fri Mar 20 17:43:35 2026 +0100 Merge branch 'master' into fix/AG-40699 commit d45c599 Author: Adam Wróblewski <adam@adguard.com> Date: Thu Mar 19 11:17:34 2026 +0100 Simplify condition commit 4026bc2 Author: Adam Wróblewski <adam@adguard.com> Date: Thu Mar 19 11:09:43 2026 +0100 Refactor disable-newtab-links scriptlet: migrate to TypeScript and fix click handling
1 parent 6a878bf commit e753384

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ The format is based on [Keep a Changelog], and this project adheres to [Semantic
1010
<!-- TODO: change `@added unknown` tag due to the actual version -->
1111
<!-- during new scriptlets or redirects releasing -->
1212

13+
## [Unreleased]
14+
15+
### Fixed
16+
17+
- `disable-newtab-links` not preventing clicks when the element has its own `click` handler
18+
added via `addEventListener` [#483].
19+
20+
[Unreleased]: https://github.com/AdguardTeam/Scriptlets/compare/v2.3.0...HEAD
21+
[#483]: https://github.com/AdguardTeam/Scriptlets/issues/483
22+
1323
## [v2.3.0] - 2026-03-18
1424

1525
### Added
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { hit } from '../helpers';
2+
import { type Source } from './scriptlets';
23

34
/**
45
* @scriptlet disable-newtab-links
@@ -17,10 +18,10 @@ import { hit } from '../helpers';
1718
*
1819
* @added v1.0.4.
1920
*/
20-
export function disableNewtabLinks(source) {
21+
export function disableNewtabLinks(source: Source) {
2122
document.addEventListener('click', (ev) => {
2223
let { target } = ev;
23-
while (target !== null) {
24+
while (target instanceof Element) {
2425
if (target.localName === 'a' && target.hasAttribute('target')) {
2526
ev.stopPropagation();
2627
ev.preventDefault();
@@ -29,7 +30,7 @@ export function disableNewtabLinks(source) {
2930
}
3031
target = target.parentNode;
3132
}
32-
});
33+
}, { capture: true });
3334
}
3435

3536
export const disableNewtabLinksNames = [

tests/scriptlets/disable-newtab-links.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,21 @@ test('adg works', (assert) => {
5454
done();
5555
});
5656
});
57+
58+
test('addEventListener click should not fire', (assert) => {
59+
let called = false;
60+
runScriptlet(name);
61+
62+
const elem = createLink();
63+
elem.addEventListener('click', () => {
64+
called = true;
65+
});
66+
elem.click();
67+
68+
const done = assert.async();
69+
setTimeout(() => {
70+
assert.strictEqual(called, false, 'click handler not called');
71+
assert.strictEqual(window.hit, 'FIRED', 'hit fired');
72+
done();
73+
});
74+
});

0 commit comments

Comments
 (0)