-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagSort.user.js
109 lines (98 loc) · 3.23 KB
/
agSort.user.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// ==UserScript==
// @name Anagol Sorter
// @include http://golf.shinh.org/all.rb
// @description add buttons to sort list of problems in anarchy golf
// ==/UserScript==
(function() {
function showElement(element) {
element.style.display = "";
}
function hideElement(element) {
element.style.display = "none";
}
function isEndlessProblem(element) {
return element.lastChild.nodeValue === " (endless)";
}
function isPostMortemProblem(element) {
return element.lastChild.nodeValue === " (post mortem)";
}
function isActiveProblem(element) {
return !(isEndlessProblem(element) || isPostMortemProblem(element));
}
function toggleButtons(element, buttons) {
for (var i in buttons) {
buttons[i].button.disabled = false;
}
element.disabled = true;
}
function changeSubtitle(text) {
var siteSubtitle = document.getElementsByTagName("h2")[0];
siteSubtitle.firstChild.nodeValue = text;
}
var problemsList = document.getElementsByTagName("ol")[0];
var problems = problemsList.getElementsByTagName("li");
var buttonsList = document.createElement("ul");
var buttons = new function() {
this.showAll = {};
this.showActive = {};
this.showEndless = {};
this.showPostMortem = {};
};
for (var i in buttons) {
buttons[i].wrapper = document.createElement("li");
buttons[i].button = document.createElement("input");
buttons[i].button.type = "button";
buttons[i].wrapper.style.listStyle = "none";
buttons[i].wrapper.style.display = "inline-block";
}
buttons.showAll.button.value = "all";
buttons.showAll.button.disabled = true;
buttons.showAll.button.addEventListener("click", function(event) {
for (var i = 0; i < problems.length; ++i) {
showElement(problems[i]);
}
changeSubtitle("All problems");
toggleButtons(event.target, buttons);
}, false);
buttons.showPostMortem.button.value = "post mortem";
buttons.showPostMortem.button.addEventListener("click", function(event) {
for (var i = 0; i < problems.length; ++i) {
if (isPostMortemProblem(problems[i])) {
showElement(problems[i]);
} else {
hideElement(problems[i]);
}
}
changeSubtitle("Post mortem problems");
toggleButtons(event.target, buttons);
}, false);
buttons.showEndless.button.value = "endless";
buttons.showEndless.button.addEventListener("click", function(event) {
for (var i = 0; i < problems.length; ++i) {
if (isEndlessProblem(problems[i])) {
showElement(problems[i]);
} else {
hideElement(problems[i]);
}
}
changeSubtitle("Endless problems");
toggleButtons(event.target, buttons);
}, false);
buttons.showActive.button.value = "active";
buttons.showActive.button.addEventListener("click", function(event) {
for (var i = 0; i < problems.length; ++i) {
if (isActiveProblem(problems[i])) {
showElement(problems[i]);
} else {
hideElement(problems[i]);
}
}
changeSubtitle("Active problems");
toggleButtons(event.target, buttons);
}, false);
for (var i in buttons) {
buttons[i].wrapper.appendChild(buttons[i].button);
buttonsList.appendChild(buttons[i].wrapper);
}
document.body.insertBefore(buttonsList, problemsList);
})();