aboutsummaryrefslogtreecommitdiffstats
path: root/bitly.js
blob: 08d004cd64db82a117a1ff1336ff89c2cc746948 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// ==VimperatorPlugin==
// @name           Bit.ly
// @description-ja Bit.ly で短縮URLを得る
// @license        Creative Commons 2.1 (Attribution + Share Alike)
// @version        1.0
// @minVersion     1.2
// @maxVersion     2.0pre
// ==/VimperatorPlugin==
//

(function () {

  function bitly (uri, callback) {
    let req = new XMLHttpRequest();
    req.onreadystatechange = function () {
      if (req.readyState != 4)
        return;
      if (req.status == 200)
        return callback && callback(req.responseText, req);
      else
        throw new Error(req.statusText);
    };
    req.open('GET', 'http://bit.ly/api?url=' + uri, callback);
    req.send(null);
    return !callback && req.responseText;
  }

  // commands.removeUserCommand('bitly');
  commands.addUserCommand(
    ['bitly'],
    'Copy bitly url',
    function () {
      bitly(buffer.URL, function (short) {
        alert(short);
        util.copyToClipboard(short);
        liberator.echo('`' + short + "' was copied to clipboard.");
      });
    }
  );

  // 外から使えるように
  liberator.plugins.bitly = {
    get: bitly
  };

})();