Hack 55. Autocomplete Search Terms as You Type

 < Day Day Up > 

Google can suggest your search terms before you even finish typing them.

It's true: Google is clairvoyant. It can guess what you're going to search for even before you've typed it. Well, maybe that overstates it. But it can certainly take an educated guess, based on the popularity and number of results of certain keywords.

Don't believe me? Visit http://www.google.com/webhp?complete=1 and start typing, and Google will autocomplete your query after you've typed just a few characters. This is insanely cool, and virtually nobody knows about it. And even people "in the know" need to visit a special page to use it. This hack makes this functionality work everywhere even on the Google home page (http://www.google.com).

6.10.1. The Code

This user script runs on all Google pages, but it works only on pages with a search form. Of course, being Google, this is most pages, including the home page and web search result pages.

This hack doesn't do any of the autocompletion work itself. It relies entirely on Google's own functionality for suggesting completions for partial search terms, defined entirely in http://www.google.com/ac.js. All we need to do is create a <script> element pointing to Google's own code, and insert it into the page. Then, we tell Google to activate it by adding another <script> element that calls Google's own InstallAC function.

Save the following user script as google-autocomplete.user.js:

 // ==UserScript== // @name  Google Autocomplete // @namespace    http://diveintomark.org/projects/greasemonkey/ // @description  Autocomplete search keywords as you type // @include      http://*.google.tld/* // @exclude      http://*/*complete=1* // ==/UserScript== function getSearchBox(sFormName) { return document.forms.namedItem(sFormName); } function injectAC(sFormName) { var elmScript = document.createElement('script'); elmScript.src = 'http://www.google.com/ac.js'; document.body.appendChild(elmScript); var elmDriver = document.createElement('script'); elmDriver.innerHTML = 'var elmForm = document.forms.namedItem("' + sFormName + '");\n' + 'InstallAC(elmForm, elmForm.elements.namedItem("q"),' + 'elmForm.elements.namedItem("btnG"), "search", "en");'; document.body.appendChild(elmDriver); } var sFormName = 'f'; var elmForm = getSearchBox(sFormName); if (!elmForm) { sFormName = 'gs'; elmForm = getSearchBox(sFormName); } if (!elmForm) { return; } window.setTimeout(function( ) { injectAC(sFormName); }, 100); 

6.10.2. Running the Hack

After installing the user script (Tools Install This User Script), go to http://www.google.com and start typing the word greasemonkey. After typing the first three letters, gre, you will see a drop-down menu with possible completions, as shown in Figure 6-15.

If you continue typing greasemonkey and then type a space, Google will suggest possible multiword searches, as shown in Figure 6-16.

Figure 6-15. Autocompletion of "gre" search


Figure 6-16. Suggestions for multiword "greasemonkey" search


     < Day Day Up > 


    Greasemonkey Hacks
    Greasemonkey Hacks: Tips & Tools for Remixing the Web with Firefox
    ISBN: 0596101651
    EAN: 2147483647
    Year: 2005
    Pages: 168
    Authors: Mark Pilgrim

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net