From bca5b63c710f124677e93a4118ce6b6c68ddcada Mon Sep 17 00:00:00 2001 From: Astro Date: Tue, 12 Jun 2012 00:48:44 +0200 Subject: [PATCH] bitlove-enclosures.js --- content/static/script/bitlove-enclosures.js | 117 ++++++++++++++++++++ xsl/header.xsl | 3 + 2 files changed, 120 insertions(+) create mode 100644 content/static/script/bitlove-enclosures.js diff --git a/content/static/script/bitlove-enclosures.js b/content/static/script/bitlove-enclosures.js new file mode 100644 index 000000000..28c5f07fe --- /dev/null +++ b/content/static/script/bitlove-enclosures.js @@ -0,0 +1,117 @@ +var XHR = window.AnonXMLHttpRequest || window.XMLHttpRequest; +if (!XHR) + throw new Error("Your browser lacks support for standardized AJAX calls. What the ...?"); + + +var urlCallbacks = {}; + +function resolve(url, cb) { + if (urlCallbacks.hasOwnProperty(url)) { + urlCallbacks[url].push(cb); + } else { + urlCallbacks[url] = [cb]; + } + + maySetTimeout(); +} + +var timeout; +function maySetTimeout() { + var pending = Object.keys(urlCallbacks).length > 0; + + if (!timeout && pending) { + timeout = setTimeout(function() { + timeout = undefined; + maySend(); + }, 50); + } else if (timeout && !pending) { + clearTimeout(timeout); + } +} + +function maySend() { + var urls = Object.keys(urlCallbacks).slice(0, 8); + if (urls.length > 0) { + doSend(urls, function(response) { + /* Dispatch to callbacks */ + urls.forEach(function(url) { + var urlResponse = response[url]; + var cbs = urlCallbacks[url]; + delete urlCallbacks[url]; + cbs.forEach(function(cb) { + try { + cb(urlResponse); + } catch(e) { + if (console && console.error) + console.error(e && e.stack || e); + } + }); + }); + }); + } +} + +function doSend(urls, cb) { + var q = urls.map(function(url) { + return "url=" + encodeURIComponent(url); + }).join("&"); + + var cl = new XHR(); + cl.open('GET', 'http://api.bitlove.org/by-enclosure.json?' + q); + cl.onreadystatechange = function() { + if (this.readyState == this.DONE) { + var response; + if (this.status == 200 && + this.responseText) { + try { + response = JSON.parse(this.responseText); + } catch (e) { + if (console && console.error) + console.error(e && e.stack || e); + } + } + cb(response); + + /* Continue with next batch: */ + maySend(); + } + }; + cl.send(); +} + + +$(document).ready(function() { + /* For C3D2-Web */ + $('a[rel="enclosure"]').each(function() { + var a = $(this); + var prevLi = a.parent(); + var url = a.attr('href'); + + if (/\.torrent$/.test(url)) + return; + + resolve(url, function(info) { + var torrent = info && info.sources && info.sources[0] && info.sources[0].torrent; + if (info && torrent) { + var li = $('
  • Torrent:
  • '); + li.find('a').attr('href', torrent); + li.find('.s').text(info.seeders + " Seeder, "); + li.find('.l').text(info.leechers + " Leecher"); + if (info.downloaded == 1) { + li.find('.d').text(", 1 Download"); + } else if (info.downloaded > 1) { + li.find('.d').text(", " + info.downloaded + " Downloads"); + } + + var link = info.sources[0].permalink; + if (link) { + var source = $(' on Bitlove'); + source.find('a').attr('href', link); + li.append(source); + } + + prevLi.after(li); + } + }); + }); +}); diff --git a/xsl/header.xsl b/xsl/header.xsl index 6be5126e9..ed247306d 100644 --- a/xsl/header.xsl +++ b/xsl/header.xsl @@ -58,6 +58,9 @@ + + +