Highlight Searched-for Text


The following code implements a feature that will highlight the search word in the pages that where found by TextSearch or by Google, Yahoo and many other search engines. The core of this feature is a JavaScript library by the name of searchhi.

From the searchhi website:
The searchhi JavaScript library is a way of automatically highlighting words on a page when that page was reached by a search engine. In essence, if you search, for example, Google for some words, and then follow a link from the search results to a searchhi enabled page, the words you searched for will be highlighted on that page.

First I had to make a few minor changes to this JavaScript libary. This to make it aware of the TextSearch activity.
The resulting file (searchhi.js) is then copied in the 3rdparty/plugins/searchhi directory. Also make sure that your create a .htaccess file in the same directory with the following content:
<IfModule mod_rewrite.c>
RewriteEngine off
</IfModule>


Then copy the searchhi.js file into the 3rdparty/plugins/searchhi directory.
 /* http://www.kryogenix.org/code/browser/searchhi/ */
/* Modified 20021006 to fix query string parsing and add case insensitivity */
function highlightWord(node,word) {
    // Iterate into this nodes childNodes
    if (node.hasChildNodes) {
        var hi_cn;
        for (hi_cn=0;hi_cn<node.childNodes.length;hi_cn++) {
            highlightWord(node.childNodes[hi_cn],word);
        }
    }
   
    // And do this node itself
    if (node.nodeType == 3) { // text node
        tempNodeVal = node.nodeValue.toLowerCase();
        tempWordVal = word.toLowerCase();
        if (tempNodeVal.indexOf(tempWordVal) != -1) {
            pn = node.parentNode;
            if (pn.className != "searchword") {
                // word has not already been highlighted!
                nv = node.nodeValue;
                ni = tempNodeVal.indexOf(tempWordVal);
                // Create a load of replacement nodes
                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.appendChild(hiwordtext);
                pn.insertBefore(before,node);
                pn.insertBefore(hiword,node);
                pn.insertBefore(after,node);
                pn.removeChild(node);
            }
        }
    }
}

function SearchHighlight() {
    if (!document.createElement) return;
    ref = document.referrer;
    if (ref.indexOf('?') == -1) return;
    qs = ref.substr(ref.indexOf('?')+1);
    qsa = qs.split('&');
    for (i=0;i<qsa.length;i++) {
        qsip = qsa[i].split('=');
            if (qsip.length == 1) continue;
            if (qsip[0] == 'q' || qsip[0] == 'p' || qsip[0] == 'phrase') { // q= for Google, p= for Yahoo, phrase for wikka
            words = unescape(qsip[1].replace(/\+/g,' ')).split(/\s+/);
                    for (w=0;w<words.length;w++) {
                highlightWord(document.getElementsByTagName("body")[0],words[w]);
                    }
            }
    }
}

window.onload = SearchHighlight;


Now all that rest us is two small changes to the wikka.css file and to the header.php action.

In css/wikka.css add the following lines:
/* ##### searchhi plugin ##### */

.searchword {
  background-color: yellow;
}


In the actions/header.php file add the following code just before the </HEAD> tag:
<script src="3rdparty/plugins/searchhi/searchhi.js" type="text/javascript"></script>


It is installed on my Wikka so you can see for yourself how it works.


CategoryUserContributions
Comments
Comment by BrianKoontz
2006-02-02 13:16:26
Appears to work on Firefox just fine (at least with version 1.0).
Comment by RolandStens
2006-02-03 00:13:50
Hmm, my version is Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7
Comment by BrianKoontz
2006-02-03 00:54:57
Huh...maybe I'm just looking at the wrong thing. I e-mailed you a screenshot with what looks like some highlighting to me...
Comment by RolandStens
2006-02-03 17:15:30
Yes, that is beautiful highlighting on your Mac!
But I am running XP. Maybe a bug report for the firefox folks.
What is your exact version?
Comment by BrianKoontz
2006-02-04 00:10:27
The screenshot I sent you was from version 1.5, beta 2.
Comment by RolandStens
2006-02-05 01:30:22
Upgraded to Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1 and this works fine now!
Comment by JavaWoman
2007-07-26 02:28:03
It would be nice if it supported other search engines than only Google and Yahoo! Ask, MS LiveSearch, MSN...
Comment by RolandStens
2007-07-27 18:06:36
MS LiveSearch works already, Ask ditto, MSN=MS LiveSearch

Do a search like this: site:stens.ca "Good Stuff to Know"
And you'll see that it works.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki