Wednesday, July 29, 2009

Multi-Parameter Firefox Keywords and Chrome Search Engines

By far, my most popular post is my guide on Firefox Keywords and Chrome Search Engines. I recommend reading that one to get caught up if you don't know what Firefox Keywords and/or Chrome Search Engines are.

Shortly after I wrote that, I found this post on Lifehacker explaining how to combine bookmarklets and keywords for some very clever and useful tricks, but it didn't work on Chrome as written, and I never got around to figuring it out. Tonight I finally took the time to figure it out.

The problem (besides the Lifehacker example being overly complicated) is that Chrome doesn't like {}'s in keywords. Those aren't really necessary unless your keywords are very complicated, though, so I got it to work. This is the code we'll be working with:

javascript:
var s='%s';
url='YOUR URL, WITH %s's FOR EACH TERM';
query='';
urlchunks=url.split('%s');
schunks=s.split(';');
for(i=0; i<schunks.length; i++)query+=urlchunks[i]+schunks[i];
location.replace(query);

As it says in the code, all you need to do is insert the url you want to use, with the usual %s's anywhere you want to insert a search term. You then set it up like any other keyword or search engine (see the other post for how to do that; note: you can just copy/paste the multi-line code into Chrome or Firefox, they don't require that you condense it down to one line first). To use it, just separate your search terms with ;'s.

This fairly simple code can let you do some very cool things. Here's an example for mapping between any two points in Google Maps:

javascript:
var s='%s';
url='http://maps.google.com/maps?saddr=%s&daddr=%s';
query='';
urlchunks=url.split('%s');
schunks=s.split(';');
for(i=0; i<schunks.length; i++)query+=urlchunks[i]+schunks[i];
location.replace(query);

I set this up as a Chrome search engine with keyword "mapp" (although something like "directions" might be easier to remember). If I type "mapp NYC; DC", I get a google map between New York City and Washington, DC.

A warning: if you include a space after the ; like I did in that example, you technically get a space before your second search term. I could probably over-complicate the code and clean that out, but it works fine for Google, and I can learn to avoid it once I set up ones that don't tolerate it. Oh, and if you include more ;'s than the keyword has %s's, the extra stuff will just be tacked onto the end (for example, "mapp NYC;DC;Other" tries to generate a map between "NYC" and "DC Other"

If you use this, please let me know in the comments if you come up with any clever ones. Remember: this works for any url that you want to insert something into, not just searches, so there are definitely some interesting possibilities out there waiting to be discovered.

5 comments:

Suel Brito said...

Thank you so much!
It works perfectly!

Morc said...

Could this not work any more in Chrome 23? I think I did as instructed, but Chrome does nothing after hitting Enter, just clears the line.

Jon Harmon said...

Darnit, it doesn't seem to work anymore. I'll post if I find a way to make it work.

Jon Harmon said...

It seems to work IF I already have something loaded on the page; if it's a brand new tab, it doesn't work.

Anonymous said...

Thank you!!

Works perfectly for me! You made the best browser trick even better!

Chrome:
33.0.1750.117 (Official Build 252094) m

Windows