highlightWords

Highlight Words

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/535935/1588732/highlightWords.js

  1. function escapeRegex(string) {
  2. return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  3. }
  4.  
  5. function highlightWords(words, highlightClass = 'highlight') {
  6. if (!words || words.length === 0) return;
  7.  
  8. // Create patterns for words and phrases
  9. const patterns = words.map(word => {
  10. const trimmed = word.trim();
  11. if (trimmed.includes(' ')) {
  12. // Handle phrases
  13. const parts = trimmed.split(/\s+/);
  14. const escapedParts = parts.map(escapeRegex);
  15. const pattern = escapedParts.map(part => `\\b${part}\\b`).join('\\s+');
  16. return { pattern, numWords: parts.length };
  17. } else {
  18. // Handle single words
  19. const escaped = escapeRegex(trimmed);
  20. const pattern = `\\b${escaped}\\b`;
  21. return { pattern, numWords: 1 };
  22. }
  23. });
  24.  
  25. // Sort patterns by number of words descending to match longer phrases first
  26. patterns.sort((a, b) => b.numWords - a.numWords);
  27.  
  28. // Create the regex pattern by joining all patterns with '|'
  29. const regexPattern = patterns.map(p => p.pattern).join('|');
  30. const regex = new RegExp(regexPattern, 'gi');
  31.  
  32. // Select text nodes excluding script, style, and already highlighted elements
  33. const xpath = `//text()[not(ancestor::script) and not(ancestor::style) and not(ancestor::*[contains(concat(" ", normalize-space(@class), " "), " ${highlightClass} ")])]`;
  34. const textNodes = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  35.  
  36. for (let i = 0; i < textNodes.snapshotLength; i++) {
  37. const textNode = textNodes.snapshotItem(i);
  38. const text = textNode.nodeValue;
  39. const matches = [...text.matchAll(regex)];
  40.  
  41. if (matches.length > 0) {
  42. const fragment = document.createDocumentFragment();
  43. let lastIndex = 0;
  44.  
  45. for (const match of matches) {
  46. const offset = match.index;
  47. if (offset > lastIndex) {
  48. fragment.appendChild(document.createTextNode(text.slice(lastIndex, offset)));
  49. }
  50. const span = document.createElement('span');
  51. span.className = highlightClass;
  52. span.textContent = match[0];
  53. fragment.appendChild(span);
  54. lastIndex = offset + match[0].length;
  55. }
  56.  
  57. if (lastIndex < text.length) {
  58. fragment.appendChild(document.createTextNode(text.slice(lastIndex)));
  59. }
  60.  
  61. textNode.parentNode.replaceChild(fragment, textNode);
  62. }
  63. }
  64. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址