Hack 96. Refine Your Google Search

 < Day Day Up > 

Google might already know what keywords you should add to your search to find exactly what you're looking for.

As described in "Autocomplete Search Terms as You Type" [Hack #55], you can visit http://www.google.com/webhp?complete=1 and start typing, and Google Suggest will autocomplete your query as you type. By itself, this is wickedly cool. Now, let's make it even cooler by integrating it into the main Google web search. Along with the usual search results, you'll see a list of related queries made up of additional keywords, so you can refine your search.

12.3.1. The Code

Google Suggest works by requesting a specially constructed URL with the characters you've typed so far. The request returns JavaScript code, and Google Suggest evaluates this code and adds the results to its autocomplete menu. If you type a complete keyword, followed by a space, Google Suggest will return a list of popular searches that include your keyword plus one or two other words.

For example, if you type firefox, Google Suggest constructs this URL:

 http://www.google.com/complete/search?js=true&qu=firefox  

Enter that URL in your location bar and you'll see Google's response:

 sendRPCDone(frameElement, "firefox", new Array("firefox", "firefox download", "firefox browser", "firefox extensions", "firefox plugins", "firefox mozilla", "firefox themes", "firefox.com", "firefox web browser", "firefox 1.0"), new Array("25,900,000 results", "8,000,000 results", "6,990,000 results", "1,270,000 results", "1,250,000 results", "8,160,000 results", "1,950,000 results", "1 result", "5,460,000 results", "6,540,000 results"), new Array("")); [end example] 

In other words, Google is already doing the hard part: tracking billions of queries and ranking them by popularity. Compared to that, constructing the request and parsing the response is easy. You can mimic Google's autocomplete algorithm by constructing the URL yourself, calling GM_ xmlhttpRequest, and parsing the response.

Save the following user script as refinesearch.user.js:

 // ==UserScript== // @name Refine Your Search // @namespace http://diveintomark.org/projects/greasemonkey/ // @description adds a "refine your search" list on Google search results // @include http://www.google.tld/search* // ==/UserScript== function getCurrentSearchText( ) { var elmForm = document.forms.namedItem('gs'); if (!elmForm) { return; } var elmSearchBox = elmForm.elements.namedItem('q'); if (!elmSearchBox) { return; } var usQuery = elmSearchBox.value; if (!usQuery) { return; } return usQuery; } function getFirstSearchResult( ) { var results = document.evaluate("//p[@class='g']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); return results.snapshotLength ? results.snapshotItem(0) : null; } function parseRefineYourSearchResults(oResponse) { if (oResponse.responseText.indexOf('new Array(') == -1) return; var arResults = oResponse.responseText.split( 'new Array("')[1].split('")')[0].split('", "'); var usQuery = getCurrentSearchText( ); var htmlArResults = new Array( ); for (var i = 0; i < arResults.length; i++) { if (!arResults[i] || (arResults[i] == usQuery)) continue; htmlArResults.push('<a href="http://www.google.com/search?q=' + escape(arResults[i]) + '">' + arResults[i] + '</a>'); } if (!htmlArResults.length) return; var elmRefine = document.createElement('div'); elmRefine.id = 'refineyoursearch'; elmRefine.style.fontSize = 'small'; elmRefine.style.paddingTop = elmRefine.style.paddingBottom = '1em'; var html = 'Refine your search: ' + htmlArResults.join(' &middot; '); elmRefine.innerHTML = html; var elmFirstResult = getFirstSearchResult( ); elmFirstResult.parentNode.insertBefore(elmRefine, elmFirstResult); } var usQuery = getCurrentSearchText( ); if (!usQuery) return; if (!getFirstSearchResult( )) return; GM_xmlhttpRequest({ method: "GET", url: "http://www.google.com/complete/search?hl=en&js=true&qu=" +   escape(usQuery + ' '), onload: parseRefineYourSearchResults }); 

12.3.2. Running the Hack

After installing the user script from Tools Install This User Script, go to http://www.google.com and search for firefox. Before the first search result, youll see a list of related queries, as shown in Figure 12-3.

Figure 12-3. Google search for "firefox" with suggested refined searches


If you click on one of the suggested refined searches, such as firefox plugins, Google displays those search results, which include suggestions for even further refinements, as shown in Figure 12-4. Depending on your keywords, you might be able to drill down several levels, until Google finally runs out of suggestions.

Figure 12-4. Google search for "firefox plugins" with suggestions


Google Suggest works only on web searches, and only in English, so this hack inherits those limitations. You can read more about Google Suggest in Google's FAQ (http://labs.google.com/suggestfaq.html).

     < 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