aboutsummaryrefslogtreecommitdiffstats
path: root/bitly.js
blob: 970376ff3d9138f3109bb8cfd8a4cd5e1454b30d (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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/* NEW BSD LICENSE {{{
Copyright (c) 2008-2010, anekos.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

    1. Redistributions of source code must retain the above copyright notice,
       this list of conditions and the following disclaimer.
    2. Redistributions in binary form must reproduce the above copyright notice,
       this list of conditions and the following disclaimer in the documentation
       and/or other materials provided with the distribution.
    3. The names of the authors may not be used to endorse or promote products
       derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
THE POSSIBILITY OF SUCH DAMAGE.


###################################################################################
# http://sourceforge.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license       #
# に参考になる日本語訳がありますが、有効なのは上記英文となります。                #
###################################################################################

}}} */

// PLUGIN_INFO {{{
let PLUGIN_INFO =
<VimperatorPlugin>
  <name>bit.ly</name>
  <description>Get short alias by bit.ly and j.mp</description>
  <description lang="ja">bit.ly  j.mp で短縮URLを得る</description>
  <version>2.1.2</version>
  <author mail="anekos@snca.net" homepage="http://d.hatena.ne.jp/nokturnalmortum/">anekos</author>
  <license>new BSD License (Please read the source code comments of this plugin)</license>
  <license lang="ja">修正BSDライセンス (ソースコードのコメントを参照してください)</license>
  <updateURL>https://github.com/vimpr/vimperator-plugins/raw/master/bitly.js</updateURL>
  <minVersion>2.0pre</minVersion>
  <detail><![CDATA[
    == Commands ==
      :bitly [<URL>]
        Copy to clipboard.
      :jmp [<URL>]
        Copy to clipboard.
    == Require ==
      bit.ly API Key
  ]]></detail>
</VimperatorPlugin>;
// }}}


(function () {

  const Realm = 'API Key for bit.ly (bitly.js)';
  const HostName = 'http://api.bit.ly';
  const ApiUrl = 'http://api.bit.ly/v3';
  const PasswordManager = Cc['@mozilla.org/login-manager;1'].getService(Ci.nsILoginManager);
  const LoginInfo =
    new Components.Constructor(
      '@mozilla.org/login-manager/loginInfo;1',
      Ci.nsILoginInfo,
      'init'
    );

  function getAuth () {
    let count = {};
    let logins = PasswordManager.findLogins(count, HostName, null, Realm);
    if (logins.length)
      return logins[0];
  }

  function setupAuth (callback) {
    liberator.open('http://bit.ly/a/your_api_key', liberator.NEW_TAB);
    commandline.input(
      'Login name for bit.ly: ',
      function (username) {
        commandline.input(
          'API Key: ',
          function (apiKey) {
            let login = LoginInfo(HostName, null, Realm, username, apiKey, '', '');
            PasswordManager.addLogin(login);
            callback();
          },
          {
            default: let (e = content.document.querySelector('#bitly_api_key')) (e ? e.value : '')
          }
        );
      }
    );
  }

  function shorten (url, domain, command, callback) {
    function fixResponseText (s)
      s.trim();

    liberator.log(arguments);
    function get () {
      let req = new XMLHttpRequest();
      req.onreadystatechange = function () {
        if (req.readyState != 4)
          return;
        if (req.status == 200)
          return callback && callback(fixResponseText(req.responseText), req);
        else
          return liberator.echoerr(req.statusText);
      };
      let requestUri =
        ApiUrl + '/' + (command || 'shorten') + '?' +
        'apiKey=' + auth.password + '&' +
        'login=' + auth.username + '&' +
        (command !== 'expand' ? 'uri=' : 'shortUrl=') + encodeURIComponent(url) + '&' +
        'domain=' + (domain || 'bit.ly') + '&' +
        'format=txt';
      req.open('GET', requestUri, callback);
      req.send(null);
      return !callback && fixResponseText(req.responseText);
    }

    if (!url)
      url = buffer.URL;

    let auth = getAuth();

    if (auth)
      return get();

    if (callback) {
      let args = Array.slice(arguments);
      setupAuth(function () shorten.apply(this, args));
    } else {
      liberator.echoerr('Not found API Key!! Try :bitly command, before use.');
    }
  }

  [
    ['jmp', 'j.mp'],
    ['bitly', 'bit.ly'],
  ].forEach(function ([name, domain]) {
    commands.addUserCommand(
      [name],
      'Copy ' + domain + ' url',
      function (args) {
        let url = args.literalArg ? util.stringToURLArray(args.literalArg)[0] : buffer.URL;
        let cmd = args['-expand'] ? 'expand' : 'shorten';

        shorten(url, domain, cmd, function (short) {
          util.copyToClipboard(short);
          liberator.echo(short + ' <= ' + url);
        });
      },
      {
        literal: 0,
        options: [
          [['-expand', '-e'], commands.OPTION_NOARG]
        ],
        completer: function (context) {
          context.completions = [
            [buffer.URL, 'Current URL']
          ];
          context.fork('URL', 0, context, completion.url);
        }
      },
      true
    );
    __context__[name] = function (url, cmd, callback) shorten(url, domain, cmd, callback);
  });

  __context__.get = shorten;
})();
ly the numerical part of the hint chh.usedTabKey = false; // when we used <Tab> to select an element chh.hints = []; chh.validHints = []; // store the indices of the "hints" array with valid elements chh.activeTimeout = null; // needed for hinttimeout > 0 chh.canUpdate = false; // used in number2hintchars chh.transval = {"0":0, "1":1, "2":2, "3":3, "4":4, "5":5, "6":6, "7":7, "8":8, "9":9, "a":10, "b":11, "c":12, "d":13,"e":14, "f":15, "g":16, "h":17, "i":18, "j":19, "k":20, "l":21, "m":22, "n":23, "o":24, "p":25,"q":26, "r":27, "s":28, "t":29, "u":30, "v":31, "w":32, "x":33, "y":34, "z":35}; // used in hintchars2number chh.conversion = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // keep track of the documents which we generated the hints for // docs = { doc: document, start: start_index in hints[], end: end_index in hints[] } chh.docs = []; //}}} // reset all important variables chh.reset = function ()//{{{ { liberator.statusline.updateInputBuffer(""); chh.hintString = ""; chh.hintNumber = 0; chh.usedTabKey = false; chh.hints = []; chh.validHints = []; chh.canUpdate = false; chh.docs = []; if (chh.activeTimeout) clearTimeout(chh.activeTimeout); chh.activeTimeout = null; } //}}} chh.updateStatusline = function ()//{{{ { liberator.statusline.updateInputBuffer(("") + (chh.hintString ? "\"" + chh.hintString + "\"" : "") + (chh.hintNumber > 0 ? " <" + chh.hintNumber + ">" : "")); } //}}} // this function 'click' an element, which also works // for javascript links chh.hintchars2number = function (hintstr)//{{{ { // convert into 'normal number then make it decimal-based var converted = ""; // translate users hintchars into a number (chh.conversion) 0 -> 0, 1 -> 1, ... for (var i = 0, l = hintstr.length; i < l; i++) converted += "" + chh.conversion[chh.hintchars.indexOf(hintstr[i])]; // add one, since hints begin with 0; return parseInt(converted, chh.hintchars.length); // hintchars.length is the base/radix } //}}} chh.number2hintchars = function (nr)//{{{ { var oldnr = nr; var converted = ""; var tmp = ""; tmp = nr.toString(chh.hintchars.length); // hintchars.length is the base/radix) // translate numbers into users hintchars // tmp might be 2e -> (chh.transval) 2 and 14 -> (chh.hintchars) according hintchars for (var i = 0, l = tmp.length; i < l; i++) converted += "" + chh.hintchars[chh.transval[tmp[i]]]; return converted; } //}}} chh.openHint = function (where)//{{{ { if (chh.validHints.length < 1) return false; var x = 1, y = 1; var elem = chh.validHints[chh.hintNumber - 1] || chh.validHints[0]; var elemTagName = elem.localName.toLowerCase(); elem.focus(); liberator.buffer.followLink(elem, where); return true; } //}}} chh.focusHint = function ()//{{{ { if (chh.validHints.length < 1) return false; var elem = chh.validHints[chh.hintNumber - 1] || chh.validHints[0]; var doc = window.content.document; var elemTagName = elem.localName.toLowerCase(); if (elemTagName == "frame" || elemTagName == "iframe") { elem.contentWindow.focus(); return false; } else { elem.focus(); } var evt = doc.createEvent("MouseEvents"); var x = 0; var y = 0; // for imagemap if (elemTagName == "area") { [x, y] = elem.getAttribute("coords").split(","); x = Number(x); y = Number(y); } evt.initMouseEvent("mouseover", true, true, doc.defaultView, 1, x, y, 0, 0, 0, 0, 0, 0, 0, null); elem.dispatchEvent(evt); } //}}} chh.yankHint = function (text)//{{{ { if (chh.validHints.length < 1) return false; var elem = chh.validHints[chh.hintNumber - 1] || chh.validHints[0]; if (text) var loc = elem.textContent; else var loc = elem.href; liberator.copyToClipboard(loc); liberator.echo("Yanked " + loc, liberator.commandline.FORCE_SINGLELINE); } //}}} chh.saveHint = function (skipPrompt)//{{{ { if (chh.validHints.length < 1) return false; var elem = chh.validHints[chh.hintNumber - 1] || chh.validHints[0]; try { liberator.buffer.saveLink(elem,skipPrompt); } catch (e) { liberator.echoerr(e); } } //}}} chh.generate = function (win)//{{{ { var startDate = Date.now(); if (!win) win = window.content; var doc = win.document; var height = win.innerHeight; var width = win.innerWidth; var scrollX = doc.defaultView.scrollX; var scrollY = doc.defaultView.scrollY; var baseNodeAbsolute = doc.createElementNS("http://www.w3.org/1999/xhtml", "span"); baseNodeAbsolute.style.backgroundColor = "red"; baseNodeAbsolute.style.color = "white"; baseNodeAbsolute.style.position = "absolute"; baseNodeAbsolute.style.fontSize = "10px"; baseNodeAbsolute.style.fontWeight = "bold"; baseNodeAbsolute.style.lineHeight = "10px"; baseNodeAbsolute.style.padding = "0px 1px 0px 0px"; baseNodeAbsolute.style.zIndex = "10000001"; baseNodeAbsolute.style.display = "none"; baseNodeAbsolute.className = "vimperator-hint"; var elem, tagname, text, span, rect; var res = liberator.buffer.evaluateXPath(chh.hinttags, doc, null, true); liberator.log("shints: evaluated XPath after: " + (Date.now() - startDate) + "ms"); var fragment = doc.createDocumentFragment(); var start = chh.hints.length; while ((elem = res.iterateNext()) != null) { // TODO: for frames, this calculation is wrong rect = elem.getBoundingClientRect(); if (!rect || rect.top > height || rect.bottom < 0 || rect.left > width || rect.right < 0) continue; rect = elem.getClientRects()[0]; if (!rect) continue; // TODO: mozilla docs recommend localName instead of tagName tagname = elem.tagName.toLowerCase(); text = ""; span = baseNodeAbsolute.cloneNode(true); span.style.left = (rect.left + scrollX) + "px"; span.style.top = (rect.top + scrollY) + "px"; fragment.appendChild(span); chh.hints.push([elem, text, span, null, elem.style.backgroundColor, elem.style.color]); } doc.body.appendChild(fragment); chh.docs.push({ doc: doc, start: start, end: chh.hints.length - 1 }); // also generate hints for frames for (var i = 0; i < win.frames.length; i++) chh.generate(win.frames[i]); liberator.log("shints: generate() completed after: " + (Date.now() - startDate) + "ms"); return true; } //}}} // TODO: make it aware of imgspans chh.showActiveHint = function (newID, oldID)//{{{ { var oldElem = chh.validHints[oldID - 1]; if (oldElem) oldElem.style.backgroundColor = chh.bgcolor; var newElem = chh.validHints[newID - 1]; if (newElem) newElem.style.backgroundColor = chh.selcolor; } //}}} chh.showHints = function ()//{{{ { var startDate = Date.now(); var win = window.content; var height = win.innerHeight; var width = win.innerWidth; var elem, tagname, text, rect, span, imgspan; var hintnum = 1; //var findTokens = chh.hintString.split(/ +/); var activeHint = chh.hintNumber || 1; chh.validHints = []; for (var j = 0; j < chh.docs.length; j++) { var doc = chh.docs[j].doc; var start = chh.docs[j].start; var end = chh.docs[j].end; var scrollX = doc.defaultView.scrollX; var scrollY = doc.defaultView.scrollY; outer: for (let i = start; i <= end; i++) { [elem, , span, imgspan] = chh.hints[i]; text = ""; if (elem.firstChild && elem.firstChild.tagName == "IMG") { if (!imgspan) { rect = elem.firstChild.getBoundingClientRect(); if (!rect) continue; imgspan = doc.createElementNS("http://www.w3.org/1999/xhtml", "span"); imgspan.style.position = "absolute"; imgspan.style.opacity = 0.5; imgspan.style.zIndex = "10000000"; imgspan.style.left = (rect.left + scrollX) + "px"; imgspan.style.top = (rect.top + scrollY) + "px"; imgspan.style.width = (rect.right - rect.left) + "px"; imgspan.style.height = (rect.bottom - rect.top) + "px"; imgspan.className = "vimperator-hint"; chh.hints[i][3] = imgspan; doc.body.appendChild(imgspan); } imgspan.style.backgroundColor = (activeHint == hintnum) ? chh.selcolor : chh.bgcolor; imgspan.style.display = "inline"; } if (!imgspan) elem.style.backgroundColor = (activeHint == hintnum) ? chh.selcolor : chh.bgcolor; elem.style.color = chh.fgcolor; if (chh.showcapitals) span.textContent = chh.number2hintchars(hintnum++).toUpperCase(); else span.textContent = chh.number2hintchars(hintnum++); span.style.display = "inline"; chh.validHints.push(elem); } } liberator.log("shints: showHints() completed after: " + (Date.now() - startDate) + "ms"); return true; } //}}} chh.removeHints = function (timeout)//{{{ { var firstElem = chh.validHints[0] || null; var firstElemselcolor = ""; var firstElemColor = ""; for (var j = 0; j < chh.docs.length; j++) { var doc = chh.docs[j].doc; var start = chh.docs[j].start; var end = chh.docs[j].end; for (let i = start; i <= end; i++) { // remove the span for the numeric display part doc.body.removeChild(chh.hints[i][2]); if (chh.hints[i][3]) // a transparent span for images doc.body.removeChild(chh.hints[i][3]); if (timeout && firstElem == chh.hints[i][0]) { firstElemselcolor = chh.hints[i][4]; firstElemColor = chh.hints[i][5]; } else { // restore colors var elem = chh.hints[i][0]; elem.style.backgroundColor = chh.hints[i][4]; elem.style.color = chh.hints[i][5]; } } // animate the disappearance of the first hint if (timeout && firstElem) { setTimeout(function () { firstElem.style.backgroundColor = firstElemselcolor; firstElem.style.color = firstElemColor; }, timeout); } } liberator.log("shints: removeHints() done"); chh.reset(); } //}}} chh.processHints = function (followFirst)//{{{ { if (chh.validHints.length == 0) { liberator.beep(); return false; } if (!followFirst) { var firstHref = chh.validHints[0].getAttribute("href") || null; if (firstHref) { if (chh.validHints.some(function (e) { return e.getAttribute("href") != firstHref; })) return false; } else if (chh.validHints.length > 1) return false; } var activeNum = chh.hintNumber || 1; var loc = chh.validHints[activeNum - 1].href || ""; switch (chh.submode) { case ";": chh.focusHint(); break; case "a": chh.saveHint(false); break; case "s": chh.saveHint(true); break; case "o": chh.openHint(liberator.CURRENT_TAB); break; case "O": liberator.commandline.open(":", "open " + loc, liberator.modes.EX); break; case "t": chh.openHint(liberator.NEW_TAB); break; case "T": liberator.commandline.open(":", "tabopen " + loc, liberator.modes.EX); break; case "w": chh.openHint(liberator.NEW_WINDOW); break; case "W": liberator.commandline.open(":", "winopen " + loc, liberator.modes.EX); break; case "y": chh.yankHint(false); break; case "Y": chh.yankHint(true); break; default: liberator.echoerr("INTERNAL ERROR: unknown submode: " + chh.submode); } var timeout = followFirst ? 0 : 500; chh.removeHints(timeout); if (liberator.modes.extended & liberator.modes.ALWAYS_HINT) { setTimeout(function () { chh.canUpdate = true; chh.hintString = ""; chh.hintNumber = 0; liberator.statusline.updateInputBuffer(""); }, timeout); } else { if (timeout == 0 || liberator.modes.isReplaying) { // force a possible mode change, based on wheter an input field has focus liberator.events.onFocusChange(); if (liberator.mode == liberator.modes.HINTS) liberator.modes.reset(false); } else { liberator.modes.add(liberator.modes.INACTIVE_HINT); setTimeout(function () { if (liberator.mode == liberator.modes.HINTS) liberator.modes.reset(false); }, timeout); } } return true; } //}}} // TODO: implement framesets chh.show = function (mode, minor, filter)//{{{ { if (mode == liberator.modes.EXTENDED_HINT && !/^[;asoOtTwWyY]$/.test(minor)) { liberator.beep(); return; } liberator.modes.set(liberator.modes.HINTS, mode); chh.submode = minor || "o"; // open is the default mode chh.hintString = filter || ""; chh.hintNumber = 0; chh.canUpdate = false; chh.generate(); // get all keys from the input queue var mt = Components.classes["@mozilla.org/thread-manager;1"].getService().mainThread; while (mt.hasPendingEvents()) mt.processNextEvent(true); chh.canUpdate = true; chh.showHints(); if (chh.validHints.length == 0) { liberator.beep(); liberator.modes.reset(); return false; } else if (chh.validHints.length == 1) { chh.processHints(true); return false; } else // still hints visible return true; } //}}} chh.hide = function ()//{{{ { chh.removeHints(0); } //}}} chh.onEvent = function (event)//{{{ { var key = liberator.events.toString(event); if (chh.showcapitals && key.length == 1) key = key.toLowerCase(); // clear any timeout which might be active after pressing a number if (chh.activeTimeout) { clearTimeout(chh.activeTimeout); chh.activeTimeout = null; } switch (key) { case "<Return>": chh.processHints(true); break; case "<Tab>": case "<S-Tab>": chh.usedTabKey = true; if (chh.hintNumber == 0) chh.hintNumber = 1; var oldID = chh.hintNumber; if (key == "<Tab>") { if (++chh.hintNumber > chh.validHints.length) chh.hintNumber = 1; } else { if (--chh.hintNumber < 1) chh.hintNumber = chh.validHints.length; } chh.showActiveHint(chh.hintNumber, oldID); return; case "<BS>": //TODO: may tweak orig hints.js too (adding 2 lines ...) var oldID = chh.hintNumber; if (chh.hintNumber > 0) { chh.hintNumber = Math.floor(chh.hintNumber / chh.hintchars.length); chh.hintString = chh.hintString.substr(0, chh.hintString.length - 1); chh.usedTabKey = false; } else { chh.usedTabKey = false; chh.hintNumber = 0; liberator.beep(); return; } chh.showActiveHint(chh.hintNumber, oldID); break; case "<C-w>": case "<C-u>": chh.hintString = ""; chh.hintNumber = 0; break; default: // pass any special or ctrl- etc. prefixed key back to the main vimperator loop if (/^<./.test(key) || key == ":") { //FIXME: won't work probably var map = null; if ((map = liberator.mappings.get(liberator.modes.NORMAL, key)) || (map = liberator.mappings.get(liberator.modes.HINTS, key))) //TODO { map.execute(null, -1); return; } liberator.beep(); return; } if (chh.hintchars.indexOf(key) >= 0) // TODO: check if in hintchars { chh.hintString += key; var oldHintNumber = chh.hintNumber; if (chh.hintNumber == 0 || chh.usedTabKey) { chh.usedTabKey = false; } chh.hintNumber = chh.hintchars2number(chh.hintString); chh.updateStatusline(); if (!chh.canUpdate) return; if (chh.docs.length == 0) { chh.generate(); chh.showHints(); } chh.showActiveHint(chh.hintNumber, oldHintNumber || 1); if (chh.hintNumber == 0 || chh.hintNumber > chh.validHints.length) { liberator.beep(); return; } // orig hints.js comment: if we write a numeric part like 3, but we have 45 hints, only follow // the hint after a timeout, as the user might have wanted to follow link 34 if (chh.hintNumber > 0 && chh.hintNumber * chh.hintchars.length <= chh.validHints.length) { if (chh.timeout > 0) chh.activeTimeout = setTimeout(function () { chh.processHints(true); }, chh.timeout); return false; } // we have a unique hint chh.processHints(true); return; } if (chh.usedTabKey) { chh.usedTabKey = false; chh.showActiveHint(1, chh.hintNumber); } } chh.updateStatusline(); }//}}} // <<<<<<<<<<<<<<< registering/setting up this plugin //liberator.modes.setCustomMode ("CHAR-HINTS", liberator.plugins.charhints.onEvent, // liberator.plugins.charhints.hide); liberator.hints = chh; liberator.mappings.addUserMap([liberator.modes.NORMAL], [chh.mapNormal], "Start Custum-QuickHint mode", function () { liberator.plugins.charhints.show(liberator.modes.QUICK_HINT); }, { noremap: true } ); liberator.mappings.addUserMap([liberator.modes.NORMAL], [chh.mapNormalNewTab], "Start Custum-QuickHint mode, but open link in a new tab", function () { liberator.plugins.charhints.show(liberator.modes.QUICK_HINT, "t"); }, { noremap: true } ); liberator.mappings.addUserMap([liberator.modes.NORMAL], [chh.mapExtended], "Start an extended hint mode", function (arg) { if (arg == "f") liberator.plugins.charhints.show(liberator.modes.ALWAYS_HINT, "o"); else if (arg == "F") liberator.plugins.charhints.show(liberator.modes.ALWAYS_HINT, "t"); else liberator.plugins.charhints.show(liberator.modes.EXTENDED_HINT, arg); }, { flags: liberator.Mappings.flags.ARGUMENT, noremap: true } ); // vim: set fdm=marker sw=4 ts=4 et: