Greasy Fork is available in English.
网页中文本转为可点击链接 添加颜色下划线
当前为
// ==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);
}
}
})();