Google Search Highlighter

Highlights searched terms in results in yellow, and underlines exact matches

  1. // ==UserScript==
  2. // @name Google Search Highlighter
  3. // @namespace gglSearchHighlight_kk
  4. // @description Highlights searched terms in results in yellow, and underlines exact matches
  5. // @version 1.0
  6. // @author Kai Krause <kaikrause95@gmail.com>
  7. // @include http*://www.google.*/*
  8. // @include http*://www.google.co*.*/*
  9. // @run-at document-start
  10. // ==/UserScript==
  11.  
  12. let embedCSS = setInterval(() => {
  13. if (document.head) {
  14. let css = document.createElement("style");
  15. css.innerText = "em { background-color: #FFFF7F; color: black !important }";
  16. document.head.appendChild(css);
  17. clearInterval(embedCSS);
  18. }
  19. }, 4);
  20.  
  21. setInterval(() => {
  22. let search = document.title;
  23. search = search.replace(/(imagesize|site|related|cache|inurl|filetype|-|OR).*?(?=\s|$)/g, "|split|");
  24. search = search.replace(/"/g, "|split|");
  25. search = search.split("|split|");
  26.  
  27. search.forEach((term) => {
  28. term = term.trimStart().trim().toLowerCase();
  29. term = term.replace(/(\s\*\s|\*)/g, ".*?");
  30. if (!term) return;
  31.  
  32. document.querySelectorAll("em").forEach((el) => {
  33. let str = el.textContent.trimStart().trim().toLowerCase();
  34.  
  35. let isMatch = false;
  36.  
  37. if (str === term) {
  38. isMatch = true;
  39. }
  40. else if (term.includes("*")) {
  41. let re = new RegExp(term);
  42. if (re.test(str)) isMatch = true;
  43. }
  44.  
  45. if (isMatch) el.innerHTML = "<u>" + el.textContent + "</u>";
  46. });
  47. });
  48. }, 50);