Hack 50. Search Wikipedia with Google Site Search

 < Day Day Up > 

Replace Wikipedia's slow search engine with Google's lightning-quick site search.

I hack because I care. Really. I spend a lot of time on Google, and it shows in the number of hacks I've written that customize my experience of Google's services. The same applies to Wikipedia, the free (and freely licensed) online encyclopedia. I Hold Wikipedia in the highest regard, not only as a useful research tool, but as an example of a successful online community.

So, what's my beef with Wikipedia? Their site search is incredibly slow. I freely admit that I've been spoiled by Google. If I even bother using a site's internal search engine (as opposed to, say, searching Google with the site name as an additional keyword), I am instantly annoyed if the site search doesn't come back with useful results in under one second. Simon Willison shares my frustration, and he wrote this hack that modifies Wikipedia's search form to use Google Site Search instead of the site's internal search engine.

6.5.1. The Code

This user script runs on all Wikipedia pages. It uses hardcoded knowledge of Wikipedia's page structure to find the search form (<form >), and then modifies the form's action attribute to point to Google Site Search. The search form has two submit buttons, so the script moves them around and directs one of them to Wikipedia's internal search, while the default button goes to Google Site Search.

Save the following user script as wikipedia-googlesearch.user.js:

 // ==UserScript== // @name   Search Wikipedia with Google // @namespace   http://simon.incutio.com/code/greasemonkey/ // @description   Alters Wikipedia search to use Google Site Search // @include   http://*.wikipedia.org/* // ==/UserScript== // based on code by Simon Willison // and included here with his gracious permission var form = document.getElementById('searchform'); var inputs = form.getElementsByTagName('input'); var input = inputs[0]; var go = inputs[1]; var search = inputs[2]; if (form && input && go && search) { // Move Go to the right form.appendChild(go); // Unbold it (by clearing its ID) go.id = ''; // Search should be bold instead search.style.fontWeight = 'bold'; // Update form to use Google form.action = 'http://www.google.com/search'; input.name = 'as_q'; // Add hidden q variable for site specific search var q = document.createElement('input'); q.type = 'hidden'; q.name = 'q'; q.value = 'site:' + window.location.host; form.appendChild(q); // Set Go up to behave as normal go.addEventListener('click', function(event) { window.location.href = 'http://en.wikipedia.org/wiki/Special' +  ':Search?search=' + escape(input.value); event.preventDefault();  }, true); } 

6.5.2. Running the Hack

Before installing the user script, go to http://en.wikipedia.org, enter logical fallacies in the search box on the left, and click Search. The site search will churn and churn, and eventually take you to a page showing search results. Your mileage may vary, but for me, this search takes almost 30 seconds.

Now, install the user script (Tools Install This User Script), and revisit or refresh http://en.wikipedia.org. The search form looks the same, except that the Go and Search buttons have been reversed. Now, when you type logical fallacies and click Search (or just press Enter), it will take you to Googles site search results of the Wikipedia site, as shown in Figure 6-8.

If you prefer to use Wikipedia's built-in search engine, you can do so by clicking Go instead of Search. The user script sets up this button to redirect to ">http://en.wikipedia.org/wiki/Special:Search?search=<your_search_keywords>. If Wikipedia can find an exact match, this will redirect to the result page; otherwise, it will display Wikipedia's search results page.

Figure 6-8. Google site search on wikipedia.org


     < 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