您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Highlight Words
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/535935/1588724/highlightWords.js
function escapeRegex(string) { return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); } function highlightWords(words, highlightClass = 'highlight') { if (!words || words.length === 0) return; const regex = new RegExp('\\b(' + words.map(escapeRegex).join('|') + ')\\b', 'gi'); const xpath = `//text()[not(ancestor::script) and not(ancestor::style) and not(ancestor::*[contains(concat(" ", normalize-space(@class), " "), " ${highlightClass} ")])]`; const textNodes = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); for (let i = 0; i < textNodes.snapshotLength; i++) { const textNode = textNodes.snapshotItem(i); const text = textNode.nodeValue; const matches = [...text.matchAll(regex)]; if (matches.length > 0) { const fragment = document.createDocumentFragment(); let lastIndex = 0; for (const match of matches) { const offset = match.index; if (offset > lastIndex) { fragment.appendChild(document.createTextNode(text.slice(lastIndex, offset))); } const span = document.createElement('span'); span.className = highlightClass; span.textContent = match[0]; fragment.appendChild(span); lastIndex = offset + match[0].length; } if (lastIndex < text.length) { fragment.appendChild(document.createTextNode(text.slice(lastIndex))); } textNode.parentNode.replaceChild(fragment, textNode); } } }