Greasy Fork

Greasy Fork is available in English.

网页文本转链接

高性能文本转链接方案,支持动态内容

当前为 2025-08-09 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        网页文本转链接
// @description   高性能文本转链接方案,支持动态内容
// @version      1.0
// @author       WJ
// @match       *://*/*
// @exclude      https://*.bing.com/*
// @exclude      https://*.baidu.com/*
// @exclude      https://*.yandex.com/*
// @exclude      https://*.yandex.ru/*
// @license       MIT
// @grant        none
// @run-at       document-idle
// @namespace   http://greasyfork.icu/users/914996
// ==/UserScript==

(() => {
  // 1. 注入样式
  document.head.insertAdjacentHTML(
    'beforeend',
    '<style>.url-link{color:#348A87;text-decoration:underline}</style>'
  );

  // 2. URL 正则与常量
  const tlds = [
    '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'
  ].join('|');
  const urlRegex = new RegExp(
    String.raw`\b[\w.:/?=%&#-]{3,}\.(?:${tlds})(?!\w)[\w.:/?=%&#-]*|` +
    String.raw`(?:(?:https?:\/\/)|(?:www\.|wap\.))[\w.:/?=%&#-@+~=]{3,250}\.[\w]{2,6}\b[\w.:/?=%&#-@+~=]*`,
    'gi'
  );

  // 3. 工具函数 - 优化跳过检测逻辑
  const skipTags = new Set(['A','SVG','SCRIPT','STYLE','TEXTAREA','BUTTON','SELECT','OPTION','CODE','PRE','INPUT']);
  const skipSelectors = [
    '[contenteditable]', 'code', 'pre', '.WJ_modal',
    '.modal', '.popup', '.dialog', '[role="dialog"]',
    '.ace_editor', '.CodeMirror', '.monaco-editor', '.cm-editor'
  ].join(',');
  const PROCESSED = 'data-url-processed';
  const shouldSkip = el => !el || skipTags.has(el.tagName) || el.isContentEditable || !!el.closest?.(skipSelectors);

  // 4. 节点处理函数
  const processNode = root => {
    if (!root || root.hasAttribute?.(PROCESSED) || shouldSkip(root)) return;
    root.setAttribute(PROCESSED, 'true');
    const walker = document.createTreeWalker(
      root,
      NodeFilter.SHOW_TEXT,
      { acceptNode: n => shouldSkip(n.parentElement) ? NodeFilter.FILTER_REJECT : NodeFilter.FILTER_ACCEPT }
    );
    const tasks = [];
    for (let node; (node = walker.nextNode());) {
      const o = node.textContent ?? '';
      const r = o.replace(urlRegex, m => {
        const h = /^\w+:\/\//.test(m) ? m : `https://${m}`;
        return `<a class="url-link" target="_blank" rel="noopener noreferrer" href="${h}">${m}</a>`;
      });
      r !== o && tasks.push({ node, replaced: r });
    }
    for (const { node, replaced } of tasks) {
      node.replaceWith(document.createRange().createContextualFragment(replaced));
    }
  };

  // 5. 观察器
  const io = new IntersectionObserver(entries => {
    for (const { isIntersecting, target } of entries) {
      if (isIntersecting) {
        io.unobserve(target);
        requestIdleCallback?.(() => processNode(target), { timeout: 1000 });
      }
    }
  });
  const mo = new MutationObserver(mutations => {
    for (const { addedNodes } of mutations) {
      for (const node of addedNodes) {
        if (node.nodeType === 1 && !shouldSkip(node) && !node.hasAttribute?.(PROCESSED)) {
          io.observe(node);
        }
      }
    }
  });
  // 6. 初始化
  setTimeout(() => {
    [...document.body.children].forEach(el => !shouldSkip(el) && io.observe(el));
    mo.observe(document.body, { childList: true, subtree: true });
  }, 1000);
})();