Barnes & Noble-Hennepin County Library Lookup v. 1.4

By Glenn Peterson Last update Dec 12, 2009 — Installed 1,375 times.

There are 3 previous versions of this script.

// ==UserScript==
// @name          Barnes & Noble-Hennepin County Library Lookup v. 1.4
// @namespace     http://www.mundell.org
// @description	  Search the Hennepin County Library Catalog from BN book listings.
// @include       http://*.barnesandnoble.*
// ==/UserScript==

// fixed for Firefox 1.5 and GM 0.6.4
// v.3 updated 10/3/07 for changes on BN website
// v.4 updated 8/12/08 for changes on BN website

(

function()
{

var libraryUrlPattern = 'http://catalog.hclib.org/ipac20/ipac.jsp?index=ISBN&term='
var libraryURLPatternForLink = 'http://hzapps.hclib.org/pub/ipac/link2ipac.cfm?BNLinkyVersion=1.4&index=ISBN&term='
var libraryURLPatternForNoMatch = 'http://hzapps.hclib.org/pub/ipac/link2ipac.cfm?BNLinkyVersion=1.4&index=UTL&term='
var libraryName = 'Hennepin County';
var libraryAvailability = /Checked In/;
var libraryOnOrder = /(\d+) Copies On Order/;
var libraryInProcess = /Pending/; 
var libraryHolds = /Current Requests: (\d+)/;
var libraryCopies = /Reservable copies: (\d+)/;
var libraryDueBack = /(\d{2}\/\d{2}\/\d{4})/;
var notFound = /Sorry, could not find anything matching/

var libraryLookup = 
    {
    insertLink: function(isbn, hrefTitle, aLabel, color)
        {
        var div = origTitle.parentNode;
        var title = origTitle.firstChild.nodeValue;

        var newTitle = document.createElement('h2');
        newTitle.setAttribute('style','margin-bottom: 0; padding-bottom: 0;');

        var titleText = document.createTextNode(title);
        newTitle.appendChild(titleText);
        
        var br = document.createElement('br');

        var link = document.createElement('a');
        link.setAttribute ( 'title', hrefTitle );
        link.setAttribute('href', libraryURLPatternForLink + isbn);
        link.setAttribute('style','color: ' + color + '\;' + 'background-color:#FFFF99; font-size:100%');

        var label = document.createTextNode( aLabel );

        link.appendChild(label);

        div.insertBefore(newTitle, origTitle);
        div.insertBefore(br, origTitle);
        div.insertBefore(link, origTitle);
        div.removeChild(origTitle);
        },

    insertNoMatchLink: function(TheTitle, hrefTitle, aLabel, color)
        {
        var div = origTitle.parentNode;
        var title = origTitle.firstChild.nodeValue;

        var newTitle = document.createElement('b');
        newTitle.setAttribute('id','title');

        var titleText = document.createTextNode(title);
        newTitle.appendChild(titleText);
        
        var br = document.createElement('br');

        var link = document.createElement('a');
        link.setAttribute ( 'title', hrefTitle );
        link.setAttribute('href', libraryURLPatternForNoMatch + TheTitle);
        link.setAttribute('style','color: ' + color + '\;' + 'background-color:#FFFF99; font-size:100%');

        var label = document.createTextNode( aLabel );

        link.appendChild(label);

        div.insertBefore(newTitle, origTitle);
        div.insertBefore(br, origTitle);
        div.insertBefore(link, origTitle);
        div.removeChild(origTitle);
        },
		
    doLookup: function ( isbn )
        {
        GM_xmlhttpRequest
            ({
            method:'GET',
            url: libraryUrlPattern + isbn,
            onload:function(results)
                {
                page = results.responseText;
                if ( notFound.test(page) )
                    {
                    var due = page.match(notFound)[1]
                    libraryLookup.insertNoMatchLink (
                        TheTitle,
                        "Check for other editions",
                        "This edition not in " + libraryName + " Library. Click to check for other editions.",
                        "red"
                        );
                    }
				else if ( libraryAvailability.test(page) )
                    {
					var copies = page.match(libraryCopies)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "On the shelf now!",
                        "Available in " + libraryName + " Library!  (Library owns " + copies + " copies)",
                        "green"
                        );
                    }
                else if ( libraryOnOrder.test(page) )
                    {
					var CopiesOnOrder = page.match(libraryOnOrder)[1]
					var holds = page.match(libraryHolds)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "On order!",
                        "Request from " + libraryName + " Library (" + CopiesOnOrder + " copies on order, " + holds + " requests)",
                        "#AA7700"  // dark yellow
                        );
                    }                    
                else if ( libraryInProcess.test(page) )
                    {
					var copies = page.match(libraryCopies)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "In process!",
                        "Available soon at " + libraryName + " Library! (" + copies + " copies pending)" ,
                        "#AA7700"  // dark yellow
                        );
                    }                    
                 else if ( libraryHolds.test(page)  )
                    {
                    var holds = page.match(libraryHolds)[1]
                    var copies = page.match(libraryCopies)[1]
                    var due = page.match(libraryDueBack)[1]
                    libraryLookup.insertLink (
                        isbn,
                        holds + " Holds",
                        "Request from " + libraryName + " Library (currently " + holds + " requests on " + copies + " copies)",
                        "#AA7700"   //dark yellow
                        );
                    }
				else if ( libraryDueBack.test(page) )
                    {
                    var due = page.match(libraryDueBack)[1]
                    libraryLookup.insertLink (
                        isbn,
                        "Due back " + due,
                        "Due back at " + libraryName + " Library on " + due,
                        "#AA7700"  // dark yellow
                        );
                    }
                else
                    {
                    libraryLookup.insertLink (
                        isbn,
                        "Error",
                        "Error checking " + libraryName + " Library",
                        "orange"
                        );
                    }
                }
            });
        }


    }

// var isbn = window.genTextSm.location.href.match(/isbn=(\d{7,9}[\d|X])/)[1];

/*
var isbn;
for (var i  in document.anchors) {
	if (i.href != null) {
	  alert(i.href);	
	  if (i.href.match(/selection=(\d{7,9}[\d|X])/)) {
	    isbn = i.href.match(/selection=(\d{7,9}[\d|X])/);
	  }
	}
}
*/

/*
try 
    { var isbn = window.content.location.href.match(/\/(\d{7,9}[\d|X])\//)[1];  }
catch (e)
    { return; }
*/

var origTitle = document.evaluate("//div[@class='wrapL0']/h1", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
var TheTitle = origTitle.textContent ;

var isbnNode = document.evaluate("//a[@class='isbn-a']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
var rawisbn = isbnNode.textContent ;
var isbn = rawisbn.match(/\d{1,12}[\d|X]/);
//alert (isbn);

if ( ! origTitle )
  { return; }

libraryLookup.doLookup(isbn);

}
)();