One of Firefox's most useful features is Quick Searches, the ability to assign a keyword to a link and pass a parameter in when you launch it - like thes useful to look up "useful" in a thesaurus.
But what about shortcuts that need more than one parameter, like comparing two stock prices? Turns out that multiple parameter keyword searches in Firefox are possible, with some Javascript magic.
After the jump, find out how to compare 3 stock symbols or pit 4 words against one another with Firefox keyword searches.
Single-parameter quick searches use the %s symbol to pass information into a link. For example, a bookmark with the keyword map might be http://maps.google.com/maps?q=%s. Type map San Diego Zoo into the address bar with that bookmark set up and poof! You'll get a map of the zoo.
If you want directions from one place to another, you need multiple parameters, which you can have - with a little help from our friend Javascript. Use this link template to enable as many %s instances in your bookmark URL as you need (sorry for the horizontal scroll):
javascript:var%C2%A0s='%s'; url='###INSERT YOUR KEYWORD SEARCH URL HERE###'; t=''; qc=0; chunks=url.split('%s'); for(i=0; i<s.length; i++){if(s.charAt(i)=='"')qc=qc^1; t+=((s.charAt(i)==' '&&qc)?'^':s.charAt(i)); }args=t.split(/\s/); nurl=''; for(i=0; i<chunks.length; i++){nurl+=chunks[i]; if(args[i]!=undefined)%C2%A0{args[i]=args[i].replace(/\^/g,' '); nurl+=args[i]; }}location.replace(nurl,'< BR>');
Replace ###INSERT YOUR KEYWORD SEARCH URL HERE###' with your bookmark URL that has multiple instances of the %s parameter in it.
Check out some examples of useful multi-parameter keyword bookmarks. (Note: Copy and paste each of these URLs into a new bookmark and assign a keyword to launch them from the Firefox address bar.)
Alexaholic site traffic comparison (2 sites)
javascript:var%C2%A0s='%s'; url='http://www.alexaholic.com/%s+%s'; t=''; qc=0; chunks=url.split('%s'); for(i=0; i<s.length; i++){if(s.charAt(i)=='"')qc=qc^1; t+=((s.charAt(i)==' '&&qc)?'^':s.charAt(i)); }args=t.split(/\s/); nurl=''; for(i=0; i<chunks.length; i++){nurl+=chunks[i]; if(args[i]!=undefined)%C2%A0{args[i]=args[i].replace(/\^/g,' '); nurl+=args[i];}}location.replace(nurl,'<BR>');
Bookmark this and give it the keyword
comparetraffic. Then, type comparetraffic lifehacker.com example.com to run it.
Google Fight (2 terms)
javascript:var%C2%A0s='%s'; url='http://googlefight.com/index.php?lang=en_GB&word1=%s&word2=%s'; t=''; qc=0; chunks=url.split('%s'); for(i=0; i<s.length; i++){if(s.charAt(i)=='"')qc=qc^1; t+=((s.charAt(i)==' '&&qc)?'^':s.charAt(i)); }args=t.split(/\s/); nurl=''; for(i=0; i<chunks.length; i++){nurl+=chunks[i]; if(args[i]!=undefined)%C2%A0{args[i]=args[i].replace(/\^/g,' '); nurl+=args[i]; }}location.replace(nurl,'< BR>');
Pit two search terms against one another. Bookmark this one with the keyword
fight and then try fight "Getting Things Done" Lifehacker to try it out. (Remember, multi-word values have to be enclosed in quotes.)
Google Trends (2 terms)
javascript:var%C2%A0s='%s'; url='http://www.google.com/trends?q=%s%2C%s&ctab=0&geo=all&date=all'; t=''; qc=0; chunks=url.split('%s'); for(i=0; i<s.length; i++){if(s.charAt(i)=='"')qc=qc^1; t+=((s.charAt(i)==' '&&qc)?'^':s.charAt(i)); }args=t.split(/\s/); nurl=''; for(i=0; i<chunks.length; i++){nurl+=chunks[i]; if(args[i]!=undefined)%C2%A0{args[i]=args[i].replace(/\^/g,' '); nurl+=args[i]; }}location.replace(nurl,'< BR>');
Compare the usage patterns of two words using Google Trends data.
Google Maps directions (start address, destination address)
javascript:var%C2%A0s='%s'; url='http://maps.google.com/maps?saddr=%s&daddr=%s'; t=''; qc=0; chunks=url.split('%s'); for(i=0; i<s.length; i++){if(s.charAt(i)=='%22')qc=qc^1; t+=((s.charAt(i)==' '&&qc)?'^':s.charAt(i)); }args=t.split(/\s/); nurl=''; for(i=0; i<chunks.length; i++){nurl+=chunks[i]; if(args[i]!=undefined)%C2%A0{args[i]=args[i].replace(/\^/g,' '); nurl+=args[i]; }}location.replace(nurl,'< BR>');
Get directions from Google Maps using a
directions keyword and your starting and ending points, ie, directions "San Diego, CA" "San Francisco, CA"
Yahoo! Stock price comparison (2 stock symbols)
javascript:var%C2%A0s='%s'; url='http://finance.yahoo.com/q/bc?t=6m&s=%s&c=%s'; t=''; qc=0; chunks=url.split('%s'); for(i=0; i<s.length; i++){if(s.charAt(i)=='"')qc=qc^1; t+=((s.charAt(i)==' '&&qc)?'^':s.charAt(i)); }args=t.split(/\s/); nurl=''; for(i=0; i<chunks.length; i++){nurl+=chunks[i]; if(args[i]!=undefined)%C2%A0{args[i]=args[i].replace(/\^/g,' '); nurl+=args[i]; }}location.replace(nurl,'< BR>');
For more on quick searches, check out Adam's previously-published article, Firefox and the art of keyword bookmarking.











Got something to say? Comment instantly or login now.
03/16/07
No sorry I have had a problem with escaped character, this is the good code:
javascript:var quote=document.getSelection();var s='%s'; if ((quote=='' && s.length>5)) {testo=encodeURIComponent(s.substring(5,s.length));url='http://translate.google.com/translate_t?langpair=%s|%s&text='+testo;} else{if (quote=='') url='http://www.google.com/translate?langpair=%s|%s&u='+encodeURIComponent(location.href); else url='http://translate.google.com/translate_t?text='+quote+'&langpair=%s|%s'};t=''; qc=0; chunks=url.split('%s'); for(i=0; i<5; i++){if(s.charAt(i)=='%22')qc=qc^1; t+=((s.charAt(i)==' '&&qc)?'^':s.charAt(i)); }args=t.split(/\s/); nurl=''; for(i=0; i
Sorry, please delete my message if possible :-(
03/16/07
Great trick :-) Thanks Gina :-)
I have used your code for a bookmarklet to translate page that I have posted also in my blog with
citation of your original post.
This is my bookmarklet:
javascript:var quote=document.getSelection();var s='%s'; if ((quote=='' && s.length>5)) {testo=encodeURIComponent(s.substring(5,s.length));url='http://translate.google.com/translate_t?langpair=%s|%s&text='+testo;} else{if (quote=='') url='http://www.google.com/translate?langpair=%s|%s&u='+encodeURIComponent(location.href); else url='http://translate.google.com/translate_t?text='+quote+'&langpair=%s|%s'};t=''; qc=0; chunks=url.split('%s'); for(i=0; i; if(args!=undefined) {args=args.replace(/\^/g,' '); nurl+=args; }}location.replace(nurl,'');
It use Google Translate to translate web pages, text selected and tex as third parameters.
It need two parameters for language of page/text and language to translate page/text.
Example:
trans en it
without selected text translate the english page in italian
trans en fr
with selected text, translate selected text in french
trans en it I like Lifehacker
translate "I like Lifehacker" in italian :-).
There is only a problem with the bookmarklet.
If I use strange character as "è,ùà" as third parameter it doesn't work :-(
I have another one that use BabelFish instead of Google translate.
03/13/07
Yeah, I'm a web developer, so when I learned about the %s features of Firefox bookmarking, I ran with it.
I wrote a PHP or ColdFusion page where I send my %s param and it parses the query string and does whatever action I decide.
for instance, I wrote a page that sends emails to my wife,since I email her a lot. So this is all I have to enter in my address bar:
em h I'm leaving work at 5:30 today.\n\nSee you soon.
em - That's firefox's keyword.
h - My wife's first initial. My ColdFusion page expects that the first word in the string will be my code/identifier/key that maps to an array of possible recipients.
the rest - anything after the first code word gets put into the message. Note that I also added code that finds "\n" and replaces it with a hard return (to let me do a little formatting).
And with PHP sites costing so little these days, just about anybody with some technical savvy can get into some similar web processing!
03/02/07
@Jonathan Aquino: I totally agree. YubNub can do all this and more, and I use it every day. I even installed it as a Web Search engine in Quicksilver and created a special trigger to bring it up directly.
(For everyone else, though, realize that Jon's a bit biased, as he is Yubnub's creator.)
03/01/07
Yet another way to do it is to create a YubNub command for it (then others can use what you've created). I've created one for comparetraffic - type "man comparetraffic" into YubNub to see its implementation.
03/01/07
@Shlake: I can't seem to find a way to search for a location by its label (home, work, etc). You could hardcode them into the javascript along with a bit of logic... replace 'nurl+=args[i];' with 'if (args[i].match(/HOME/i)){nurl+='real home address';}else{nurl+=args[i];}' and put as many 'if else' statements as you need if you need more than one label...
03/01/07
These are very useful, but there's a keyword issue that's driving me crazy:
I already have a keyword for the Google Bookmarks bookmarklet (thanks to LH), but now I'm trying to create a keyword that allows me to enter the labels at the address bar, instead of waiting for the GB window to open and load. Consequently, I would also be able to have separate keywords with preset labels.
For example, I could type "gb SomeLabel" and get the regular Google Bookmarks window, and it would already have the label "SomeLabel" filled in.
I tried putting "&labels:%s" on the bookmarklet address, but it won't work. I don't understand why, because when I put the same thing at the end of the "pure" URL (without the javascript: stuff) it works.
Any ideas?
03/01/07
OK... I can't figure out how to make a code snippet in the comments look good, but here's a version of the Google Maps quick search that supports 2 or more addresses. I've tested up to 5 addresses...
javascript:var%C2%A0s='%s';url='http://maps.google.com/maps?saddr=%s&daddr=%s';t='';qc=0;for(i=0; i<s.length; i++){if(s.charAt(i)=='"')qc=qc^1; t+=((s.charAt(i)==' '&&qc)?'^':s.charAt(i));}nurl='';chunks=url.split('%s');args=t.split(/\s/);for(i=2;i<args.length;i++){tmp=chunks.pop();chunks.push('+to:');chunks.push(tmp);}for(i=0;i<chunks.length;i++){nurl+=chunks[i];if(args[i]!=undefined)%C2%A0{args[i]=args[i].replace(/\^/g,' ');nurl+=args[i];}}location.replace(nurl,'< BR>');"
03/01/07
You can obtain directions from Google Maps via quick search simply by inserting 'to' between the two locations.
This code looks like it could be quite useful though. Thanks Lifehacker!
03/01/07
Anybody know how to use keywords for Google Maps' saved locations?
That would make the Directions QuickSearch pretty useful. Instead of having to type in "directions [entire address 1] [entire address 2]", you could use a keyword for something like "directions home work"
5:37 AM
Anybody know how to use keywords for Google Maps' saved locations?
That would make the Directions QuickSearch pretty useful. Instead of having to type in "directions [entire address 1] [entire address 2]", you could use a keyword for something like "directions home work"