This repository has been archived by the owner on Sep 17, 2018. It is now read-only.
/
longurlplease.js
178 lines (171 loc) · 7.47 KB
/
longurlplease.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
/*
* Powered by the most excellent http://www.longurlplease.com/
*
* See http://www.longurlplease.com/js/longurlplease.js for the original source.
*
*/
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?)://(307\.to|adjix\.com|b23\.ru|bacn\.me|bit\.ly|bloat\.me|budurl\.com|cli\.gs|clipurl\.us|cort\.as|digg\.com|dwarfurl\.com|ff\.im|fff\.to|href\.in|idek\.net|is\.gd|kl\.am|korta\.nu|lin\.cr|ln\-s\.net|loopt\.us|lost\.in|memurl\.com|merky\.de|migre\.me|moourl\.com|nanourl\.se|ow\.ly|peaurl\.com|ping\.fm|piurl\.com|plurl\.me|pnt\.me|poprl\.com|post\.ly|rde\.me|reallytinyurl\.com|redir\.ec|rubyurl\.com|short\.ie|short\.to|smallr\.com|sn\.im|sn\.vc|snipr\.com|snipurl\.com|snurl\.com|su\.pr|tiny\.cc|tinysong\.com|tinyurl\.com|togoto\.us|tr\.im|tra\.kz|trg\.li|twurl\.cc|twurl\.nl|u\.mavrev\.com|u\.nu|ur1\.ca|url\.az|url\.ie|urlx\.ie|w34\.us|xrl\.us|yep\.it|zi\.ma|zurl\.ws)/[a-zA-Z0-9_-]+)$|((^http(s?)://[a-zA-Z0-9_-]+\.notlong\.com)|(^http(s?)://[a-zA-Z0-9_-]+\.qlnk\.net)|(^http(s?)://chilp\.it/[?][a-zA-Z0-9_-]+)|(^http(s?)://trim\.li/nk/[a-zA-Z0-9_-]+))[/]?$"),
numberOfUrlsPerBatch : 4,
lengthen : function(options) {
if (typeof(options) == 'undefined')
options = {};
var makeRequest = function() { alert('not sure how to call api'); };
if (options.transport != null ){
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 = longurlplease.defaultExpandMethod;
if (options.lengthenShortUrl != null)
{
if(typeof options.lengthenShortUrl == 'function')
lengthenShortUrl = options.lengthenShortUrl;
else if(typeof options.lengthenShortUrl == 'string')
{
if(options.lengthenShortUrl == 'href-only')
lengthenShortUrl = longurlplease.hrefOnlyExpandMethod;
else if(options.lengthenShortUrl == 'full')
lengthenShortUrl = longurlplease.fullExpandMethod;
else if(options.lengthenShortUrl == 'text-and-title')
lengthenShortUrl = longurlplease.textAndTitleExpandMethod;
}
}
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;
}
},
defaultExpandMethod: function(aTag, 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 (aTag.href == aTag.innerHTML) {
// var linkText = longUrl.replace(/^http(s?):\/\//, '').replace(/^www\./, '');
// aTag.innerHTML = linkText.substring(0, aTag.innerHTML.length - 3) + '...';
var linkText = longUrl;
if (linkText.length > 40) {
aTag.innerHTML = linkText.substring(0, 40) + '...';
} else {
aTag.innerHTML = linkText;
}
}
aTag.href = longUrl;
},
hrefOnlyExpandMethod : function(aTag, longUrl) {
aTag.href = longUrl;
},
fullExpandMethod : function(aTag, longUrl) {
aTag.href = longUrl;
aTag.innerHTML = longUrl;
},
textAndTitleExpandMethod : function(aTag, longUrl) {
var linkText = longUrl.replace(/^http(s?):\/\//, '').replace(/^www\./, '');
aTag.innerHTML = linkText.substring(0, aTag.innerHTML.length - 3) + '...';
aTag.title = longUrl;
},
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);
},
// made possible by http://flxhr.flensed.com/
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() + "?ua=flxhr&" + paramString);
flproxy.send();
},
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;
};
}