Greasy Fork

Greasy Fork is available in English.

网页文本转链接

网页中文本转为可点击链接 添加颜色下划线

当前为 2025-06-28 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         网页文本转链接
// @description  网页中文本转为可点击链接 添加颜色下划线
// @version      2.3
// @author       WJ
// @match        *://*/*
// @license      MIT
// @grant        none
// @namespace http://greasyfork.icu/users/914996
// ==/UserScript==

(function() {
    'use strict';
    
    // 正则表达式优化
    const ZZBDS = /\b[\w.:/?=%&#-]{3,}\.(?:app|aero|aer|art|asia|beer|biz|cat|cc|chat|ci|cloud|club|cn|com|cool|coop|co|dev|edu|email|fit|fun|gov|group|hk|host|icu|info|ink|int|io|jobs|kim|love|ltd|luxe|me|mil|mobi|moe|museum|name|net|nl|network|one|online|org|plus|post|press|pro|red|ren|run|ru|shop|site|si|space|store|tech|tel|top|travel|tv|tw|uk|us|video|vip|wang|website|wiki|wml|work|ws|xin|xyz|yoga|zone)(?!\w)[\w.:/?=%&#-]*/gi;
    
    // 添加样式
    const style = document.createElement('style');
    style.textContent = '.domain-highlight{color:#348A87!important;text-decoration:underline!important;cursor:pointer!important}';
    document.head.appendChild(style);
    
    // 创建树遍历器收集文本节点
    const treeWalker = document.createTreeWalker(
        document.body,
        NodeFilter.SHOW_TEXT,
        { 
            acceptNode: (node) => {
                const parent = node.parentNode;
                return parent.isContentEditable || ['TEXTAREA', 'BUTTON', 'SCRIPT', 'STYLE', 'A'].includes(parent.tagName)
                    ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT;
            }
        }
    );
    
    // 批量处理节点
    const nodes = [];
    while (treeWalker.nextNode()) nodes.push(treeWalker.currentNode);
    
    for (const node of nodes) {
        const text = node.textContent;
        // 仅保留正则检查
        if (!ZZBDS.test(text)) continue;
        ZZBDS.lastIndex = 0;
        
        const HTM = text.replace(ZZBDS, match => 
            `<a href="${/^https?:\/\//i.test(match) ? '' : 'https://'}${match}" 
              class="domain-highlight" target="_blank">${match}</a>`
        );
        
        if (HTM !== text) {
            const PAN = document.createElement('span');
            PAN.innerHTML = HTM;
            node.replaceWith(PAN);
        }
    }
})();