User:Diberri/Template filler/googlebooks.wtf.autolink.user.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/* <nowiki> */
// ==UserScript==
// @name        Highlight ISBNs for Wikipedia template filler
// @namespace   http://diberri.dyndns.org/
// @description Links ISBNs found on Google Books pages to the Wikipedia template filler to generate a {{cite book}} template.
// @include     http://books.google.com/books?id=*
// ==/UserScript==

var template_name = "cite book";
var css_style = "border:1px solid #9999cc; background-color:#eeeeff; margin:5px; padding:5px; font-size:10pt; float:right";
var base_url = "http://diberri.dyndns.org/cgi-bin/templatefiller/index.cgi?type=isbn";

var conts = document.evaluate( "//td[@class='metadata_value']/span", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );

for( var i = 0; i < conts.snapshotLength; i++ ) {
  var cont = conts.snapshotItem(i);
  var text = cont.childNodes[0];
  var text_str = text.nodeValue;

  if( text_str.match( /([X\d]{10})/ ) ) {
    var id = RegExp.lastParen;
    var wtf_url = base_url+"&id="+id+"&add_ref_tag=1&add_text_url=1"

    var wtflink = document.createElement( cont.nodeName );
        wtflink.innerHTML = "<div class=\"wtflink\" style=\""+css_style+"\">Fill template: <a style=\"font-weight:bold\" href=\""+wtf_url+"\">{{"+template_name+"}}</a></div>";

    var p = document.getElementById("summary_content");
        p.insertBefore( wtflink, p.firstChild );
  }
}
/* </nowiki> */