-
Notifications
You must be signed in to change notification settings - Fork 3
/
keepintab.js
33 lines (29 loc) · 881 Bytes
/
keepintab.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
function removeTargetIfNotFrame(element)
{
let currentTarget = element.getAttribute("target");
if (currentTarget && (currentTarget[0] === '_')) {
element.removeAttribute("target");
}
}
function removeTargetAttributes(parent)
{
Array.from(parent.querySelectorAll("a[target]")).forEach(removeTargetIfNotFrame);
}
// Create an observer instance
let observer = new MutationObserver(mutations => {
mutations.forEach(m => {
if (m.type === "attributes" && m.target.hasAttribute("target")) {
removeTargetIfNotFrame(m.target);
} else {
removeTargetAttributes(m.target);
}
});
});
// Pass in the target node, as well as the observer options
observer.observe(document.body, {
childList: true,
subtree: true,
attributes: true,
attributeFilter: ["target"]
});
removeTargetAttributes(document);