Skip to content

Commit 60f5759

Browse files
committed
Add prevent-navigation scriptlet
Related issue: AdguardTeam/Scriptlets#532
1 parent 018feea commit 60f5759

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*******************************************************************************
2+
3+
uBlock Origin - a comprehensive, efficient content blocker
4+
Copyright (C) 2026-present Raymond Hill
5+
6+
This program is free software: you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation, either version 3 of the License, or
9+
(at your option) any later version.
10+
11+
This program is distributed in the hope that it will be useful,
12+
but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
GNU General Public License for more details.
15+
16+
You should have received a copy of the GNU General Public License
17+
along with this program. If not, see {http://www.gnu.org/licenses/}.
18+
19+
Home: https://github.com/gorhill/uBlock
20+
21+
*/
22+
23+
import { registerScriptlet } from './base.js';
24+
import { safeSelf } from './safe-self.js';
25+
26+
/**
27+
* @scriptlet prevent-navigation
28+
*
29+
* @description
30+
* Conditionally abort navigation events.
31+
*
32+
* @param [pattern]
33+
* Optional. A pattern to match against the assigned value. The pattern can be
34+
* a plain string, or a regex. Prepend with `!` to reverse the match condition.
35+
* No pattern
36+
*
37+
* Reference:
38+
* https://github.com/AdguardTeam/Scriptlets/commit/cd2d8eefd5
39+
* */
40+
41+
export function preventNavigation(
42+
pattern = ''
43+
) {
44+
const safe = safeSelf();
45+
const logPrefix = safe.makeLogPrefix('prevent-navigation', pattern);
46+
const needle = pattern === 'location.href' ? self.location.href : pattern;
47+
const matcher = safe.initPattern(needle, { canNegate: true });
48+
self.navigation.addEventListener('navigate', ev => {
49+
if ( ev.userInitiated ) { return; }
50+
const { url } = ev.destination;
51+
if ( pattern === '' ) {
52+
safe.uboLog(logPrefix, `Navigation to ${url}`);
53+
return;
54+
}
55+
if ( safe.testPattern(matcher, url) ) {
56+
ev.preventDefault();
57+
safe.uboLog(logPrefix, `Prevented navigation to ${url}`);
58+
}
59+
});
60+
}
61+
registerScriptlet(preventNavigation, {
62+
name: 'prevent-navigation.js',
63+
dependencies: [
64+
safeSelf,
65+
],
66+
world: 'ISOLATED',
67+
});

src/js/resources/scriptlets.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import './prevent-addeventlistener.js';
3131
import './prevent-dialog.js';
3232
import './prevent-fetch.js';
3333
import './prevent-innerHTML.js';
34+
import './prevent-navigation.js';
3435
import './prevent-settimeout.js';
3536
import './prevent-xhr.js';
3637
import './replace-argument.js';

0 commit comments

Comments
 (0)