// ==UserScript==
// @name          Saitama Prefectural Library Lookup from Amazon book listings.
// @namespace     http://www.amazon.co.jp
// @description   Saitama Prefectural Library Lookup from Amazon book listings.
// @include       http://*.amazon.*
// ==/UserScript==

// Version 20100307
libsearch(
  'https://www2.lib.city.saitama.jp/licsxp-opac/WOpacTifSchCmpdExecAction.do?hash=&returnid=&gamenid=tiles.WTifSchCmpd&chkflg=check&loccodschkflg=check&langcodschkflg=nocheck&condition1=0&condition1Text=&range1=0&mixing1=0&condition2=1&condition2Text=&range2=0&mixing2=0&condition3=6&condition3Text=',
  '&#x3055;&#x3044;&#x305F;&#x307e;&#x5e02;&#x56F3;&#x66F8;&#x9928;',
  'https://www2.lib.city.saitama.jp/licsxp-opac/WOpacMsgNewListToTifTilDetailAction.do?tilcod='
);

var isbn10 = '*';
var isbn13 = '*';

function libsearch( api, title, anchor ) {

  // get ISB10
  document.body.parentNode.innerHTML.match( /\s(4(\d{8}|-[\d-]{9}-)[\dX])/ );
  if ( RegExp.$1!='*' ) {
    isbn10 = RegExp.$1;
  }

  // get ISB13
  document.body.parentNode.innerHTML.match( /\s(978\-4(\d{8}|-[\d-]{9}-)[\dX])/ );
  if ( RegExp.$1!='*' ) {
    isbn13 = RegExp.$1;
  }

  // check library
  var header = document.evaluate( "//div[@id='priceBlock']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
  if ( header ) {
    checkLibrary( api, title, isbn10, header, anchor );
  }
}

// isbn10, isbn13の2回しか繰り返さない
var count = 2;
function checkLibrary( api, title, isbn, header, anchor ) {
  GM_xmlhttpRequest(
    {
      method  : "GET",
      url     : api + isbn,
      headers : {
                  'User-Agent'  : 'Mozilla/4.0 (compatible) Greasemonkey',
                  'Content-type': 'application/x-www-form-urlencoded'
                },
      onload  : function( response ) {
                  if ( count > 0 ) {
                    count = count - 1;
                    response.responseText.match(/tilcod=(\d{13})/);
                    if ( (RegExp.$1 != '*') && (RegExp.$1 != '') ) {
                      makelink( api, title, header, anchor, RegExp.$1 );
                    } else {
                      if ( isbn13!='*' ) {
                        checkLibrary( api, title, isbn13, header, anchor );
                      }
                    }
                  } else {
                    makelink( api, title, header, anchor, "" );
                  }
                }
    }
  );
  return 1;
}

function makelink( api, title, header, anchor, foungflg ) {
  var msg = ( foungflg ) ? '<b>&#x8535;&#x66f8;&#x3042;&#x308A;&#x3002;</b>' : '<b>&#x8535;&#x66F8;&#x4E0D;&#x660E;&#x3002;</b>';
  var spl_link = document.createElement( 'div' );
  spl_link.setAttribute( 'class', 'buying' );
  spl_link.innerHTML = '<span class=\"availGreen\" style=\"color:#ff0000;\">' + msg + '</span>&nbsp;';
  if ( foungflg ) {
    spl_link.innerHTML += '<a href=\"' + anchor + foungflg + '\" target=\"_blank\" title=\"' + title + '\">' + title + '</a>&#x3067;&#x8868;&#x793A;&nbsp;'
                        + '<a href=\"http://www.dokushogaku.com/\" target=\"_blank\" title=\"&#x3055;&#x3044;&#x305F;&#x307E;&#x5E02;&#x56F3;&#x66F8;&#x9928;&#x5F85;&#x3061;&#x4EBA;&#x6570\">'
                        + '<img src=\"http://www.dokushogaku.com/php/saitama/user.php?url=' + anchor + foungflg + '\" border=\"0\">'
                        + '</a></div>';
  } else {
    spl_link.innerHTML += '<a href=\"' + api + isbn10 + '\" target=\"_blank\" title=\"' + title + '\">' + title + '</a>&#x3067;&#x691C;&#x7D22;&#x3002;'
                        + '&#x7121;&#x3044;&#x306A;&#x3089;&#x3001;<a href=\"' + api + isbn13 + '\" target=\"_blank\" title=\"&#x3053;&#x3063;&#x3061;\">&#x3053;&#x3063;&#x3061;</a>&#x3002;</div>';
  }
  header.parentNode.insertBefore( spl_link, header.nextSibling );
}

