2 Copyright (c) 2008-2010, anekos.
5 Redistribution and use in source and binary forms, with or without modification,
6 are permitted provided that the following conditions are met:
8 1. Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright notice,
11 this list of conditions and the following disclaimer in the documentation
12 and/or other materials provided with the distribution.
13 3. The names of the authors may not be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25 THE POSSIBILITY OF SUCH DAMAGE.
28 ###################################################################################
29 # http://sourceforge.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license #
30 # に参考になる日本語訳がありますが、有効なのは上記英文となります。 #
31 ###################################################################################
36 let PLUGIN_INFO = xml`
39 <description>Get short alias by bit.ly and j.mp</description>
40 <description lang="ja">bit.ly や j.mp で短縮URLを得る</description>
41 <version>2.1.2</version>
42 <author mail="anekos@snca.net" homepage="http://d.hatena.ne.jp/nokturnalmortum/">anekos</author>
43 <license>new BSD License (Please read the source code comments of this plugin)</license>
44 <license lang="ja">修正BSDライセンス (ソースコードのコメントを参照してください)</license>
45 <updateURL>https://github.com/vimpr/vimperator-plugins/raw/master/bitly.js</updateURL>
46 <minVersion>2.0pre</minVersion>
62 const Realm = 'API Key for bit.ly (bitly.js)';
63 const HostName = 'http://api.bit.ly';
64 const ApiUrl = 'http://api.bit.ly/v3';
65 const PasswordManager = Cc['@mozilla.org/login-manager;1'].getService(Ci.nsILoginManager);
67 new Components.Constructor(
68 '@mozilla.org/login-manager/loginInfo;1',
75 let logins = PasswordManager.findLogins(count, HostName, null, Realm);
80 function setupAuth (callback) {
81 liberator.open('http://bit.ly/a/your_api_key', liberator.NEW_TAB);
83 'Login name for bit.ly: ',
88 let login = LoginInfo(HostName, null, Realm, username, apiKey, '', '');
89 PasswordManager.addLogin(login);
93 default: let (e = content.document.querySelector('#bitly_api_key')) (e ? e.value : '')
100 function shorten (url, domain, command, callback) {
101 function fixResponseText (s)
104 liberator.log(arguments);
106 let req = new XMLHttpRequest();
107 req.onreadystatechange = function () {
108 if (req.readyState != 4)
110 if (req.status == 200)
111 return callback && callback(fixResponseText(req.responseText), req);
113 return liberator.echoerr(req.statusText);
116 ApiUrl + '/' + (command || 'shorten') + '?' +
117 'apiKey=' + auth.password + '&' +
118 'login=' + auth.username + '&' +
119 (command !== 'expand' ? 'uri=' : 'shortUrl=') + encodeURIComponent(url) + '&' +
120 'domain=' + (domain || 'bit.ly') + '&' +
122 req.open('GET', requestUri, callback);
124 return !callback && fixResponseText(req.responseText);
130 let auth = getAuth();
136 let args = Array.slice(arguments);
137 setupAuth(function () shorten.apply(this, args));
139 liberator.echoerr('Not found API Key!! Try :bitly command, before use.');
146 ].forEach(function ([name, domain]) {
147 commands.addUserCommand(
149 'Copy ' + domain + ' url',
151 let url = args.literalArg ? util.stringToURLArray(args.literalArg)[0] : buffer.URL;
152 let cmd = args['-expand'] ? 'expand' : 'shorten';
154 shorten(url, domain, cmd, function (short) {
155 util.copyToClipboard(short);
156 liberator.echo(short + ' <= ' + url);
162 [['-expand', '-e'], commands.OPTION_NOARG]
164 completer: function (context) {
165 context.completions = [
166 [buffer.URL, 'Current URL']
168 context.fork('URL', 0, context, completion.url);
173 __context__[name] = function (url, cmd, callback) shorten(url, domain, cmd, callback);
176 __context__.get = shorten;