-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathopen_discord_link_in_app.user.js
54 lines (48 loc) · 1.46 KB
/
open_discord_link_in_app.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
// ==UserScript==
// @name open discord link in app
// @namespace https://github.com/x94fujo6rpg/SomeTampermonkeyScripts
// @updateURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/open_discord_app.user.js
// @downloadURL https://github.com/x94fujo6rpg/SomeTampermonkeyScripts/raw/master/open_discord_app.user.js
// @version 0.02
// @description use discord app to open discord link instead of open in browser
// @author x94fujo6
// @match *://*/*
// ==/UserScript==
/* jshint esversion: 9 */
(async function () {
const script_name = "open discord link in app";
let count = 0, id;
await wait_tab();
id = setInterval(() => {
let result = main();
if (!result) {
count++;
}
if (count >= 10) {
clearInterval(id);
}
}, 1000);
function main() {
let reg = /https\:\/\/(discordapp\.com\/channels.*|discord\.com\/channels.*)/,
links = document.querySelectorAll("a"),
discord_links;
if (links.length > 0) {
discord_links = [...links].filter(a => a.href.match(reg));
if (discord_links.length > 0) {
[...discord_links].forEach(a => a.href = a.href.replace(reg, "discord://$1"));
return true;
}
}
return false;
}
function wait_tab() {
return new Promise(resolve => {
if (document.visibilityState === "visible") return resolve();
document.addEventListener("visibilitychange", () => {
if (document.visibilityState === "visible") {
return resolve();
}
});
});
}
})();