// ==UserScript==
// @name Itabashi Prefectural Library Lookup from Amazon book listings.
// @namespace http://www.amazon.co.jp
// @description Itabashi Prefectural Library Lookup from Amazon book listings.
// @include http://*.amazon.*
// ==/UserScript==
// Version 20070430
libsearch(
'http://www.lib.city.itabashi.tokyo.jp/ITWLIB/servlet/search.result?code_genre1=2&code_value1=',
'板橋区図書館'
);
function libsearch( api, title ) {
// get ISB10
document.body.parentNode.innerHTML.match( /\s(4(\d{8}|-[\d-]{9}-)[\dX])/ );
var isbn = '';
if ( RegExp.$1!='' ) {
isbn = RegExp.$1
// get ISB13
} else {
document.body.parentNode.innerHTML.match( /\s(978\-4(\d{8}|-[\d-]{9}-)[\dX])/ );
if ( RegExp.$1!='' ) {
isbn = RegExp.$1
}
}
// check library
var header = document.evaluate( "//b[@class='sans']", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null ).singleNodeValue;
if ( header ) {
if ( isbn ) {
checkLibrary( api, title, isbn, header );
} else {
makelink( api, title, '', header, 0 );
}
}
}
function checkLibrary( api, title, isbn, header ) {
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 ) {
makelink( api, title, isbn, header, response.responseText.match(/detail_list/i) );
}
}
);
}
function makelink( api, title, isbn, header, foungflg ) {
var msg = ( foungflg ) ? '★蔵書あり★' : '蔵書なし...';
var spl_link = document.createElement( 'a' );
spl_link.setAttribute( 'target', '_blank' );
spl_link.setAttribute( 'href', api + isbn );
spl_link.setAttribute( 'title', 'To Saitama Municipal Library' );
spl_link.innerHTML = '
» [' + msg + '] ' + title + 'で検索 ';
header.parentNode.insertBefore( spl_link, header.nextSibling );
}