Search Word Highlighter

Search Word Highlighter is a Userscript that will highlight your Google search words on web pages.
Before you can use a userscript you need to first install:
Greasemonkey
You can swich highlighting on and off from the greasemonkey menu.
If you switch on the greedy highlighting it will also highlight previous search terms.
Download Search Word Highlighter from the userscripts.org website. (often doesn't work)
Download Search Word Highlighter from the userscripts-mirror.org website.
Download Search Word Highlighter from the GO-HERE.NL website.
Source
// ==UserScript==
// @name Search word highlighter
// @namespace fgjshaflkjhwejslfkjshfshfklajhasdlfjhsl
// @description Highlights search words on search result pages
// @include *
// @version 1
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_registerMenuCommand
// ==/UserScript==
/* I've adapted se-hilite(2006) by scott Yang: http://scott.yang.id.au/code/se-hilite/ */
Hilite = { max_nodes: 1000, /* Maximum number of DOM nodes to test. */
style_name: 'hilite', /* Name of the style to be used.*/
style_name_suffix: true }; /* Use different style names for different search keywords */
colors = "#FCF75A#BBFC5A#6BFC5A#5AFCE9#5ACBFC#FC855A#FC5AE9#FC5A5A#B65AFC".split('#'); /* Colors to use */
highlightONcss="";
highlightOFFcss = "";
for(x=1;x<=colors.length;x++){ highlightONcss += ".hilite"+x+" {background-color:#"+colors[x]+";color:#000;}"; } /* Make a css for the colors! */
for(x=1;x<=colors.length;x++){ highlightOFFcss += ".hilite"+x+" {background-color:transparent;color:inherit;}"; } /* Make a css without colors. */
GM_registerMenuCommand("Highlighter ON/OFF", turnOnOff, "H");
GM_registerMenuCommand("Highlighter greedy/exact", beGreedy, "G");
function turnOnOff(){ var css = document.createElement("style"); /* Swap the CSS with colours for one without (and back) */
css.type = "text/css";
if(GM_getValue("highlighter", "on") == "on"){ GM_setValue("highlighter", "off");
css.innerHTML = highlightOFFcss; }
else { GM_setValue("highlighter", "on");
css.innerHTML = highlightONcss; }
document.head.appendChild(css);
}
function beGreedy(){ if(GM_getValue("greedyMatching", "off") == "on"){ GM_setValue("greedyMatching", "off"); } /* Greedy matching. (i.e. if "porn" should be highlighted in "pornography") */
else { GM_setValue("greedyMatching", "on"); } }
var css = document.createElement("style");
css.type = "text/css";
if(GM_getValue("highlighter", "on") == "on"){ css.innerHTML = highlightONcss; } /* Use the right css */
else{ css.innerHTML = highlightOFFcss; }
document.head.appendChild(css);
Hilite.hiliteElement = function(elm, query){ if (!query || elm.childNodes.length == 0){ return; } /* The legacy code to Highlight a DOM element with a list of keywords. */
var qre = new Array();
for (var i = 0; i < query.length; i ++) { query[i] = query[i].toLowerCase();
if (GM_getValue("greedyMatching", "off") == "off" ){ qre.push('\\b'+query[i]+'\\b'); }
else{ qre.push(query[i]); } }
qre = new RegExp(qre.join("|"), "i");
var stylemapper = {};
for (var i = 0; i < query.length; i ++) { if (Hilite.style_name_suffix){ stylemapper[query[i]] = Hilite.style_name+(i+1); }
else{ stylemapper[query[i]] = Hilite.style_name; } }
var textproc = function(node) { var match = qre.exec(node.data);
if (match) { var val = match[0];
var k = '';
var node2 = node.splitText(match.index);
var node3 = node2.splitText(val.length);
var span = node.ownerDocument.createElement('SPAN');
node.parentNode.replaceChild(span, node2);
span.className = stylemapper[val.toLowerCase()];
span.appendChild(node2);
return span; }
else { return node; } };
Hilite.walkElements(elm.childNodes[0], 1, textproc); }
Hilite.hilite = function() { var e = null; /* Highlight a HTML document. */
q = GM_getValue("firstQuery", "");
if(GM_getValue("greedyMatching", "") == "on" && GM_getValue("secondQuery","") !=""){ q = q + "+" + GM_getValue("secondQuery",""); }
q = q.split('+').slice(0,100);
if (q &&(e = document.body)){ Hilite.hiliteElement(e, q); } }
Hilite.walkElements = function(node, depth, textproc) {
var skipre = /^(script|style|textarea)/i;
var count = 0;
while (node && depth > 0){ count ++;
if (count >= Hilite.max_nodes) { var handler = function() { Hilite.walkElements(node, depth, textproc); }
setTimeout(handler, 50);
return; }
if (node.nodeType == 1) { if (!skipre.test(node.tagName) && node.childNodes.length > 0) { node = node.childNodes[0];
depth ++;
continue; } }
else if (node.nodeType == 3) { node = textproc(node); }
if (node.nextSibling) { node = node.nextSibling; }
else { while (depth > 0) { node = node.parentNode;
depth --;
if (node.nextSibling) { node = node.nextSibling;
break; } } } } }
if(location.href.indexOf('https://www.google.com/search?') != 0 ){ Hilite.hilite(); } /* If we are not on google.com we want the highlighting. */
else { foo = ""; /* If we are on google.com we dont want the highlighting. */
theQuery = document.location.search.slice(1).split('&');
for(x=0;x<theQuery.length;x++){ if(theQuery[x].indexOf('q=') == 0){ GM_setValue("firstQuery", theQuery[x].slice(2));
GM_setValue("secondQuery", ""); } }
window.onhashchange = function () { theHash = document.location.hash.slice(1).split('&'); /* A newer query (if any) is to be found in the location hash. */
for(x=0;x<theHash.length;x++){ if(theHash[x].indexOf('q=') == 0){ GM_setValue("secondQuery", theHash[x].slice(2)); } } } }
