| 1 | // PLUGIN_INFO//{{{ |
|---|
| 2 | var PLUGIN_INFO = |
|---|
| 3 | <VimperatorPlugin> |
|---|
| 4 | <name>amazon_simple_uri</name> |
|---|
| 5 | <description>Copy Amazon Simple URI.</description> |
|---|
| 6 | <description lang="ja">シンプルなAmazon URIをクリップボードにコピーします。</description> |
|---|
| 7 | <author mail="from.kyushu.island@gmail.com" homepage="http://iddy.jp/profile/from_kyushu">from_kyushu</author> |
|---|
| 8 | <version>0.1</version> |
|---|
| 9 | <license>GPL</license> |
|---|
| 10 | <minVersion>1.2</minVersion> |
|---|
| 11 | <maxVersion>2.1</maxVersion> |
|---|
| 12 | <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/amazon_simple_uri.js</updateURL> |
|---|
| 13 | <detail><![CDATA[ |
|---|
| 14 | |
|---|
| 15 | == Option == |
|---|
| 16 | >|| |
|---|
| 17 | let g:amazon_asamashi = "hogehoge-22" |
|---|
| 18 | ||< |
|---|
| 19 | と設定することにより、Amazon アソシエイトID(上の例ではhogehoge-22)をURLに追加します。 |
|---|
| 20 | |
|---|
| 21 | ]]></detail> |
|---|
| 22 | </VimperatorPlugin>; |
|---|
| 23 | //}}} |
|---|
| 24 | // |
|---|
| 25 | (function() |
|---|
| 26 | { |
|---|
| 27 | // URLを正規表現でチェックしてISBNっぽい10桁,13桁の数字と取り出す |
|---|
| 28 | function getIsbn(uri) |
|---|
| 29 | { |
|---|
| 30 | var regex = new RegExp("([0-9]{9,13}[0-9Xx])"); |
|---|
| 31 | var match = uri.match(regex); |
|---|
| 32 | var isbn = match[0]; |
|---|
| 33 | if (isbn.length == 13) |
|---|
| 34 | { |
|---|
| 35 | isbn = getIsbn10(isbn); |
|---|
| 36 | } |
|---|
| 37 | return isbn; |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | // SSBのAPIを使ってISBNから書籍情報を取得 |
|---|
| 41 | function getBookInfo(isbn) |
|---|
| 42 | { |
|---|
| 43 | var uri = "http://stack.nayutaya.jp/api/book/"; |
|---|
| 44 | if (isbn.length == 10) |
|---|
| 45 | { |
|---|
| 46 | uri += "isbn10/" + isbn + ".json"; |
|---|
| 47 | } |
|---|
| 48 | else if (isbn.length == 13) |
|---|
| 49 | { |
|---|
| 50 | uri += "isbn13/" + isbn + ".json"; |
|---|
| 51 | } |
|---|
| 52 | var xhr = new XMLHttpRequest(); |
|---|
| 53 | xhr.open('GET', uri, false); |
|---|
| 54 | xhr.send(null); |
|---|
| 55 | if (xhr.status != 200) { |
|---|
| 56 | liberator.echoerr('false'); |
|---|
| 57 | return; |
|---|
| 58 | } |
|---|
| 59 | return window.eval('(' + xhr.responseText + ')'); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | //SSBのAPIから書籍情報を取り、そこからISBN10を取得 |
|---|
| 63 | function getIsbn10(isbn13) |
|---|
| 64 | { |
|---|
| 65 | var info = getBookInfo(isbn13); |
|---|
| 66 | return info.response.book.isbn10; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | commands.addUserCommand( |
|---|
| 70 | ['amazoncopy','asc'], |
|---|
| 71 | 'Copy Amazon Short URI', |
|---|
| 72 | function(args) |
|---|
| 73 | { |
|---|
| 74 | var asin = window.content.document.getElementById('ASIN'); |
|---|
| 75 | // ASINが取得できなかった場合 / Amazon以外の書籍情報ページから取得する場合 |
|---|
| 76 | if (asin == null) |
|---|
| 77 | { |
|---|
| 78 | asin = getIsbn(buffer.URL); |
|---|
| 79 | } |
|---|
| 80 | else |
|---|
| 81 | { |
|---|
| 82 | asin = asin.value |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | var uri = "http://www.amazon.co.jp/dp/" + asin + "/"; |
|---|
| 86 | var asamashi = typeof liberator.globalVariables.amazon_asamashi == "undefined" ? '' : liberator.globalVariables.amazon_asamashi; |
|---|
| 87 | if (args == "+a") |
|---|
| 88 | { |
|---|
| 89 | uri += asamashi; |
|---|
| 90 | } |
|---|
| 91 | util.copyToClipboard(uri); |
|---|
| 92 | liberator.echo("[ASC] Copy to clipboard."); |
|---|
| 93 | } |
|---|
| 94 | ); |
|---|
| 95 | })(); |
|---|