public
anonymous / gist:39890
Created

  • Download Gist
gistfile1.js
JavaScript
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
jQuery.longurlplease = function() {
jQuery('body').longurlplease()
};
jQuery.fn.longurlplease = function() {
var longurlpleaseShortenerPattern = new RegExp("^http(s?)://(adjix\\.com|bit\\.ly|dwarfurl\\.com|ff\\.im|idek\\.net|is\\.gd|ln-s\\.net|loopt\\.us|ping\\.fm|piurl\\.com|piurl\\.com|poprl\\.com|qlnk\\.net|reallytinyurl\\.com|rubyurl\\.com|short\\.ie|smallr\\.com|snipr\\.com|snipurl\\.com|snurl\\.com|tinyurl\\.com|tr\\.im|twurl\\.nl|ub0\\.cc|ur1\\.ca|url\\.ie|urlx\\.ie|xrl\\.us|yep\\.it|zi\\.ma|zurl\\.ws)/.*")
var toLengthen = [];
var urlToElement = {};
this.find('a').filter(function() { return this.href.match(longurlpleaseShortenerPattern)}).each(function() {
toLengthen.push(this.href);
var listOfElements = urlToElement[this.href];
if(listOfElements==null)
listOfElements = []
listOfElements.push(this);
urlToElement[this.href] = listOfElements;
});
var mapped = jQuery.map(toLengthen, function(n) {
return "q=" + encodeURI(n);
});
var subArray, i = 0;
while (i < mapped.length) {
subArray = mapped.slice(i, i + 4)
var paramStringPart = subArray.join('&');
var loader = new air.URLLoader();
loader.addEventListener( air.Event.COMPLETE,
function (event) {
var data = event.target.data;
data = JSON.parse( data );
jQuery.each(data, function(key, val) {
if(val != null) {
var aTags = urlToElement[key];
for (var ai = 0; ai < aTags.length; ai++) {
aTag = aTags[ai];
if (aTag.href == aTag.innerHTML) {
var linkText = val.replace(/^http(s?):\/\//, '').replace(/^www\./, '');
aTag.innerHTML = linkText.substring(0, aTag.innerHTML.length - 3) + '...';
}
aTag.href = val;
}
}
});
}
);
var request = new air.URLRequest( "http://www.longurlplease.com/api/v1.1?ua=jquery&" + paramStringPart );
loader.load( request );
// jQuery.getJSON("http://www.longurlplease.com/api/v1.1?ua=jquery&" + paramStringPart);
i = i + 4;
}
return this;
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.