-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagStat.js
32 lines (27 loc) · 980 Bytes
/
agStat.js
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
// ==UserScript==
// @name anagol statistics counter
// @include http://golf.shinh.org/p.rb?*
// ==/UserScript==
addEventListener("input", function(event) {
if (event.target.name !== "code") return;
function get_statistics(s) {
var a = [0, 0, 0, 0];
var an = /[a-zA-Z0-9]/;
var ws = /[ \t\n]/;
s.split("").forEach(function(c) {
var x = c.charCodeAt(0);
var z = encodeURI(c).substring(1).split("%").length;
a[an.test(c) ? 2 : ws.test(c) ? 1 : x < 127 && x > 32 ? 3 : 0] += z;
});
return a;
}
var code = event.target;
var stat = get_statistics(code.value);
stat = " " + stat.reduce(function(x, y) { return x + y; }) + "B" +
" (" + stat[0] + "B / " + stat[2] + "B / " + stat[3] + "B)";
var reveal = document.getElementsByName("reveal")[0];
reveal.parentNode.
insertBefore(document.createTextNode(" "), reveal.nextSibling);
reveal.parentNode.normalize();
reveal.nextSibling.nodeValue = stat;
}, false);