-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
199 lines (175 loc) · 6.17 KB
/
index.html
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<html>
<head>
<style>
.center-align {
text-align: center;
}
body {
filter: invert(1);
background-color: black;
}
a {
filter: invert(1);
}
div {
padding: 1em;
}
</style>
</head>
<body>
<title>VNDB Playtime Calculator</title>
<h1 class="center-align">VNDB Playtime Calculator</h1>
<div class="center-align">
<label for="userid">VNDB User ID:</label>
<input type="text" id="userid" name="userid" placeholder="u123456"><br /><br />
<button type="button" id="labels-button">Get Labels!</button>
<div id="labels-section"></div>
</div>
<div class="center-align" id="output-section">
</div>
</body>
<script>
// I know this code is horrific. I'm not good at JS and don't like it either. I mean, what the fuck is a promise anyway?
// If it works then it's good code -other dev
let currentPage = 1;
let vnData = [];
let labelsData = [];
function timeCalculation(total_minutes) {
// turn minutes into days, hours and minutes
let days = Math.floor(total_minutes / (60 * 24));
let hours = Math.floor((total_minutes / 60) % 24);
let minutes = total_minutes % 60;
return `${days} days, ${hours} hours and ${minutes} minutes`;
}
function processData(data) {
let outputSection = document.getElementById("output-section");
let totalMinutes = 0;
let hasNoData = [];
let hasData = [];
// add up playtime and note if VN has any playtime data
for (const result of data) {
if (result.vn.length_minutes == null) {
hasNoData.push(result);
}
else {
totalMinutes += result.vn.length_minutes;
hasData.push(result);
}
}
// show total playtime
outputSection.innerHTML += `<h2>Total Playtime: ${timeCalculation(totalMinutes)}</h2>`;
// show any VNs without playtime data
if (hasNoData.length > 0) {
outputSection.innerHTML += "<h3>Some of your VNs have no Playtime Data:</h3>";
for (const element of hasNoData) {
outputSection.innerHTML += `<a href="https://vndb.org/${element.id}">${element.vn.title}</a><br />`
}
}
// show VNs used to add up playtime data
outputSection.innerHTML += `<h3>${hasData.length} VNs used for Calculation:</h3>`;
for (const element of hasData) {
outputSection.innerHTML += `<a href="https://vndb.org/${element.id}">${element.vn.title}</a><br />`
}
}
function processPage(data) {
// collect data and go to next page
vnData = vnData.concat(data);
currentPage++;
}
async function buttonClick() {
let json = "";
currentPage = 1;
let requestCount = 0;
vnData = [];
let morePages = true;
let userid = document.getElementById("userid").value;
// get more pages as long as there's still data to get
while (morePages && requestCount < 100) // limit requests to 100 to avoid things going totally apeshit if something goes wrong
{
// do an http request in probably the ugliest way possible (is this normal by js standards? i sure as fuck hope not)
await fetch("https://api.vndb.org/kana/ulist", {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: `{
"user": "${userid}",
"filters": ["label", "=", ${document.getElementById("labels").value}],
"fields": "id, vn.title, vn.length_minutes",
"page": ${currentPage},
"results": "100"
}`,
}).then(function (response) {
if (response.status != 200) {
window.alert(`Error getting Data. (${response.status})\nPlease make sure the User ID you entered is valid.`);
document.getElementById("show-button").removeAttribute("disabled");
}
else {
return response.json();
}
}).then(function (json) {
console.log(json);
if (json.results.length > 0) {
processPage(json.results);
}
else {
morePages = false;
}
});
requestCount++;
}
// finish up
processData(vnData);
document.getElementById("show-button").removeAttribute("disabled");
}
async function labelsButtonClick() {
let json = "";
let userid = document.getElementById("userid").value;
await fetch("https://api.vndb.org/kana/ulist_labels?user=" + userid, {
method: 'GET',
}).then(function (response) {
if (response.status != 200) {
window.alert(`Error getting Data. (${response.status})\nPlease make sure the User ID you entered is valid.`);
document.getElementById("labels-button").removeAttribute("disabled");
}
else {
return response.json();
}
}).then(function (json) {
if (json.labels.length > 0) {
labelsData = json.labels;
}
});
}
document.getElementById("labels-button").onclick = async function () {
this.disabled = true; // disable button to prevent async fuckups if someone clicks it multiple times
labelsSection = document.getElementById("labels-section");
labelsSection.innerHTML = ""; // empty output just in case this page was used before
await labelsButtonClick();
let innerHTMLtoAdd = ""
innerHTMLtoAdd += `<label for="labels">VN Label: </label>`;
innerHTMLtoAdd += `<select name="label-select" id="labels">`;
labelsData.forEach(label => {
if (label.label == "Finished") { // Default value
innerHTMLtoAdd += `<option selected value=${label.id}>${label.label}</option>`
}
else {
innerHTMLtoAdd += `<option value=${label.id}>${label.label}</option>`
}
})
innerHTMLtoAdd += `</select>`
labelsSection.innerHTML = innerHTMLtoAdd;
const showButton = document.createElement('button');
showButton.type = 'button';
showButton.id = 'show-button';
showButton.textContent = 'Show Me!';
showButton.onclick = function () {
this.disabled = true; // disable button to prevent async fuckups if someone clicks it multiple times
document.getElementById("output-section").innerHTML = ""; // empty output just in case this page was used before
buttonClick();
}
labelsSection.insertAdjacentElement('afterend', showButton);
}
</script>
</html>