Created
December 25, 2008 12:20
-
-
Save anonymous/39890 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment