Ah, Software

Bookmarklets

Bookmarklets are little snippets of code that you can hide in your browser's list of links. Instead of whisking you off to some far-off url they can perform more useful tasks such as looking up a word in an online dictionary or checking the availability of a book in your local library. Here are some such snippets that we've come up with. To use them drag the link into your "Links" bar if you're using Internet Explorer or onto your Personal Toolbar if you're using Netscape. If you're using something else, drag it whereever you would normally drag a url you want to save.

Thomas Bill Lookup

This bookmarklet looks up a selected (highlighted) bill on the Thomas server at the congressional website. It is currently set up for the 107th session of congress for 2002. Note that the session is determined by the current date and the algorithm will have to be adjusted if you want a different session. The bill number should be of the form HR nnnn or S nnnn with or without spaces or periods. The javascript code uses regular expressions to parse the highlighted text, so the bill number simply needs to be somewhere in the highlighted section.

Tested so far on Windows 2000 using IE 5.0 and Netscape 4.6.

Sample text.


Now a bill (S 2646) has been introduced in the Senate, with bipartisan support, to set new protections that will insure fair hearings. See also HR 2147 or S.811 or S 812 for further details. Not to mention H.R.303.

Find on the All Music Guide

This will look up an artist, for example the Cowboy Junkies or Cecil Taylor, on the All Music Guide.

Find in Encyclopedia Brittanica

This will look up the highlighted term on EncyclopediaBrittanica.com.


Some guides for writing bookmarklets

  • All the javascript code ends up in one line. You can format it as you like - however, all the whitespace gets stripped out when it becomes a bookmark or link.
  • A consequence of being in one line is that you can't place comments in your code unless they're at the very end - otherwise all the code after the comment becomes part of the comment. This makes debugging a bit awkward. I usually end up with a lot of alert() statements that get tossed before finalizing the code.
  • Code tested on a web page and in a bookmarklet may behave differently, depending on the code's interaction with the web page contents. The getHighlight() function below, for example, can't be tested properly except as a bookmarklet.
  • Apparently there is a 500-or-so-character limit in IE on bookmarklet code. This limits the complexity of what a bookmarklet can handle.


  • The code:


    function getHighlight()
    {
    var isIE = (navigator.appName.indexOf('Microsoft') != -1);
    var highlight='';
    if (isIE)
    {
    highlight = document.selection.createRange().text;
    }
    else
    {
    highlight = document.getSelection();
    }
    return (highlight);
    }
    // Returns the highlighted text on the current web page.


    function findSession()
    {
    var now=new Date();
    return (now.getFullYear() - 1895);
    }
    // Determines the current session number from the current year (2002 = 107th congressional session).