// ==UserScript== // @name 网页中网址高亮显示并跳转 // @namespace http://tampermonkey.net/ // @version 0.1 // @license MIT // @description 在网页上选中网址时自动高亮显示,并点击跳转 // @author Jack // @match *://*/* // @grant none // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 正则表达式匹配网址 const urlRegex = /https?:\/\/[^\s]+/g; // 遍历页面中的所有文本节点 function traverseTextNodes(node) { if (node.nodeType === Node.TEXT_NODE) { const text = node.nodeValue; const matches = text.match(urlRegex); if (matches) { const span = document.createElement('span'); span.innerHTML = text.replace(urlRegex, match => `${match}`); node.parentNode.replaceChild(span, node); } } else { for (let child of node.childNodes) { traverseTextNodes(child); } } } // 初始化函数 function init() { traverseTextNodes(document.body); } // 等待页面加载完成后执行 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();