Tuesday, December 9, 2008

Creating an intelligent shortcut (bookmarklet) in Firefox

On of the frequent address shortcuts I use in Firefox is to redirect "w sometext" in the address bar to lookup a term (sometext) in Wikipedia. As I frequently go to my "Watchlist" in Wikipedia I though it would be neat to default to that page if I had not specified a term. A little research and I'd worked out how to get this to work as a bookmarklet. In the address bar any text added to the shortcut is referenced as "%s".

Using this basic structure it would be pretty easy to add a more complex set of conditions to make the same shortcut do many more things, such as looking up a book and working out if the text you put in was an ISBN or a title.

Here's my example formatted so you can read it:


javascript:(
function(){
  if("%s".length==0){
     // Default
    window.location="http://en.wikipedia.org/wiki/Special:Watchlist"
  }else{
     // Wikipedia lookup
    window.location="http://en.wikipedia.org/wiki/%s"
  }
}
)()


And here it is formatted as a link you can drop into your bookmarks, don't forget to edit it so that it has "w" as a keyword/shortcut:
Wikipedia lookup

No comments: