From 9c5a7d92bab0338ec0752f08449d634560a85a6c Mon Sep 17 00:00:00 2001 From: from_kyushu Date: Tue, 9 Jun 2009 06:43:18 +0000 Subject: Add amazon_simple_uri.js git-svn-id: http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk@33860 d0d07461-0603-4401-acd4-de1884942a52 --- amazon_simple_uri.js | 95 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 amazon_simple_uri.js (limited to 'amazon_simple_uri.js') diff --git a/amazon_simple_uri.js b/amazon_simple_uri.js new file mode 100644 index 0000000..8f55c1f --- /dev/null +++ b/amazon_simple_uri.js @@ -0,0 +1,95 @@ +// PLUGIN_INFO//{{{ +var PLUGIN_INFO = + + amazon_simple_uri + Copy Amazon Simple URI. + シンプルなAmazon URIをクリップボードにコピーします。 + from_kyushu + 0.1 + GPL + 1.2 + 2.1 + http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/amazon_simple_uri.js + || + let g:amazon_asamashi = "hogehoge-22" +||< +と設定することにより、Amazon アソシエイトID(上の例ではhogehoge-22)をURLに追加します。 + + ]]> +; +//}}} +// +(function() +{ + // URLを正規表現でチェックしてISBNっぽい10桁,13桁の数字と取り出す + function getIsbn(uri) + { + var regex = new RegExp("([0-9]{9,13}[0-9Xx])"); + var match = uri.match(regex); + var isbn = match[0]; + if (isbn.length == 13) + { + isbn = getIsbn10(isbn); + } + return isbn; + } + + // SSBのAPIを使ってISBNから書籍情報を取得 + function getBookInfo(isbn) + { + var uri = "http://stack.nayutaya.jp/api/book/"; + if (isbn.length == 10) + { + uri += "isbn10/" + isbn + ".json"; + } + else if (isbn.length == 13) + { + uri += "isbn13/" + isbn + ".json"; + } + var xhr = new XMLHttpRequest(); + xhr.open('GET', uri, false); + xhr.send(null); + if (xhr.status != 200) { + liberator.echoerr('false'); + return; + } + return window.eval('(' + xhr.responseText + ')'); + } + + //SSBのAPIから書籍情報を取り、そこからISBN10を取得 + function getIsbn10(isbn13) + { + var info = getBookInfo(isbn13); + return info.response.book.isbn10; + } + + commands.addUserCommand( + ['amazoncopy','asc'], + 'Copy Amazon Short URI', + function(args) + { + var asin = window.content.document.getElementById('ASIN'); + // ASINが取得できなかった場合 / Amazon以外の書籍情報ページから取得する場合 + if (asin == null) + { + asin = getIsbn(buffer.URL); + } + else + { + asin = asin.value + } + + var uri = "http://www.amazon.co.jp/dp/" + asin + "/"; + var asamashi = typeof liberator.globalVariables.amazon_asamashi == "undefined" ? '' : liberator.globalVariables.amazon_asamashi; + if (args == "+a") + { + uri += asamashi; + } + util.copyToClipboard(uri); + liberator.echo("[ASC] Copy to clipboard."); + } + ); +})(); -- cgit v1.2.3