< Day Day Up > |
When you click through to a page from a search engine, highlight the terms you originally searched for. Have you ever searched for something on Google, then clicked through to a page and been unable to figure out why this page ranked so highly? Not only does it seem irrelevant, you can't even find the keywords you originally searched for! This hack tracks your search engine clickthroughs and highlights your original search keywords when you leave the results page for a given hit. 6.11.1. The CodeThis user script runs on all pages except Google search result pages. The code is divided into three parts:
Save the following user script as searchhi.user.js: // ==UserScript== // @name Search Highlight // @namespace http://www.kryogenix.org/code/ // @description highlight search terms when coming a search engine // @include * // @exclude http://www.google.tld/search* // ==/UserScript== // based on code by Stuart Langridge // and included here with his gracious permission // http://www.kryogenix.org/code/browser/searchhi/ function highlightWord(node, word) { if (node.hasChildNodes) { for (var hi_cn = 0; hi_cn<node.childNodes.length; hi_cn++) { highlightWord(node.childNodes[hi_cn], word); } } if (node.nodeType == Node.TEXT_NODE) { var tempNodeVal, tempWordVal, pn, nv, ni, before, docWordVal, after, hiwordtext, hiword; tempNodeVal = node.nodeValue.toLowerCase( ); tempWordVal = word.toLowerCase( ); if (tempNodeVal.indexOf(tempWordVal) != -1) { pn = node.parentNode; if (pn.className != "searchword") { nv = node.nodeValue; ni = tempNodeVal.indexOf(tempWordVal); before = document.createTextNode(nv.substr(0,ni)); docWordVal = nv.substr(ni, word.length); after = document.createTextNode(nv.substr(ni+word.length)); hiwordtext = document.createTextNode(docWordVal); hiword = document.createElement("span"); hiword.className = "searchword"; hiword.style.backgroundColor = 'yellow'; hiword.style.color = 'black'; hiword.appendChild(hiwordtext); pn.insertBefore(before, node); pn.insertBefore(hiword, node); pn.insertBefore(after, node); pn.removeChild(node); } } } } function highlightSearchKeywords( ) { var ref = document.referrer; if (ref.indexOf('?') == -1) { return; } var qs = ref.substr(ref.indexOf('?')+1); var qsa = qs.split('&'); for (var i = 0; i < qsa.length; i++) { var qsip = qsa[i].split('='); if (qsip.length == 1) { continue; } if (qsip[0] == 'q') { var words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/); for (var w = words.length - 1; w >= 0; w--) { highlightWord(document.body, words[w]); } } } } window.addEventListener('load', highlightSearchKeywords, true); 6.11.2. Running the HackAfter installing the user script (Tools Install This User Script), go to http://www.google.com and search for greasemonkey. Click through to the Greasemonkey home page (http://greasemonkey.mozdev.org/), and you will see the word The script can also handle multiword searches. Go to http://www.google.com and search for download firefox. Click through to the Firefox home page (http://www.mozilla.org), and you will see both download and firefox highlighted in several places, as shown in Figure 6-18.
Figure 6-17. Greasemonkey home page with "Greasemonkey" highlightedFigure 6-18. Firefox home page with "download" and "firefox" highlighted6.11.3. Hacking the HackIt's easy to extend this script to handle search engines other than Google. Whenever you click from one page to another, you have access to the referring page in document.referrer. (That's why the script works in the first place.) Yahoo! Web Search uses a slightly different URL on its result pages. On Google, your search keywords are stored in the q parameter; on Yahoo!, they are stored in the p parameter. To highlight search terms when coming from either Google or Yahoo!, change this line: if (qsip[0] == 'q') { to this: if (qsip[0] == 'q' || qsip[0] == 'p') { You might also want to exclude Yahoo! search result pages by adding this line to the script's metadata section: // @exclude http://search.yahoo.com/* This prevents the script from highlighting your search terms on the second page of Yahoo!'s search results. |
< Day Day Up > |