Text Link Converter

将文本格式的URL转换为可点击的链接

当前为 2025-02-07 提交的版本,查看 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Text Link Converter
// @namespace    your-namespace
// @version      1.0
// @description  将文本格式的URL转换为可点击的链接
// @author       DeepSeek & SodaCode
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 配置参数
    const config = {
        linkAttributes: {
            target: "_blank",          // 在新标签页打开
            rel: "noopener noreferrer" // 安全属性
        },
        ignoredTags: ["SCRIPT", "STYLE", "TEXTAREA", "PRE", "CODE", "A"], // 要忽略的标签
        urlRegExp: /\b(https?|ftp):\/\/[^\s/$.?#].[^\s]*\b/gi              // URL匹配正则
    };

    // 创建可见性检查函数
    const isVisible = (element) => {
        const style = window.getComputedStyle(element);
        return style.display !== 'none' &&
               style.visibility !== 'hidden' &&
               element.offsetParent !== null;
    };

    // 链接转换函数
    const convertTextToLinks = (textNode) => {
        const parent = textNode.parentNode;
        if (!parent || config.ignoredTags.includes(parent.nodeName) || !isVisible(parent)) {
            return;
        }

        const tempDiv = document.createElement('div');
        const text = textNode.nodeValue;
        let lastIndex = 0;
        let match;

        while ((match = config.urlRegExp.exec(text)) !== null) {
            // 处理匹配前的文本
            if (match.index > lastIndex) {
                tempDiv.appendChild(document.createTextNode(text.slice(lastIndex, match.index)));
            }

            // 创建链接
            const link = document.createElement('a');
            link.href = match[0];
            link.textContent = match[0];

            // 添加额外属性
            Object.entries(config.linkAttributes).forEach(([attr, value]) => {
                link.setAttribute(attr, value);
            });

            tempDiv.appendChild(link);
            lastIndex = config.urlRegExp.lastIndex;
        }

        // 处理剩余文本
        if (lastIndex < text.length) {
            tempDiv.appendChild(document.createTextNode(text.slice(lastIndex)));
        }

        // 替换原始文本节点
        parent.replaceChild(tempDiv, textNode);
    };

    // 递归遍历DOM节点
    const walkNodes = (node) => {
        if (node.nodeType === Node.TEXT_NODE) {
            if (config.urlRegExp.test(node.nodeValue)) {
                config.urlRegExp.lastIndex = 0; // 重置正则状态
                convertTextToLinks(node);
            }
        } else if (!config.ignoredTags.includes(node.nodeName) && node.hasChildNodes()) {
            Array.from(node.childNodes).forEach(walkNodes);
        }
    };

    // 开始处理文档主体
    walkNodes(document.body);

    // 观察DOM变化处理动态内容
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === Node.ELEMENT_NODE) {
                    walkNodes(node);
                }
            });
        });
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();