forked from vimpr/vimperator-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitly.js
177 lines (156 loc) · 6 KB
/
bitly.js
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 = xml`
<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="[email protected]" 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;
})();