Skip to content

Instantly share code, notes, and snippets.

@danwrong
Created October 1, 2009 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danwrong/199146 to your computer and use it in GitHub Desktop.
Save danwrong/199146 to your computer and use it in GitHub Desktop.
jQuery(function($) {
if (typeof $.keys != 'function') {
$.extend({
keys: function(obj) {
var a = [];
$.each(obj, function(k){ a.push(k) });
return a;
}
});
}
var services, imageLinks;
function findService(hostname) {
var keys = $.keys(services);
for(var i=0; i < keys.length; i++) {
if (hostname.indexOf(keys[i]) >= 0) {
return keys[i];
}
}
}
function showImage($link) {
link = $link.get(0);
var service = findService(link.host);
var thumbnailURL = services[service](link.pathname);
var img = new Image();
img.src = thumbnailURL;
// Will need a little bit of work here to add any extra markup/classes
$link.parents('li.status').append(img);
$link.data('eiDone', true);
}
function showNearbyImages() {
$(imageLinks).each( function() {
var $link = $(this);
// need to IE equivs for scrollY/innerHeight here
if (((window.scrollY + window.innerHeight + 100) >= $link.offset().top) &&
!$link.data('eiDone')) {
showImage($link);
}
});
}
$.twitter = $.twitter || {};
$.extend($.twitter, {
updateEmbeddedImages: function() {
var expression = $.map($.keys(services), function(service) {
return "a[href*='" + service + "']"
}).join(', ');
imageLinks = $(expression);
},
embedImages: function(imageServices) {
services = imageServices;
$.twitter.updateEmbeddedImages();
$(window).scroll(showNearbyImages);
showNearbyImages();
}
})
});
jQuery.twitter.embedImages({
'twitpic.com': function(path) {
var code = path.match(/\/([a-zA-Z0-9]+)/)[1];
return "http://twitpic.com/show/thumb/" + code;
},
'yfrog': function(path) {
var code = path.match(/\/([a-zA-Z0-9]+)/)[1];
return "http://yfrog.com/" + code + ".th.jpg";
}
});
// When page updated with more tweets have it generate a custom event then...
// $(document).bind('twitter_tweetsloaded', $.twitter.updateEmbeddedImages);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment