-
-
Save adatapost/473508 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
(function($){ | |
// Store fetched long URLs here. | |
var cache = {}; | |
$.fn.longUrl = function() { | |
return this.each(function(){ | |
var elem = $(this), | |
href = elem.attr( 'href' ), | |
api = 'http://www.longurlplease.com/api/v1.1?callback=?'; | |
if ( cache[ href ] ) { | |
// URL exists in cache, so use it. | |
elem.longUrlLengthen( cache[ href ] ); | |
} else { | |
// Fetch JSON data. | |
$.getJSON( api, { q: href }, function(data){ | |
if ( data[ href ] ) { | |
// If the data is valid, update the cache. | |
cache[ href ] = data[ href ]; | |
// Call `longUrlLengthen` for this element + long url. | |
elem.longUrlLengthen( cache[ href ] ); | |
} | |
}); | |
} | |
}); | |
}; | |
$.fn.longUrlLengthen = function( url ) { | |
// Update the element's title attribute with `url`. | |
return this.attr( 'title', url ); | |
}; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment