Widget:Book cover/doc

< Widget:Book cover

This widget emits JavaScript code that uses the Google Books API to retrieve and display a book cover image for a given ISBN. The ISBN is supplied by preceding widget invocation with a suitable {{code| produced by Special:BookSources. That page removes punctuation from the supplied ISBN before generating, so the widget does not need to handle punctuated or other formatted ISBN numbers.

Book cover images are retrieved asynchronously, and after the page has loaded.

Parameters
The widget take no parameters directly. Instead, it takes its input ISBN from the value field of the directly preceding {{code| {{#widget:Book cover}} produces: . This is to ensure that they do not clash with any other JavaScript shipped by MediaWiki, either now or in future. The book cover code is in.

Overview

 * On loading the page,  retrieves the target ISBN, and if it passes validation, it starts an asynchronous call to the Google Books API to retrieve book data. Data is requested in JSON format.
 * When the Google Books API call completes, it changes the HTML content of the associated element to the book cover image, if one is available.
 * It does nothing if the ISBN fails validation, the API response does not contain a book cover, or if the API request times out or is throttled by Google. Because book cover images are 'decorative', there is no error indication for these cases.

Multiple transclusion

 * The JavaScript code finds the requested ISBN by searching the DOM for elements with class bh-book-cover. The value attribute contains the ISBN, and the text inside this element is where the book cover is displayed.
 * Caller code should place the required element just ahead of the JavaScript source. On page load, the JavaScript finds all existing and matching  elements, and then starts a Google Books API call for the last one found.
 * Because elements appearing later on in the DOM have not yet been parsed, this means that the last one found is always the one placed just ahead of this section of JavaScript. This indirect way of moving caller data into JavaScript works around MediaWiki's framework restrictions.

Portability

 * The code uses a relatively minimal set of JavaScript features. It tries to avoid using features that might not be present in some older browsers.

Widgets
Widgets are implemented as hooks into parser functions. Prepend '#' to invoke a widget, such as.

Widgets are coded using a template language known as "Smarty". Refer to the Smarty 3 Manual for the details. For the simple Javascript case, no template language syntax is needed.

Source code
The listing below shows the JavaScript source code for this widget: