-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongurlplease_air.js
137 lines (133 loc) · 5.39 KB
/
longurlplease_air.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
var longurlplease = {
// At the moment clients must maintain a list of services which they will attempt to lengthen short urls for
shortUrlsPattern : new RegExp("^http(s?)://(b23\\.ru|bit\\.ly|cli\\.gs|dwarfurl\\.com|ff\\.im|idek\\.net|is\\.gd|korta\\.nu|ln-s\\.net|loopt\\.us|moourl\\.com|ping\\.fm|piurl\\.com|poprl\\.com|reallytinyurl\\.com|rubyurl\\.com|short\\.ie|smallr\\.com|snipr\\.com|snipurl\\.com|snurl\\.com|tinyurl\\.com|tr\\.im|twurl\\.nl|u\\.mavrev.com|ur1\\.ca|url\\.ie|urlx\\.ie|xrl\\.us|yep\\.it|zi\\.ma|zurl\\.ws)/.*"),
numberOfUrlsPerBatch : 4,
lengthen : function(options) {
if (typeof(options) == 'undefined')
options = {}
var makeRequest = function() {
alert('not sure how to call api');
};
if (options.transport.toLowerCase() == 'air')
makeRequest = longurlplease.makeRequestWithAir;
else if (options.transport.toLowerCase() == 'flxhr')
makeRequest = longurlplease.makeRequestWithFlxhr;
else if (options.transport.toLowerCase() == 'jquery')
makeRequest = longurlplease.makeRequestWithJQuery;
var urlToElements = options.urlToElements;
var toLengthen = options.toLengthen;
if (toLengthen == null || urlToElements == null) {
var parent = document;
if(options.element != null)
parent = options.element;
urlToElements = {};
toLengthen = [];
var els = parent.getElementsByTagName('a');
for (var elIndex = 0; elIndex < els.length; elIndex++) {
var el = els[elIndex];
if (longurlplease.shortUrlsPattern.test(el.href)) {
toLengthen.push(el.href);
var listOfElements = urlToElements[el.href];
if (listOfElements == null)
listOfElements = []
listOfElements.push(el);
urlToElements[el.href] = listOfElements;
}
}
}
var lengthenShortUrl = function(a, longUrl) {
// You can customize this - my intention here is to alter the visible text to use as much of the long url
// as possible, but maintain the same number of characters to help keep visual consistancy.
if (a.href == a.innerHTML) {
var linkText = longUrl.replace(/^http(s?):\/\//, '').replace(/^www\./, '');
a.innerHTML = linkText.substring(0, a.innerHTML.length - 3) + '...';
}
a.href = longUrl;
};
if (options.lengthenShortUrl != null)
lengthenShortUrl = options.lengthenShortUrl
var handleResponseEntry = function(shortUrl, longUrl) {
var aTags = urlToElements[shortUrl];
for (var ai = 0; ai < aTags.length; ai++)
lengthenShortUrl(aTags[ai], longUrl);
};
var subArray, i = 0;
while (i < toLengthen.length) {
subArray = toLengthen.slice(i, i + longurlplease.numberOfUrlsPerBatch)
var paramString = longurlplease.toParamString(subArray);
makeRequest(paramString, handleResponseEntry);
i = i + longurlplease.numberOfUrlsPerBatch;
}
},
toParamString : function(shortUrls) {
var paramString = "";
for (var j = 0; j < shortUrls.length; j++) {
var href = shortUrls[j];
paramString += "q=";
paramString += encodeURI(href);
if (j < shortUrls.length - 1)
paramString += '&'
}
return paramString;
},
apiUrl : function() {
return (("https:" == document.location.protocol) ? "https" : "http") + "://longurlplease.appspot.com/api/v1.1";
},
makeRequestWithAir : function(paramString, callback) {
var loader = new air.URLLoader();
loader.addEventListener(air.Event.COMPLETE, function (event) {
JSON.parse(event.target.data, function (key, val) {
if (typeof val === 'string' && val != null)
callback(key, val);
});
});
var request = new air.URLRequest(longurlplease.apiUrl() + "?ua=air&" + paramString);
loader.load(request);
},
makeRequestWithFlxhr : function(paramString, callback) {
var flproxy = new flensed.flXHR({ autoUpdatePlayer:true, xmlResponseText:false, instancePooling:true, onreadystatechange:function (XHRobj) {
if (XHRobj.readyState == 4)
JSON.parse(XHRobj.responseText, function (key, val) {
if (typeof val === 'string' && val != null)
callback(key, val);
});
}});
flproxy.open("GET", longurlplease.apiUrl());
flproxy.send("ua=flxhr&" + paramString);
},
makeRequestWithJQuery : function(paramString, callback) {
jQuery.getJSON(longurlplease.apiUrl() + "?ua=jquery&" + paramString + "&callback=?",
function(data) {
jQuery.each(data, function(key, val) {
if (val != null)
callback(key, val);
});
});
}
};
if (typeof(jQuery) != 'undefined') {
jQuery.longurlplease = function(options) {
jQuery('body').longurlplease(options)
};
jQuery.fn.longurlplease = function(options) {
if (typeof(options) == 'undefined')
options = {}
options.transport = 'jquery';
var toLengthen = [];
var urlToElements = {};
this.find('a').filter(function() {
return this.href.match(longurlplease.shortUrlsPattern)
}).each(function() {
toLengthen.push(this.href);
var listOfElements = urlToElements[this.href];
if (listOfElements == null)
listOfElements = []
listOfElements.push(this);
urlToElements[this.href] = listOfElements;
});
options.toLengthen = toLengthen;
options.urlToElements = urlToElements;
longurlplease.lengthen(options);
return this;
};
}