aboutsummaryrefslogtreecommitdiffstats
path: root/simg.js
AgeCommit message (Collapse)Author
2010-11-12cut off obstructive http's parametermitsugu oyama
For example, http://hoge.com/path/to/hogehoge.jpg?id=hogehogehogehoge&page=2 Should drop '?id=hogehogehogehoge&page=2'
2010-11-08modified summarymitsugu oyama
2010-10-25modified href attributemitsugu oyama
2010-10-23Add simg.jsmitsugu oyama
コンテキスト・エリアに表示されている画像を保存するよ。 コンテキスト・メニューの操作より、キーボードのホーム・ポジションを愛する人に
id='n64' href='#n64'>64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
// PLUGIN_INFO//{{{
var PLUGIN_INFO =
<VimperatorPlugin>
  <name>amazon_simple_uri</name>
  <description>Copy Amazon Simple URI.</description>
  <description lang="ja">シンプルなAmazon URIをクリップボードにコピーします</description>
  <author mail="from.kyushu.island@gmail.com" homepage="http://iddy.jp/profile/from_kyushu">from_kyushu</author>
  <version>0.1</version>
  <license>GPL</license>
  <minVersion>1.2</minVersion>
  <maxVersion>2.1</maxVersion>
  <updateURL>https://github.com/vimpr/vimperator-plugins/raw/master/amazon_simple_uri.js</updateURL>
  <detail><![CDATA[

== Option ==
>||
  let g:amazon_asamashi = "hogehoge-22"
||<
と設定することによりAmazon アソシエイトID(上の例ではhogehoge-22)をURLに追加します

  ]]></detail>
</VimperatorPlugin>;
//}}}
//
(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.");
    }
  );
})();