LoginSignup
4
4

More than 5 years have passed since last update.

vimperatorプラグインcopy.jsで、いま開いてるamazonの本のページの短いURLをクリップボードにコピる。あと正規表現で好きなとこコピる

Last updated at Posted at 2012-06-25

copy.jsは、今開いてるページの好きなところをクリップボードにワンタッチでコピーできるvimperatorプラグイン。
デフォルトでは、URL、ページタイトルなどがコピーできるようになってる。

以下にあるとおり、いろいろカスタマイズできる。
http://d.hatena.ne.jp/teramako/20080621/p1

amazonの長ったらしい日本語まじりURLを、短いURLにしてクリップボードにコピーする設定を書いてみた。

以下をvimperatorrcに追加

javascript <<EOM
(function () {
  liberator.globalVariables.copy_templates = [
    { label: 'titleAndURL',    value: '%TITLE%\n%URL%' },
    { label: 'title',          value: '%TITLE%', map: ',y' },
    { label: 'anchor',         value: '<a href="%URL%">%TITLE%</a>' },
    { label: 'selanchor',      value: '<a href="%URL%" title="%TITLE%">%SEL%</a>' },
    { label: 'htmlblockquote', value: '<blockquote cite="%URL%" title="%TITLE%">%HTMLSEL%</blockquote>' },

    // ↑ここまでデフォルであるやついろいろ。ここからamazon↓
    { label: 'amazon',   value: 'copy clean amazon url from current page',
      custom: function() {
        var m = content.document.location.pathname.match(/dp\/(\d+)/);
        return m ? ('http://amazon.jp/dp/' + m[1]) : null;
      }
    },
  ];
})();
EOM

.vimperatorrcファイル上では、javascriptコマンドに文字列を食わすと実行できる。
vimperatorのコンテキストで実行されるため、documentオブジェクトにさわるには、content.document と書かないといけないので注意。

無名関数を渡せるので割となんでもできて便利。
以下は、ページ内のコンテンツのうち、正規表現にまっちする文字列をコピーする設定

    { label: 'PA', value: 'copy account_id from CMSP', map: ',pa',
      custom: function() {
        var regexp = /PA\d{8}/;

        return (function (e, re) {
          var m = e.textContent.match(re);
          if (m) return m[0];

          for (var i = 0; i < e.children.length; i++) {
            var result = arguments.callee(e.children[i], re);
            if (result) return result;
          }
          return null;
        })(content.document.body, regexp);
      }}

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4