diff options
| author | Ailin Nemui | 2016-08-24 20:28:53 +0200 |
|---|---|---|
| committer | Ailin Nemui | 2016-08-24 20:28:53 +0200 |
| commit | bdb78460c58bee1236e5c118a8f58a76e84e466a (patch) | |
| tree | 3f11111f6baa997ab50db3e37b26236d9e6b0c26 /assets | |
| parent | 4b000512ec85c183d58320a03bc93f3e72f9d5fb (diff) | |
| download | scripts.irssi.org-bdb78460c58bee1236e5c118a8f58a76e84e466a.tar.bz2 | |
github voting
Diffstat (limited to 'assets')
| -rw-r--r-- | assets/js/votes.js | 75 |
1 files changed, 75 insertions, 0 deletions
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("<span><a data-toggle=\"tooltip\" title=\"vote on github\" href=\"" + e.html_url + "\">"+link+"</a></span>"); + } + }); + if (!hasMore) { + $("#th-votes").html("Votes"); + } + }); + } + requestAll('https://api.github.com/repos/ailin-nemui/scripts.irssi.org/issues/2/comments?callback=jsonp'); +})(document, $); |
