Open Discord message links in desktop client

Replace Discord message links with links using discord:// protocol so that the messages open in the desktop client.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Open Discord message links in desktop client
// @namespace    https://jacken.men
// @version      1.1
// @description  Replace Discord message links with links using discord:// protocol so that the messages open in the desktop client.
// @author       jack1142
// @license      Apache-2.0; https://www.apache.org/licenses/LICENSE-2.0
// @match        *://*/*
// @exclude      /^https?://(?:(?:ptb|canary|www)\.)?discord(?:app)?\.com/
// @grant        none
// ==/UserScript==

// Copyright 2021 Jakub Kuczys (https://github.com/jack1142)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

(function() {
    'use strict';

    const LINK_REGEX = /^https?:\/\/(?:(?:ptb|canary|www)\.)?discord(?:app)?\.com\/channels\/((?:\d{15,21}|@me)\/\d{15,21}\/\d{15,21})\/?$/i;

    const links = document.querySelectorAll("a[href]");

    for (let link of links) {
        const match = link.href.match(LINK_REGEX);
        if (match === null) {
            continue;
        }
        link.href = `discord://discord.com/channels/${match[1]}`;
    }
})();