From bdb78460c58bee1236e5c118a8f58a76e84e466a Mon Sep 17 00:00:00 2001 From: Ailin Nemui Date: Wed, 24 Aug 2016 20:28:53 +0200 Subject: github voting --- assets/js/votes.js | 75 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 assets/js/votes.js (limited to 'assets') diff --git a/assets/js/votes.js b/assets/js/votes.js new file mode 100644 index 0000000..4fb585d --- /dev/null +++ b/assets/js/votes.js @@ -0,0 +1,75 @@ +(function(document, $){ + 'use strict'; + var jsonpRe = /^\/\*[\s\S]*?\*\/jsonp\(([\s\S]*)\)$/m; + + function requestAll(start) { + $.ajax({ + accepts: { json: 'application/vnd.github.squirrel-girl-preview' }, + dataType: 'json', + url: start, + jsonp: false, + jsonpCallback: 'jsonp', + dataFilter: function(dta, typ) { + if (typ == "json") { + return dta.replace(jsonpRe, "$1"); + } + return dta; + } + }) + .done(function(r) { + var remaining = r.meta['X-RateLimit-Remaining']; + var rateReset = r.meta['X-RateLimit-Reset']; + var timeOut = 0; + var hasMore = false; + if (remaining < 10) { + timeOut = 1000 + rateReset * 1000 - (new Date() / 1); + } + if (timeOut < 0) timeOut = 0; + if (r.meta.Link) { + r.meta.Link.forEach(function(l) { + if (l[1].rel == "next") { + window.setTimeout(function(){requestAll(l[0]);}, timeOut); + hasMore = true; + return; + } + }); + } + if (timeOut > 0) { + timeOut += 1000; + } + + r.data.forEach(function(e) { + e.body = e.body.replace(/\r/g, ""); + var redir = e.body.match(/^#(\d+)$/); + if (redir) { + var l = start.replace(/(\/issues\/)\d+(\/comments\?)/, "$1" + redir[1] + "$2").replace(/&.*/, ""); + window.setTimeout(function(){requestAll(l);}, timeOut); + hasMore = true; + return; + } + + var lines = e.body.split("\n"); + var script = lines[0].replace(/[^-a-zA-Z0-9_]/g, "_"); + if (script == "comment") { + script = "adv_windowlist_pl"; + } + var st = "#script-" + script + " .votes"; + var row = $(st); + if (row.length) { + var votes = 1+ e.reactions['+1'] - e.reactions['-1']; + var link = "*"; + if (e.reactions['heart'] >= votes) { + link = "💜"; + } + row.html( "" + votes ); + + row.append(""+link+""); + } + }); + if (!hasMore) { + $("#th-votes").html("Votes"); + } + }); + } + requestAll('https://api.github.com/repos/ailin-nemui/scripts.irssi.org/issues/2/comments?callback=jsonp'); +})(document, $); -- cgit v1.2.3