// PLUGIN_INFO//{{{ var PLUGIN_INFO = {NAME} character hint mode. hogelog 0.1.1 2.0pre 2008/12/12 2.0a1 2008/12/22 14:57:34 http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/char-hints-mod2.js ; //}}} (function () { const DEFAULT_HINTCHARS = "HJKLASDFGYUIOPQWERTNMZXCVB"; const hintContext = hints.addMode; let hintchars = DEFAULT_HINTCHARS; let inputCase = function(str) str.toUpperCase(); let inputRegex = /[A-Z]/; let showCase = function(str) str.toUpperCase(); function chars2num(chars) //{{{ { let num = 0; hintchars = inputCase(hintchars); let base = hintchars.length; for(let i=0,l=chars.length;i0); return chars; } //}}} function showCharHints() //{{{ { function showHints(win) { for(let elem in buffer.evaluateXPath("//*[@liberator:highlight and @number]", win.document)) { let num = elem.getAttribute("number"); let hintchar = num2chars(parseInt(num, 10)); elem.setAttribute("hintchar", showCase(hintchar)); if(isValidHint(hintchar)) validHints.push(elem); } Array.forEach(win.frames, showHints); } validHints = []; showHints(window.content); } //}}} function isValidHint(hint) //{{{ { if(hintInput.length == 0 ) return false; return inputCase(hint).indexOf(hintInput) == 0; } //}}} function setIOType(type) //{{{ { switch (type) { case "I": inputCase = function(str) str.toUpperCase(); inputRegex = /[A-Z]/; break; case "i": inputCase = function(str) str.toLowerCase(); inputRegex = /[a-z]/; break; case "O": showCase = function(str) str.toUpperCase(); break; case "o": showCase = function(str) str.toLowerCase(); break; } } //}}} var hintInput = ""; var validHints = []; var charhints = plugins.charhints = { show: function (minor, filter, win) //{{{ { charhints.original.show(minor, filter, win); hintInput = ""; showCharHints(); }, //}}} onInput: function (event) //{{{ { let eventkey = events.toString(event); if(/^\d$/.test(eventkey)) { commandline.command += eventkey; } let hintString = commandline.command; commandline.command = hintString.replace(inputRegex, ""); charhints.original.onInput(event); for(let i=0,l=hintString.length;i0) { let numstr = String(chars2num(hintInput)); // no setTimeout, don't run nice setTimeout(function () { for(let i=0,l=numstr.length;i133 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
/* 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.0</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>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/bitly.js</updateURL>
  <minVersion>2.0pre</minVersion>
  <maxVersion>2.4</maxVersion>
  <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(login);
          }
        );
      }
    );
  }

  function shorten (url, domain, command, callback) {
    function get () {
      let req = new XMLHttpRequest();
      req.onreadystatechange = function () {
        if (req.readyState != 4)
          return;
        if (req.status == 200)
          return callback && callback(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 && req.responseText.trim();
    }

    if (!url)
      url = buffer.URL;

    let auth = getAuth();

    if (auth)
      return get();

    if (callback) {
      setupAuth(get);
    } 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;
})();