Widget:Book title

if (!("bh_" in window)) window.bh_ = {};

bh_.book_title = { get_title: function(element) { var googleapi = "https://www.googleapis.com/books/v1/volumes?q=isbn:"; var isbn_pattern = /^(97[89]\d{10}|\d{9}[\dX])$/;

var retrieve_url = function(url, callback) { var request = new XMLHttpRequest; request.open("GET", url, true); request.responseType = "json"; request.timeout = 5000; request.onload = function { callback(request.status, request.response); }; request.ontimeout = function { callback; }; request.send; };

var handle_response = function(status, response) { if (status == 200         && response.items !== undefined && response.items.length) { var volume_info = response.items[0].volumeInfo; if (volume_info.subtitle !== undefined) element.textContent = volume_info.title + ": " + volume_info.subtitle; else element.textContent = volume_info.title; } else { element.textContent = "(Title not available)"; }   };

var isbn = element.value.replace(/[ -]/g, "");

if (isbn.match(isbn_pattern)) retrieve_url(googleapi + isbn, handle_response); else element.textContent = "(Invalid ISBN)"; },

initialize: function { var elements = document.getElementsByClassName("bh-book-title"); if (elements.length) bh_.book_title.get_title(elements[elements.length - 1]); } } bh_.book_title.initialize;