Writing a bookmarklet

A bookmarklet is a piece of javascript that you store as a bookmark. It’s a neat way of adding functionality to a web page that you don’t control. As part of my job, my team develops code in branches and after it has passed testing it needs to be ported to the trunk. Although we generate a branch report using CruiseControl which lists outstanding code ports, the page doesn’t have any totals on it. I’d like to be able just to view the totals of outstanding ports for my team. Although I could ask our build team to change how the page is generated, you can actually achieve this fairly simply using a bookmarklet. Here is the code I wrote:
    /* count the occurences of each person */
    /* the branch report is in a pre formatted tag */
    var reportInfo = document.getElementsByTagName("pre");
    var textContent = reportInfo[0].innerHTML;
    var names = ["jsmith","jbloggs","tblair","gbrown"];
    var results = "";
    /* count the occurences of a name by splitting */
    /* the string on it and subtracting one from the total */
    for (var i=0; i<names.length; i++) {
        var count = textContent.split(names[i] + "@").length;
        if (count &gt; 0) {
            count = count - 1;
        results = results + names[i] + ": " + count + "<br&gt;";
    /* create a div to display the results */
    var s=document.createElement('div');
    s.style.border='2px solid black';
You can see that it is pretty easy to count the occurrences of the developer names by using the trick of splitting a string on the name and then subtracting one from the length of the resulting array. After that you just create a new div on the page to display the results.
This entry was posted in Javascript and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

HTML tags are not allowed.

516,491 Spambots Blocked by Simple Comments