Greasy Fork

Greasy Fork is available in English.

磁力链和ED2K文本变成超链接超级链接

自动将磁力链和ED2K文本变为超链接

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name           磁力链和ED2K文本变成超链接超级链接
// @namespace      http
// @version        1.1.1
// @description    自动将磁力链和ED2K文本变为超链接
// @author         dwpublic
// @include        http*://*
// @match          http*://*
// @grant          none
// @run-at         document-end
// @license        MIT
// ==/UserScript==

(function() {
    const magnetRegex = /((magnet:\?xt=urn:[a-zA-Z0-9]+:[a-zA-Z0-9]{32,}))(?=\s|$)/g;
    const ed2kRegex = /ed2k:\/\/\|file\|(.+?)\|(\d+)\|([0-9a-fA-F]+)\|\//g;

    function convertLinks(element) {
        if (element.innerText != undefined) {
            element.innerHTML = element.innerHTML
                .replace(magnetRegex, '<a target="_blank" href="$1" style="text-decoration:underline;">$1</a>')
                .replace(ed2kRegex, '<a target="_blank" href="ed2k://|file|$1|$2|$3|/" style="text-decoration:underline;">ed2k://|file|$1|$2|$3|/</a>');
        }
    }

    function processNode(node) {
        if (node.nodeType === Node.TEXT_NODE && (node.nodeValue.match(magnetRegex) || node.nodeValue.match(ed2kRegex))) {
            const span = document.createElement('span');
            span.innerHTML = node.nodeValue
                .replace(magnetRegex, '<a target="_blank" href="$1" style="text-decoration:underline;">$1</a>')
                .replace(ed2kRegex, '<a target="_blank" href="ed2k://|file|$1|$2|$3|/" style="text-decoration:underline;">ed2k://|file|$1|$2|$3|/</a>');
            node.parentNode.replaceChild(span, node);
        } else if (node.nodeType === Node.ELEMENT_NODE) {
            node.childNodes.forEach(child => processNode(child));
        }
    }

    document.addEventListener('DOMContentLoaded', () => {
        document.querySelectorAll('body *').forEach(element => processNode(element));
    });
})();