/* NEW BSD LICENSE {{{
-Copyright (c) 2008, anekos.
+Copyright (c) 2008-2010, anekos.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
}}} */
// PLUGIN_INFO {{{
-let PLUGIN_INFO =
+let PLUGIN_INFO = xml`
<VimperatorPlugin>
<name>bit.ly</name>
- <description>Get short alias by bit.ly</description>
- <description lang="ja">Bit.ly で短縮URLを得る</description>
- <version>1.1.0</version>
+ <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>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/bitly.js</updateURL>
+ <updateURL>https://github.com/vimpr/vimperator-plugins/raw/master/bitly.js</updateURL>
<minVersion>2.0pre</minVersion>
- <maxVersion>2.0pre</maxVersion>
<detail><![CDATA[
== Commands ==
:bitly [<URL>]
Copy to clipboard.
+ :jmp [<URL>]
+ Copy to clipboard.
+ == Require ==
+ bit.ly API Key
]]></detail>
-</VimperatorPlugin>;
+</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;
+ 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.');
+ }
}
- commands.addUserCommand(
- ['bitly'],
- 'Copy bitly url',
- function (args) {
- bitly(args.literalArg || buffer.URL, function (short) {
- util.copyToClipboard(short);
- liberator.echo('`' + short + "' was copied to clipboard.");
- });
- },
- {
- literal: 0
- },
- true
- );
-
- // 外から使えるように
- liberator.plugins.bitly = {
- get: bitly
- };
+ [
+ ['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;
})();