-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitly.js
388 lines (348 loc) · 11.8 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
init_shorten_url = function()
{
var status_editor = document.querySelector("#status");
status_editor.addEventListener( "keyup" ,
function(e)
{
status_counter();
var reply_to = document.querySelector("#in_reply_to").value;
if( !e.target.value.match( new RegExp(reply_to) ) )
{
remove_in_reply_to();
}
if( e.keyCode == 32 )
{
shorten_url(e.target , "withspace");
}
mbtweet.bitly_token = guid();
var bitly_token_temp = mbtweet.bitly_token;
setTimeout(function(e)
{
shorten_url( document.querySelector("#status") , "all" , (bitly_token_temp + ""));
} , 1500);
//if(mbtweet.debug)window.console.log(bitly_token_temp);
} ,
true );
status_editor.addEventListener( "focus" ,
function(e)
{
shorten_url(e.target , "all");
} ,
true );
status_editor.addEventListener( "blur" ,
function(e)
{
shorten_url(e.target , "all");
} ,
true );
}
shorten_url = function( target )
{
if( arguments[1] == "whitespace" || arguments[2] == mbtweet.bitly_token)
{
var original_string = target.value;
var url_detection_regexp_num = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/^”\)]))?/g;
if(arguments[1] == 'all')
{
var url_detection_regexp = /((http|https):\/\/(\w+:{0,1}\w*@)?([^\s\)\"\'”]+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/;
}
else
{
var url_detection_regexp = /((http|https):\/\/(\w+:{0,1}\w*@)?([^\s\)\"\'”]+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)\s/;
//var url_detection_regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?\s/g;
}
var shorten_url_detector = /bit\.ly|j.mp|tinyurl\.com|is\.gd|turl\/nl|ff\/.im\/|twitpic\.com|twitvid\.com|pic\.gd|movapic\.com|yfrog\.com|www\.bcphotoshare\.com|bkite\.com|tiny12\.com|tumblr\.com|flic\.kr|www\.flickr\.com|bctiny\.com|f\.hatena\.ne\.jp|www.youtube.com\/watch\?v\=[a-zA-Z0-9\_]{8,15}/;
var match_url = original_string.match( url_detection_regexp_num );
if( match_url )
{
for(var i = 0 ; i < match_url.length ; i ++)
{
if( !match_url[i].match(shorten_url_detector) && match_url[i].length >= 30 )
{
window.console.log( i , match_url );
BitlyClient.shorten( match_url[i].match( url_detection_regexp )[1] , 'bitly_shorten_URL');
}
}
}
}
status_counter();
}
bitly_shorten_URL = function(data)
{ //bit.ly shorten support script
var result;
var url_list;
var short_url = '';
var original_url = '';
var i = 0;
for( key in data.results )
{
if( !key.match(/https?:\/\//) )
{
original_url = key;
short_url = key;
} else {
original_url = key.replace(/(\#|\!|\?|\+|\=|\&|\%|\@|\!|\-|\/)/g,'\\$1');
short_url = data.results[key]['shortUrl'];
var original_text = document.querySelector("#status").value;
var replace_text = original_text.replace( new RegExp(original_url) , short_url)
document.querySelector("#status").value = replace_text;
}
}
return;
}
expandResponse = function(data , target_id)
{ //bit.ly expander support script
var result;
var url_list;
var real_url = '';
var i = 0;
for( key in data.results )
{
if( key.match(/\-/) )
{
target_id = key + '';
} else {
real_url = data.results[key]['longUrl'];
}
}
expand_url( real_url , target_id );
return;
}
//
if (typeof(BitlyApi) == 'undefined')
var BitlyApi = {}; // BitlyApi namespace. You sholdn't need to access methods here. Instead, use an instance of BitlyApiClient().
if (typeof(BitlyCB) == 'undefined')
var BitlyCB = {}; // global namespace for your callback methods. Allows you to define callabacks from within other method calls.
BitlyApi.loadScript = function(_src) {
jsonp_fetch( _src );
// var e = document.createElement('script');
// e.setAttribute('language','javascript');
// e.setAttribute('type', 'text/javascript');
// e.setAttribute('src',_src); document.body.appendChild(e);
};
BitlyApi.loadCss = function(u) {
var e = document.createElement('link');
e.setAttribute('type', 'text/css');
e.setAttribute('href', u);
e.setAttribute('rel', 'stylesheet');
e.setAttribute('media', 'screen');
try {
document.getElementsByTagName('head')[0].appendChild(e);
} catch(z) {
document.body.appendChild(e);
}
};
BitlyApi.call = function(method, params, callback_method_name) {
var s = "http://api.bit.ly/" + method;
var url_args = [];
if (callback_method_name) url_args.push("callback=" + callback_method_name);
for (var name in params) {
url_args.push(name + "=" + encodeURIComponent(params[name]));
};
s += "?" + url_args.join("&");
BitlyApi.loadScript(s);
};
var BitlyApiClient = function(login, apiKey, version){
this.login = login || "ttrace";
this.apiKey = apiKey || "R_2b774725405f8f66f2f6565838bb04f5";
this.version = version || "2.0.1";
};
BitlyApiClient.prototype.googleVisRequired = "This method requires the google visualization api. Please include javascript from: http://www.google.com/jsapi. More info: http://code.google.com/apis/visualization/documentation/index.html";
BitlyApiClient.prototype.availableModules = ['stats'];
BitlyApiClient.prototype.loadingModules = {};
BitlyApiClient.prototype.moduleLoaded = function(module_name, callback_method_name) {
BitlyApiClient.prototype.loadingModules[module_name] = true;
for (var mod in BitlyApiClient.prototype.loadingModules) {
if (!BitlyApiClient.prototype.loadingModules[mod]) {
return false;
}
};
eval(callback_method_name + "();");
};
BitlyApiClient.prototype.loadModules = function(module_names, callback_method_name) {
for (var i=0; i < module_names.length; i++) {
BitlyApiClient.prototype.loadingModules[module_names[i]] = false;
};
for (var i=0; i < module_names.length; i++) {
var name = module_names[i];
var callback_name = "module_" + name + "_loaded";
BitlyCB[callback_name] = function() {
BitlyApiClient.prototype.moduleLoaded(name, callback_method_name);
};
var s = "http://bit.ly/app/modules/" + name + ".js?callback=BitlyCB." + callback_name;
try {
BitlyApi.loadScript(s);
} catch(e) {
BitlyClient.addPageLoadEvent(function(){
BitlyApi.loadScript(s);
});
}
};
try {
BitlyApi.loadCss("http://bit.ly/static/css/javascript-modules.css");
} catch(e) {
BitlyClient.addPageLoadEvent(function(){
BitlyApi.loadCss("http://bit.ly/static/css/javascript-modules.css");
});
}
};
// following code is for bit.ly supporting.
/*
# utils
*/
BitlyApiClient.prototype.addPageLoadEvent = function(func) {
var oldonload = window.onload;
if (typeof window.onload != 'function') {
window.onload = func;
} else {
window.onload = function() { oldonload(); func(); };
}
};
BitlyApiClient.prototype.extractBitlyHash = function(bitly_url_or_hash) {
if (bitly_url_or_hash == null) {
return null;
} else {
var m = bitly_url_or_hash.match(/\/([^\/]+)$/);
if (m) {
return m[1];
}
else {
return bitly_url_or_hash;
}
}
};
BitlyApiClient.prototype.createElement = function(element_type, attrs) {
var el = document.createElement(element_type);
for (var k in attrs) {
if (k == "text") {
el.appendChild(document.createTextNode(attrs[k]));
} else {
this.setAttribute(el, k, attrs[k]);
}
};
return el;
};
BitlyApiClient.prototype.setAttribute = function(element, attribute_name, attribute_value) {
if (attribute_name == "class") {
element.setAttribute("className", attribute_value); // set both "class" and "className"
}
return element.setAttribute(attribute_name, attribute_value);
};
BitlyApiClient.prototype.listen = function (elem, evnt, func) {
if (elem.addEventListener) // W3C DOM
elem.addEventListener(evnt,func,false);
else if (elem.attachEvent) { // IE DOM
var r = elem.attachEvent("on"+evnt, func);
return r;
}
};
BitlyApiClient.prototype.targ = function (e) {
var targ;
if (!e) var e = window.event;
if (e.target) targ = e.target;
else if (e.srcElement) targ = e.srcElement;
if (targ.nodeType == 3) // defeat Safari bug
targ = targ.parentNode;
return targ;
};
BitlyApiClient.prototype.toggle = function(el) {
var e;
if (typeof(el) == 'string') {
e = document.getElementById(el);
if (typeof(e) == undefined) {
throw "toggle: No DOM element with id: " + el;
return;
}
} else {
e = el;
}
if (e.style.display == 'none') {
e.style.display = '';
} else {
e.style.display = 'none';
}
};
/*
# API
Generic API caller for more advanced API usage. Allows you to specify extra params for method calls with options. Eg, you can call the /info API and ask for a subset of data using the 'keys' param.
*/
BitlyApiClient.prototype.call = function(method, params, callback_method_name) {
params['version'] = this.version;
params['login'] = this.login;
params['apiKey'] = this.apiKey;
return BitlyApi.call(method, params, callback_method_name);
};
// shorten a long url
BitlyApiClient.prototype.shorten = function(longUrl, callback_method_name) {
return this.call('shorten', {'longUrl': longUrl}, callback_method_name);
};
// expand a bitly url
BitlyApiClient.prototype.expand = function(shortUrl, callback_method_name) {
return this.call('expand', {'shortUrl': shortUrl}, callback_method_name);
};
// get info about one or more bitly urls or hashes
BitlyApiClient.prototype.info = function(bitly_hash, callback_method_name) {
var arr = bitly_hash.split(',');
var hashes = [];
for (var i=0; i < arr.length && i < 1; i++) {// limit to 1 bitly_hash
hashes.push(this.extractBitlyHash(arr[i]));
};
return this.call('info', {'hash': hashes.join(',')}, callback_method_name);
};
// get referrer data about a bilty url or hash
BitlyApiClient.prototype.stats = function(bitly_hash_or_url, callback_method_name) {
bitly_hash_or_url = this.extractBitlyHash(bitly_hash_or_url);
return this.call('stats', {'hash': bitly_hash_or_url}, callback_method_name);
};
/*
# TESTS
*/
BitlyApiClient.prototype.shortenTest = function() {
this.shorten(document.location, 'shortenTestCB');
};
function shortenTestCB(data) {
// this is how to get a result of shortening a single url
var result;
for (var r in data.results) {
result = data.results[r];
result['longUrl'] = r;
break;
};
alert(result['longUrl'] + " shortened to " + result['shortUrl']);
};
BitlyApiClient.prototype.expandTest = function() {
this.expand("http://bit.ly/3j4ir4", 'expandTestCB');
};
function expandTestCB(data) {
// this is how to get a result of expanding a single url
var result;
for (var r in data.results) {
result = data.results[r];
result['hash'] = r;
break;
};
alert(result['hash'] + " expanded to " + result['longUrl']);
};
BitlyApiClient.prototype.infoTest = function() {
this.info("http://bit.ly/3j4ir4", 'infoTestCB');
};
function infoTestCB(data) {
// this is how to get a doc of info call for a single url
var doc;
for (var r in data.results) {
doc = data.results[r];
break;
};
alert("got info for " + doc['hash'] + ". eg., longUrl is " + doc['longUrl'] + ", title is " + doc['htmlTitle']);
};
BitlyApiClient.prototype.statsTest = function() {
this.stats("http://bit.ly/3j4ir4", 'statsTestCB');
};
function statsTestCB(data) {
var stats = data.results;
alert("stats for " + stats['hash'] + ". " + stats['clicks'] + " clicks");
};
/*
# INSTANTIATE CLIENT
*/
var BitlyClient = new BitlyApiClient();