-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathdisable-newtab-links.js
More file actions
45 lines (42 loc) · 1.12 KB
/
disable-newtab-links.js
File metadata and controls
45 lines (42 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { hit } from '../helpers/index';
/**
* @scriptlet disable-newtab-links
*
* @description
* Prevents opening new tabs and windows if there is `target` attribute in element.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/wiki/Resources-Library#disable-newtab-linksjs-
*
* ### Syntax
*
* ```adblock
* example.org#%#//scriptlet('disable-newtab-links')
* ```
*
* @added v1.0.4.
*/
export function disableNewtabLinks(source) {
document.addEventListener('click', (ev) => {
let { target } = ev;
while (target !== null) {
if (target.localName === 'a' && target.hasAttribute('target')) {
ev.stopPropagation();
ev.preventDefault();
hit(source);
break;
}
target = target.parentNode;
}
});
}
disableNewtabLinks.names = [
'disable-newtab-links',
// aliases are needed for matching the related scriptlet converted into our syntax
'disable-newtab-links.js',
'ubo-disable-newtab-links.js',
'ubo-disable-newtab-links',
];
disableNewtabLinks.injections = [
hit,
];