summaryrefslogtreecommitdiffstats
path: root/assets/js/votes.js
blob: 4fb585d4086047cd52ff5ae3837b6addf643d8d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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, $);