Delicious bookmarks counter in JavaScript
This is a script I use on a few sites to automatically count the number of
delicious links to various urls. There are a few of these that I've found, but
I quite like writing JavaScript like this. Also, mine will combine as many
urls as possible into a single request to the delicious API, because it's
faster that way.
<!-- jquery -->
http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js
<!-- this is md5 implemented in javascript -->
http://pajhome.org.uk/crypt/md5/md5.js
$(function() {
// This function gets called per link, this depends on the template.
// In my case, I'm walking up to the surrounding 'post' object, then
// down to a pre-prepared p tag I want to populate with link data.
// You will want to change this if you use this code.
//
// Markup is approximately:
//
//
// ..
//
//
function markup_delicious_link( element, data ) {
var html = "";
html += data.total_posts + " delicious link(s)";
html += "";
element.parents(".post").find(".deliciousinfo").html(html);
}
// Find all links with class 'delicious'
var hashes = [];
var elements = {}
$('a.delicious').each(function(index, link) {
if ($(link).attr("href")) { // sanity check
var hash = hex_md5( $(link).attr("href") );
elements[ hash ] = $(link);
hashes.push( "hash=" + hash );
}
});
// Make calls to the delicious feed API to get details
while (hashes.length) {
// delicious permit a maximum of 15 hashes per request
var subsection = hashes.splice(0, 15);
var url = "http://badges.del.icio.us/feeds/json/url/data?";
url += subsection.join("&") + "&callback=?";
$.getJSON( url, function(data) {
for (var i=0; i