Earlier today I was searching around Google for some technical information, and was given Expert Exchange as a link. Experts Exchange has this nifty little feature, implemented as an Apache module, whereby the HTTP referer is examined for search engine footprints. When it is found that you were linked by a search engine, this Apache module named mod_suru will highlight those search terms inside the resultant web page.
“Cool,” I thought. So I went to look at mod_suru and discovered that it is only available at a relatively high cost. Certainly not a cost that I’m willing to pay for my own personal website.
So I took another path. I re-implemented the same idea as mod_suru through primarily JavaScript. My stomphighlight.js file defines a function, highlightWord, which does the magic of actually highlighting the text. The magic of determining which words to highlight is highly dependant upon the setup that the web site has. In the case of stompstompstomp.com, every footer on every page is served by a Python function. I added the following code to my footer:
import os, re, cgi
# Highlight google-ed for words!
if os.environ.has_key("HTTP_REFERER"):
words = None
m = re.search("google.[a-z.]+/search\\?(.*)", os.environ['HTTP_REFERER'])
if m:
googleQuery = cgi.parse_qs(m.group(1))
words = ' '.join(googleQuery['q']).split(' ')
words = filter(lambda x: x.find(":") == -1, words) # remove words like 'site:blahblah.com'
if words != None:
print "<!-- Begin magical search term highlighting. -->"
print """<script language="JavaScript" type="text/javascript" src="/stomphighlight.js"></script>"""
colors = ("#00eeee", "#eeee00", "#ee00ee", "#ee0000", "#00ee00", "#0000ee")
print """<script language="JavaScript" type="text/javascript"><!--"""
for i in range(len(words)):
try:
print "highlightWord(%r, %r, document.documentElement);" % (words[i], colors[i])
except IndexError:
pass
print """//--></script>"""
print "<!-- End magical search term highlighting. -->"
Hopefully, it should be pretty clear how you could take this and use the same idea on your own web site, or modify my code to support more than just Google. Yay!