-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathall_link_to_new_tab.user.js
81 lines (77 loc) · 2.42 KB
/
all_link_to_new_tab.user.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// ==UserScript==
// @name all links in new tab
// @namespace https://github.com/x94fujo6rpg/SomeTampermonkeyScripts
// @updateURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/all_link_to_new_tab.user.js
// @downloadURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/all_link_to_new_tab.user.js
// @version 0.04
// @description open all links in new tab
// @author x94fujo6
// @match *://*/*
// @grant GM_openInTab
// @run-at document-body
// ==/UserScript==
/* jshint esversion: 9 */
(function () {
'use strict';
const
/**
* active: decides whether the new tab should be focused, (是否在新分頁打開後跳到該分頁)
* insert: that inserts the new tab after the current one, (是否將新分頁排在目前分頁後面)
* setParent: makes the browser re-focus the current tab on close (是否在該分頁關閉時回到目前分頁)
* incognito: makes the tab being opened inside a incognito mode/private mode window. (是否在隱密模式下打開分頁)
*/
newTabConfig = {
active: false,
insert: false,
setParent: false,
incognito: false,
},
linkReg = /^(https|http):\/\/.+/,
log = (...any) => console.log(`%c[open all link in new tab]%c`, "color:OrangeRed;", "", ...any),
asyncTimeout = (fun, time) => {
return new Promise(resolve => {
setTimeout(() => {
fun();
resolve();
}, time);
});
},
setOnclick = () => {
let links = document.querySelectorAll("[href]");
if (links) {
links.forEach(link => {
if (!link.href.match(linkReg)) {
log(`this is not a http/https link, skip`, link);
return;
}
if (link.getAttribute("setNewTab") == "true") {
log(`this link has been set, skip`, link);
return;
}
link.onclick = function (event) {
GM_openInTab(link.href, newTabConfig);
event.preventDefault();
};
link.setAttribute("setNewTab", true);
});
return true;
} else {
setTimeout(setOnclick, 500);
return false;
}
},
setQueue = async (max = 3) => {
let done = false;
for (let i = 0; i < max; i++) {
await asyncTimeout(() => done = setOnclick(), 1000 * i);
if (!done) continue;
log("all links are set");
break;
}
},
ob = new MutationObserver(setQueue);
//--------------------------------
log("script start");
setQueue();
ob.observe(document.body, { childList: true, });
})();