-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtabify.js
57 lines (51 loc) · 1.32 KB
/
tabify.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
49
50
51
52
53
54
55
56
57
function addTarget(hostname) {
var elements = document.getElementsByTagName("a");
for (var e of elements) {
if(e.getAttribute("href") !== "#") {
e.setAttribute("target", "_blank");
e.setAttribute("rel", "nofollow");
addSpecialTweaks(e, hostname);
}
}
}
function filter(hostname) {
chrome.storage.local.get(null, function(items){
for(var item in items){
var re = new RegExp( '^'+item.replace('*','.*')+'$' );
if(re.test(hostname)){
addTarget(hostname);
}
}
});
}
function addSpecialTweaks(element, hostname){
var regex = new RegExp('youtube');
if(regex.test(hostname)){
element.addEventListener('click', openinNew);
element.removeAttribute("target");
}
}
function clone(element) {
var children = element.children;
var clone = element.cloneNode(true);
for (cas of clone.childNodes) {
clone.removeChild(cas);
}
for (child of element.childNodes) {
clone.appendChild(child);
}
element.parentNode.replaceChild(clone, element);
}
function openinNew(e) {
e.stopPropagation();
e.preventDefault();
window.open(this.href, "_blank");
window.stop();
}
var url = new URL(window.location.href);
filter(url.hostname);
var observer = new MutationObserver(function () {
var url = new URL(window.location.href);
filter(url.hostname);
});
observer.observe(document.body, {subTree: true, attributes: true});