Created
August 18, 2009 13:30
-
-
Save pi8027/169715 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
urn:isbn: なリンクを Amazon.co.jp へのリンクに変換するスクリプト | |
*/ | |
document.addEventListener('DOMContentLoaded',isbn2amazon,false); | |
function isbn2amazon() | |
{ | |
var links = document.getElementsByTagName('a'); | |
var uri = 'http://www.amazon.co.jp/exec/obidos/ASIN/'; | |
var isbn; | |
var id = 'pi8027-22'; | |
var i = 0; | |
while(i != links.length){ | |
if(links[i].href.match(/^urn:isbn:/i)){ | |
isbn = check_and_normalize_isbn | |
(links[i].href.replace(/^urn:isbn:/i,'')); | |
if(isbn != ''){ | |
links[i].setAttribute('href',uri+isbn+'/'+id+'/ref=nosim'); | |
} | |
} | |
i++; | |
} | |
} | |
function check_and_normalize_isbn(string) | |
{ | |
var isbn = string.replace(/-/g, ''); | |
var sum; | |
if(isbn.match(/^\d{9}(\d|X)$/)){ | |
return isbn; | |
} | |
else if(isbn.match(/^978\d{10}$/)){ | |
isbn = isbn.substring(3,13); | |
sum = (11-(isbn[0]*10+isbn[1]*9+isbn[2]*8+isbn[3]*7+isbn[4]*6 | |
+isbn[5]*5+isbn[6]*4+isbn[7]*3+isbn[8]*2)%11)%11; | |
isbn = isbn.replace(/.$/,(sum != 10)?sum | |
:'X'); | |
return isbn; | |
} | |
else{ | |
return ''; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment