My first bookmarklet

After quite some time I write one of these things that were supposed to be the overall topic of this blog: a snippet. This time, it’s a bookmarklet. My first bookmarklet at all. So without further ado, here’s what it takes to immediately jump from an O’Reilly catalog page to the corresponding search page at amazon.com.

function getElementsByTagNameAndClassName(tagName, className)
{
        var matches = [];
        var tagElements = document.getElementsByTagName(tagName);
        for (var i = 0; i < tagElements.length; i++)
        {
                if (tagElements[i].className == className)
                {
                        matches[matches.length] = tagElements[i];
                }
        }
        return matches;
}

var bookTitle = getElementsByTagNameAndClassName('span', 'book-title')[0].textContent;
var baseURL = 'http://www.amazon.com/s?index=blended&field-keywords=';
location.href = baseURL + encodeURIComponent(bookTitle);

Leave a Reply