Google Search Terms Highlight

Automatically highlights search terms & full phrases in pages opened by Google search results.

  1. // ==UserScript==
  2. // @name Google Search Terms Highlight
  3. // @namespace GSTH
  4. // @version 5.0
  5. // @description Automatically highlights search terms & full phrases in pages opened by Google search results.
  6. // @include *
  7. // @grant GM_getValue
  8. // @grant GM_setValue
  9. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.18.2/babel.js
  11. // @require https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.16.0/polyfill.js
  12. // @icon https://www.google.ca/images/google_favicon_128.png
  13. // @author drhouse
  14. // ==/UserScript==
  15.  
  16. $(document).ready(function () {
  17.  
  18. var ref = document.referrer;
  19.  
  20. if (location.href.toString().indexOf("https://www.google.") == -1 && ref.indexOf("https://www.google.") != -1){
  21. var gsearchmark = GM_getValue("googleSearchTerm", "");
  22. var tokens = [].concat.apply([], gsearchmark.split('"').map(function(v,i){
  23. return i%2 ? v : v.split(' ');
  24. })).filter(Boolean);
  25. var oldre = /(?:^|\b)(xx)(?=\b|$)/;
  26. //walk(document.body, newre(oldre));
  27. var keys = $.map( tokens, function( value, key ) {
  28. function newre(e){
  29. return RegExp(e.toString().replace(/\//g,"").replace(/xx/g, value), "i");
  30. }
  31.  
  32. function handleText(node, targetRe) {
  33. var match, targetNode, followingNode, wrapper;
  34. match = targetRe.exec(node.nodeValue);
  35. if (match) {
  36. targetNode = node.splitText(match.index);
  37. followingNode = targetNode.splitText(match[0].length);
  38. wrapper = document.createElement('span');
  39. $(wrapper).css('background-color', 'yellow');
  40. targetNode.parentNode.insertBefore(wrapper, targetNode);
  41. wrapper.appendChild(targetNode);
  42.  
  43. if (node.nodeValue.length === 0) {
  44. node.parentNode.removeChild(node);
  45. }
  46. if (followingNode.nodeValue.length === 0) {
  47. followingNode.parentNode.removeChild(followingNode);
  48. }
  49. match = followingNode
  50. ? targetRe.exec(followingNode.nodeValue)
  51. : null;
  52. }
  53. }
  54.  
  55. function walk(node, targetRe) {
  56. var child;
  57. switch (node.nodeType) {
  58. case 1: // Element
  59. for (child = node.firstChild;
  60. child;
  61. child = child.nextSibling) {
  62. walk(child, targetRe);
  63. }
  64. break;
  65.  
  66. case 3: // Text node
  67. handleText(node, targetRe);
  68. break;
  69. }
  70. }
  71. walk(document.body, newre(oldre));
  72. });
  73. }
  74.  
  75. if (location.href.toString().indexOf("https://www.google.") != -1 && location.href.toString().indexOf("/search") != -1){
  76. var googleSearchTerm = $("#tsf > div:nth-child(2) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input").val();
  77. GM_setValue("googleSearchTerm", googleSearchTerm);
  78. }
  79. });

QingJ © 2025

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