Widget:Book cover

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

bh_.book_cover = { get_cover: 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.imageLinks && volume_info.imageLinks.thumbnail) { var image = volume_info.imageLinks.thumbnail; var style = element.getAttribute("data-style"); if (style) style = style.replace(/["<>&]/g, "");         element.innerHTML = "";        }      }    };

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

if (isbn.match(isbn_pattern)) retrieve_url(googleapi + isbn, handle_response); },

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