-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathdisable-newtab-links.js
48 lines (44 loc) · 1.23 KB
/
disable-newtab-links.js
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
46
47
48
import { hit } from '../helpers';
/**
* @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;
}
});
}
export const disableNewtabLinksNames = [
'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',
];
// eslint-disable-next-line prefer-destructuring
disableNewtabLinks.primaryName = disableNewtabLinksNames[0];
disableNewtabLinks.injections = [
hit,
];