Greasemonkey script: Google Suggest

Written by Adrian Holovaty on March 20, 2005

I can't get enough of Greasemonkey, Aaron Boodman's Firefox extension that takes my All Music Guide extension idea to the next level by making it super simple to create and distribute scripts that alter Web pages.

Here's a Greasemonkey script I just cooked up: Google Suggest for Greasemonkey. It makes the dynamic drop-down from Google Suggest a bona fide part of Google, placing it on the Google home page and on search-result pages.

Installation directions for Greasemonkey novices:

  • Run Mozilla Firefox.
  • Install the Greasemonkey browser extension.
  • Restart your browser.
  • Go to my script's page, click "Tools | Install User Script...", and say OK to the prompts.
  • Go to Google, type in search terms, and watch the suggested search terms appear.

Tech note: The cool thing about the script is that it leverages Google's well-abstracted autocompletion library by dynamically appending it to the page as a <script> element and running a single initialization function. The dynamic <script>-appending technique could be a handy way to do more powerful stuff with Greasemonkey in the future.

UPDATE, April 26: If you don't want to install Greasemonkey, just install this super-simple Firefox extension to get the same functionality:

Google Suggest extension

Comments

Posted by Aaron on March 20, 2005, at 3:13 a.m.:

Awesome, adrian.

Posted by Michael Bierman on March 20, 2005, at 12:16 p.m.:

Great idea! But it doesn't seem to work.

Posted by Jed on March 20, 2005, at 2:31 p.m.:

Yeah, it doesn't work for me either.

Posted by Jed on March 20, 2005, at 2:34 p.m.:

Actually, I think it doesn't work with Butler. When I have Butler activated I get Javascript errors, but when I deactivate it, this works fine.

Posted by Adrian on March 20, 2005, at 5:22 p.m.:

Thanks for the report, Jed! I'll look into better interoperability with Butler.

Incidentally, this brings up an interesting problem that Greasemonkey should probably address in the long term -- script dependencies/compatibilities. We've talked about it a little bit on the GM mailing list, but nothing's come of it just yet.

Posted by Scott Johnson on March 21, 2005, at 2:20 a.m.:

Weird...I have Butler running, and this script works fine for me. :)

Posted by Follower on March 22, 2005, at 1:48 p.m.:

Hi,

In the code you make the comment "Use setTimeout because there's a slight delay between the point at which the <script> is created and the point at which the InstallAC() function is available." I used to do that too, but discovered you can actually add an "load" event handler instead.

Below I've copied in a comment I made here: <http://libgmail.sourceforge.net/googlemaps.html>

--Phil.

Update: Okay, here's a much nicer implementation of the injection code that doesn't require the timeout hack. It relies on using a "load" event triggered when the inserted script element has loaded--handy really: (Inject External File Bookmarklet 2)

javascript:(function(){if(typeof(extjs)=='undefined'){extjs='';};extjs=prompt('File to inject:',extjs);var script=document.createElement('script');script.src=extjs;var evl=new Object();evl.handleEvent=function (e){onInject();};script.addEventListener('load',evl,true); document.getElementsByTagName('head')[0].appendChild(script);})();

Posted by Adrian on March 22, 2005, at 9:55 p.m.:

Follower/Phil -- Thanks! I've updated the script. I'd been wondering about a better way to do that. Excellent.

Posted by Adrian on March 22, 2005, at 10 p.m.:

FYI, this script has been incorporated into CustomizeGoogle.com (with permission).

Posted by Daniel on March 23, 2005, at 10:13 a.m.:

I've also updated the script at CustomizeGoogle.com with the latest Google Suggest script.

Posted by Anthony on March 24, 2005, at 9:55 a.m.:

Finally, an easier way to make webpages work better!

Posted by Etienne on April 1, 2005, at 2:44 p.m.:

This is the most useful GM script I've yet seen.

Suggested improvement - change the @include line to "http://*.google.*/*" - that way, international editions of google also get google suggest (i.e. not just google.com, but also google.fr, google.co.uk, etc.).

Posted by Adrian on April 1, 2005, at 8 p.m.:

Nice idea, Etienne! Thanks.

I've implemented it, with the caveat that it'll work on non-Google sites that happen to have a ".google." subdomain -- such as test.google.ishavingamassage.com.

Posted by Jeremy Dunck on April 8, 2005, at 5:49 a.m.:

Yeah, so this is a wart.

http://*.google.*/* will match international, but it will also match http://www.google.fooledyou.com

The wildcard is nice and simple for folks that aren't up on RegEx, but it's also pretty loose. You'd basically have to write an @include for every top level domain to include only what you actually meant to.

@include http://*.google.co.uk/*

@include http://*.google.co.jp/*

...

ad nauseum.

So that sucks.

Maybe GM should be a little smarter and do its own TLD matching, so that

@include http://*.google.*/*

actually means what people want, which is:

@include http://*.google.<any valid TLD>.

(scurries off to bugzilla)

Posted by Jeremy Dunck on April 8, 2005, at 5:56 a.m.:

Logged as 9808. But it won't be fixed soon. More important issues to deal with. Thank you, please drive through. :)

Posted by Robert Andrews on April 16, 2005, at 4:49 p.m.:

So how do I get rid of the bar at the foot of the browser, which has "google_suggest" on it, on a blue background?

Thanks

Posted by Adrian on April 18, 2005, at 10:04 p.m.:

Robert -- I don't see anything like that. Any idea how I could reproduce the problem?

Posted by Anton on April 21, 2005, at 7:57 p.m.:

why isn't autocomplete turned off on the input box? Looks bad when I get my earlier searches in another dropdown. Also it should be turned off on the new google history service, because it doesn't make sense to have it there (perhaps it should rather give you autocomplete on your previous search?)

Posted by anonymous on May 20, 2005, at 10:12 a.m.:

It seems as if your script doesn't work for the new Google personalized homepage at http://www.google.com/ig

Posted by Koushiro on May 21, 2005, at 2:12 a.m.:

It won't work on the personalized homepage because the search form there has action=http://www.google.com/search, while the script searches for a form with action=/search. I'm no javascript hacker, so I'm not quite sure how to fix it elegantly: a very clumsy fix would be to just replace the line:

return document.evaluate("//form[@action='/search']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

with something like:

if (document.evaluate("//form[@action='/search']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue) {

return document.evaluate("//form[@action='/search']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

} else if (document.evaluate("//form[@action='http://www.google.com/search']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue) {

return document.evaluate("//form[@action='http://www.google.com/search']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

}

But, as I say, that's a relatively clumsy way of doing it. Works, though.

Posted by Neil Grover on May 26, 2005, at 11 p.m.:

nice work. one fix if I may suggest one. The browsers autocomplete feature will popup overtop of the javascript one. to prevent this I added the line:

f.q.setAttribute('autocomplete','off');

now my code looks like this:

(function() {

get_search_form = function() {

return document.evaluate("//form[@action='/search']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

}

// Find the search form. If it doesn't exist on this page, don't bother.

if (!get_search_form()) return;

// "Import" Google Suggest JavaScript by dynamically appending it to the

// current page as a <script> element.

var s = document.createElement('script');

s.src = 'http://www.google.com/ac.js';

var ev = new Object();

ev.handleEvent = function (e) {

var f = get_search_form();

f.q.setAttribute('autocomplete','off');

InstallAC(f, f.q, f.btnG, 'search', 'en');

};

s.addEventListener('load', ev, true);

document.getElementsByTagName('head')[0].appendChild(s);

})();

Posted by Aron Dazim on May 30, 2005, at 1:49 p.m.:

The above code is implemented in the CustomizeGoogle script (if you don't want to edit the code by yourself)

Posted by anonymous on June 3, 2005, at 9:42 a.m.:

I am using your Firefox extension for a little while and I am fascinated with it. But have one question - a problem I can not resolve: when I use "shift" and try to copy any word in a "Google search window", it is deleted immediately. Is it possible to abort such function?

Posted by Sallu on January 23, 2006, at 6:35 p.m.:

Google is offerring an 2005/2006 version NAV which is a great product by symantec.. (well almost) - I saw some news on google av on http://www.googleav.com - is this free antivirus software? where can I download the latest version of google av?

Posted by Suraj jain on May 10, 2006, at 6:49 a.m.:

Here is the code to display entries from database as Google Suggest . It is an combination of ASP + JS . works with acces database . Download the full source code and see example at

http://www.guwahatiwebhosting.com/ajax/dynamiclist/index.asp

Posted by Thibaud on June 28, 2006, at 1:59 p.m.:

Didn't work for me. Firefox 1.5.0.4. Greasemonkey 0.6.4.

When I go to Google and type in the search field nothing like Google Suggest happens.

:-/

Comments have been turned off for this page.