open discord link in app

use discord app to open discord link instead of open in browser

目前為 2022-04-14 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         open discord link in app
// @namespace    https://github.com/x94fujo6rpg/SomeTampermonkeyScripts
// @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();
				}
			});
		});
	}

})();