Skip to content

Instantly share code, notes, and snippets.

@manveru
Created November 2, 2011 09:43
Show Gist options
  • Save manveru/1333274 to your computer and use it in GitHub Desktop.
Save manveru/1333274 to your computer and use it in GitHub Desktop.
burnbit button maker
callbackData = []
styles = {normal: "burnbit_normal", compact: "burnbit_compact"}
hasClass = (tag, name) ->
tag.className.match(new RegExp("(\\s|^)" + name + "(\\s|$)"))
addClass = (tag, name) ->
tag.className += " #{name}" unless hasClass(tag, name)
fillDetails = (tag, seeds, peers) ->
tag.innerHTML = "<span class='burnbit_button_text' style='line-height:1;'>torrent</span> <span class='burnbit_torrent_details'><span class='s burnbit_seeds'>#{seeds} seeds</span><span class='p burnbit_peers'>#{peers} peers</span></span>"
finalize = (alive, href, seeds, peers) ->
return if @torrentified?
bbstyle = @getAttribute('burnbit_style')
@href = href
@torrentified = true
if bbstyle?
bbstyle = bbstyle.toLowerCase()
return if bbstyle.indexOf('custom') >= 0
for styleKey, styleValue of styles
if bbstyle.indexOf(styleKey) >= 0
addClass(@, styleValue)
fillDetails(@, seeds, peers)
return undefined
addClass(@, 'burnbit_normal')
fillDetails(@, seeds, peers)
return undefined
makeCallback = (n) ->
name = "burnBitRender#{n}"
window[name] = (torrents) ->
for torrent, m in torrents
finalize.apply(callbackData[n].tags[m], torrent)
delete window[name]
return name
request = ->
torrents = for tag, torrentId in document.getElementsByClassName('burnbit_torrent')
urls = tag.getAttribute('burnbit_file').split('|')
[tag, ("u[#{torrentId}][]=#{escape(url)}" for url in urls)]
callback = {tags: [], bytes: 0, queries: []}
callbackData.push(callback)
for [tag, queries] in torrents
bytes = 0
bytes += (query.length + 1) for query in queries
if bytes < 1600
if (callback.bytes + bytes) >= 1600 || callback.tags.length >= 20
callback = {tags: [], bytes: 0, queries: []}
callbackData.push(callback)
callback.tags.push(tag)
callback.bytes += bytes
callback.queries.push(query) for query in queries
for req, n in callbackData
a = document.createElement('script')
a.src = "http://api.burnbit.com/getTorrent?#{req.queries.join('&')}&callback=#{makeCallback(n)}"
document.body.appendChild(a)
return undefined
oldOnLoad = window.onload
window.onload = ->
oldOnLoad?()
css = "n.burnbit_normal{height:30px;width:112px;padding-left:24px;padding-top:2px;}.burnbit_normal{display:block;text-align:left;background:#fff url(http://api.burnbit.com/images/button/down.png) no-repeat left 3px fixed;font-size:17px;}.burnbit_normal .burnbit_torrent_details{display:block;line-height:1;font-weight:700;font-size:9px;width:100%;}.burnbit_normal .burnbit_button_text{display:block;}a.burnbit_compact{background-color:#FFF;font-family:arial;text-decoration:none;border:none;color:#444;height:17px;width:160px;padding-left:16px;}.burnbit_compact{display:block;text-align:left;background:#fff url(http://api.burnbit.com/images/button/downcompact.png) no-repeat left 1px;line-height:11px;font-size:15px;}.burnbit_compact .burnbit_torrent_details{line-height:18px;font-weight:700;font-size:9px;width:100%;}.burnbit_torrent_details span.s{color:#6a902a;}.burnbit_torrent_details span.p{padding-left:2px;color:#787777;}a.burnbit_normal:hover,a.burnbit_compact:hover{text-decoration:none;}"
style = document.createElement("style")
style.setAttribute("type", "text/css")
if style.styleSheet
style.styleSheet.cssText = css
else
style.appendChild(document.createTextNode(css))
document.getElementsByTagName('head')[0].appendChild(style)
request()
(function() {
var addClass, callbackData, fillDetails, finalize, hasClass, makeCallback, oldOnLoad, request, styles;
callbackData = [];
styles = {
normal: "burnbit_normal",
compact: "burnbit_compact"
};
hasClass = function(tag, name) {
return tag.className.match(new RegExp("(\\s|^)" + name + "(\\s|$)"));
};
addClass = function(tag, name) {
if (!hasClass(tag, name)) {
return tag.className += " " + name;
}
};
fillDetails = function(tag, seeds, peers) {
return tag.innerHTML = "<span class='burnbit_button_text' style='line-height:1;'>torrent</span> <span class='burnbit_torrent_details'><span class='s burnbit_seeds'>" + seeds + " seeds</span><span class='p burnbit_peers'>" + peers + " peers</span></span>";
};
finalize = function(alive, href, seeds, peers) {
var bbstyle, styleKey, styleValue;
if (this.torrentified != null) {
return;
}
bbstyle = this.getAttribute('burnbit_style');
this.href = href;
this.torrentified = true;
if (bbstyle != null) {
bbstyle = bbstyle.toLowerCase();
if (bbstyle.indexOf('custom') >= 0) {
return;
}
for (styleKey in styles) {
styleValue = styles[styleKey];
if (bbstyle.indexOf(styleKey) >= 0) {
addClass(this, styleValue);
fillDetails(this, seeds, peers);
return;
}
}
}
addClass(this, 'burnbit_normal');
fillDetails(this, seeds, peers);
};
makeCallback = function(n) {
var name;
name = "burnBitRender" + n;
window[name] = function(torrents) {
var m, torrent, _len;
for (m = 0, _len = torrents.length; m < _len; m++) {
torrent = torrents[m];
finalize.apply(callbackData[n].tags[m], torrent);
}
return delete window[name];
};
return name;
};
request = function() {
var a, bytes, callback, n, queries, query, req, tag, torrentId, torrents, url, urls, _i, _j, _k, _len, _len2, _len3, _len4, _ref;
torrents = (function() {
var _len, _ref, _results;
_ref = document.getElementsByClassName('burnbit_torrent');
_results = [];
for (torrentId = 0, _len = _ref.length; torrentId < _len; torrentId++) {
tag = _ref[torrentId];
urls = tag.getAttribute('burnbit_file').split('|');
_results.push([
tag, (function() {
var _i, _len2, _results2;
_results2 = [];
for (_i = 0, _len2 = urls.length; _i < _len2; _i++) {
url = urls[_i];
_results2.push("u[" + torrentId + "][]=" + (escape(url)));
}
return _results2;
})()
]);
}
return _results;
})();
callback = {
tags: [],
bytes: 0,
queries: []
};
callbackData.push(callback);
for (_i = 0, _len = torrents.length; _i < _len; _i++) {
_ref = torrents[_i], tag = _ref[0], queries = _ref[1];
bytes = 0;
for (_j = 0, _len2 = queries.length; _j < _len2; _j++) {
query = queries[_j];
bytes += query.length + 1;
}
if (bytes < 1600) {
if ((callback.bytes + bytes) >= 1600 || callback.tags.length >= 20) {
callback = {
tags: [],
bytes: 0,
queries: []
};
callbackData.push(callback);
}
callback.tags.push(tag);
callback.bytes += bytes;
for (_k = 0, _len3 = queries.length; _k < _len3; _k++) {
query = queries[_k];
callback.queries.push(query);
}
}
}
for (n = 0, _len4 = callbackData.length; n < _len4; n++) {
req = callbackData[n];
a = document.createElement('script');
a.src = "http://api.burnbit.com/getTorrent?" + (req.queries.join('&')) + "&callback=" + (makeCallback(n));
document.body.appendChild(a);
}
};
oldOnLoad = window.onload;
window.onload = function() {
var css, style;
if (typeof oldOnLoad === "function") {
oldOnLoad();
}
css = "n.burnbit_normal{height:30px;width:112px;padding-left:24px;padding-top:2px;}.burnbit_normal{display:block;text-align:left;background:#fff url(http://api.burnbit.com/images/button/down.png) no-repeat left 3px fixed;font-size:17px;}.burnbit_normal .burnbit_torrent_details{display:block;line-height:1;font-weight:700;font-size:9px;width:100%;}.burnbit_normal .burnbit_button_text{display:block;}a.burnbit_compact{background-color:#FFF;font-family:arial;text-decoration:none;border:none;color:#444;height:17px;width:160px;padding-left:16px;}.burnbit_compact{display:block;text-align:left;background:#fff url(http://api.burnbit.com/images/button/downcompact.png) no-repeat left 1px;line-height:11px;font-size:15px;}.burnbit_compact .burnbit_torrent_details{line-height:18px;font-weight:700;font-size:9px;width:100%;}.burnbit_torrent_details span.s{color:#6a902a;}.burnbit_torrent_details span.p{padding-left:2px;color:#787777;}a.burnbit_normal:hover,a.burnbit_compact:hover{text-decoration:none;}";
style = document.createElement("style");
style.setAttribute("type", "text/css");
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
document.getElementsByTagName('head')[0].appendChild(style);
return request();
};
}).call(this);
(function(){var i,g,h,p,q,r,j,s,k;g=[];k={normal:"burnbit_normal",compact:"burnbit_compact"};q=function(a,b){return a.className.match(RegExp("(\\s|^)"+b+"(\\s|$)"))};i=function(a,b){if(!q(a,b))return a.className+=" "+b};h=function(a,b,c){return a.innerHTML="<span class='burnbit_button_text' style='line-height:1;'>torrent</span> <span class='burnbit_torrent_details'><span class='s burnbit_seeds'>"+b+" seeds</span><span class='p burnbit_peers'>"+c+" peers</span></span>"};p=function(a,b,c,d){var e;if(this.torrentified==null){a=this.getAttribute("burnbit_style");this.href=b;this.torrentified=true;if(a!=null){a=a.toLowerCase();if(a.indexOf("custom")>=0)return;for(e in k)if(b=k[e],a.indexOf(e)>=0){i(this,b);h(this,c,d);return}}i(this,"burnbit_normal");h(this,c,d)}};r=function(a){var b;b="burnBitRender"+a;window[b]=function(c){var d,e,f;for(d=0,f=c.length;d<f;d++)e=c[d],p.apply(g[a].tags[d],e);return delete window[b]};return b};s=function(){var a,b,c,d,e,f,m,i,
h,n,l,j,k,o;m=function(){var a,b,c;b=document.getElementsByClassName("burnbit_torrent");c=[];for(f=0,a=b.length;f<a;f++)e=b[f],h=e.getAttribute("burnbit_file").split("|"),c.push([e,function(){var a,b,c;c=[];for(a=0,b=h.length;a<b;a++)i=h[a],c.push("u["+f+"][]="+escape(i));return c}()]);return c}();a={tags:[],bytes:0,queries:[]};g.push(a);for(n=0,j=m.length;n<j;n++){o=m[n];e=o[0];c=o[1];b=0;for(l=0,k=c.length;l<k;l++)d=c[l],b+=d.length+1;if(b<1600){if(a.bytes+b>=1600||a.tags.length>=20)a={tags:[],
bytes:0,queries:[]},g.push(a);a.tags.push(e);a.bytes+=b;for(b=0,l=c.length;b<l;b++)d=c[b],a.queries.push(d)}}for(c=0,m=g.length;c<m;c++)d=g[c],a=document.createElement("script"),a.src="http://api.burnbit.com/getTorrent?"+d.queries.join("&")+"&callback="+r(c),document.body.appendChild(a)};j=window.onload;window.onload=function(){var a,b;typeof j==="function"&&j();a="n.burnbit_normal { background-color: #FFF; font-family: arial; text-decoration: none; border: none; color: #444; height: 30px; width: 112px; padding-left: 24px; padding-top: 2px; } a.burnbit_normal:hover { text-decoration: none; } .burnbit_normal { display: block; text-align: left; background-image: url('http://api.burnbit.com/images/button/down.png'); background-repeat: no-repeat; background-position: left 3px; font-size: 17px;} .burnbit_normal .burnbit_torrent_details { display: block; line-height: 1; font-weight: bold; font-size: 9px; width: 100%; } .burnbit_normal .burnbit_button_text { display: block; }";
a+="a.burnbit_compact { background-color: #FFF; font-family: arial; text-decoration: none; border: none; color: #444; height: 17px; width: 160px; padding-left: 16px ;} a.burnbit_compact:hover { text-decoration: none; } .burnbit_compact { display: block; text-align: left; background-image: url('http://api.burnbit.com/images/button/downcompact.png'); background-repeat: no-repeat; background-position: left 1px; line-height: 11px; font-size: 15px;} .burnbit_compact .burnbit_torrent_details { line-height: 18px; font-weight: bold; font-size: 9px; width: 100%;}";
b=document.createElement("style");b.setAttribute("type","text/css");b.styleSheet?b.styleSheet.cssText=a+" .burnbit_torrent_details span.s { color: #6a902a; } .burnbit_torrent_details span.p { padding-left: 2px; color: #787777; }":b.appendChild(document.createTextNode(a+" .burnbit_torrent_details span.s { color: #6a902a; } .burnbit_torrent_details span.p { padding-left: 2px; color: #787777; }"));document.getElementsByTagName("head")[0].appendChild(b);return s()}}).call(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment