-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
238 lines (211 loc) · 8.01 KB
/
index.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
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
"use strict";
const WIKI_BASE = "https://rainverse.wiki";
const API_ENDPOINT = "https://rainverse.wiki/w/api.php";
const RAINVERSE_STORIES = ["Rain", "My Impossible Soulmate"];
/*** ERROR HANDLER ***/
let errorBox = document.querySelector("#errorBox");
let errorPre = document.querySelector("#errorPre");
function handleError(error) {
let text = `${error.name}: ${error.message}`;
if (error.stack) {
text += `\n${error.stack}`;
}
if (errorPre.innerText) {
errorPre.innerText += "\n---\n";
}
errorPre.innerText += text;
errorBox.classList.remove("hide");
throw error;
}
/*** STATUS INDICATOR ***/
let statusWrapper = document.querySelector("#statusWrapper");
let statusLabel = document.querySelector("label[for=status]");
function setStatus(text) {
statusLabel.innerText = text;
}
function hideStatus() {
statusWrapper.remove();
}
/*** BOILERPLATE ***/
async function getCategoryMemberContents(api, category) {
let contents = new Map();
let options = {
action: "query",
generator: "categorymembers",
gcmtype: "page",
gcmtitle: category,
gcmlimit: "max",
prop: "revisions",
rvprop: "content",
rvslots: "*",
};
let contOptions = {};
while (true) {
let data = await api.get({...options, ...contOptions});
for (let page in data.query.pages) {
page = data.query.pages[page];
// Who needs proper continuation code when you could just check if both exist?
if (!page.title || !page.revisions) {
continue;
}
let title = page.title;
let wikitext = page.revisions[0].slots.main.content;
contents.set(title, wikitext);
}
if (!data.continue) {
break;
}
contOptions = data.continue;
}
return contents;
}
// A primitive version of [[Module:Plain text]] because I'm too lazy to copy its code.
// One main difference from that module: This one converts <br> into \n, not ", "
function plainText(wikitext) {
wikitext = wikitext.replaceAll(/<ref\s[^>]+\/>/g, "");
wikitext = wikitext.replaceAll(/<ref(?:\s[^>]+)?>.+?<\/ref>/g, "");
wikitext = wikitext.replaceAll(/<br ?\/?>/g, "\n");
return wikitext;
}
// Best effort date parser...
const MONTH_NAMES = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
function parseDate(date) {
let year, month, day, match;
if (match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date)) {
// YYYY-mm-dd
year = parseInt(match[1], 10);
month = parseInt(match[2], 10);
day = parseInt(match[3], 10);
} else if (match = /^(\d{2})-(\d{2})-(\d{4})$/.exec(date)) {
// dd-mm-YYYY
year = parseInt(match[3], 10);
month = parseInt(match[2], 10);
day = parseInt(match[1], 10);
} else if (match = /^(\d{1,2}),? ([a-z]+)(?:,? (\d{4}))?$/i.exec(date)) {
// dd MMM, YYYY; dd MM
year = parseInt(match[3], 10) || 1000;
month = MONTH_NAMES.indexOf(match[2]) + 1;
day = parseInt(match[1], 10);
} else if (match = /^([a-z]+),? (\d{1,2})(?:,? (\d{4}))?$/i.exec(date)) {
// MMM dd, YYYY; MMM dd
year = parseInt(match[3], 10) || 1000;
month = MONTH_NAMES.indexOf(match[1]) + 1;
day = parseInt(match[2], 10);
} else if (match = /^(\d{4}),? ([a-z]+),? (\d{1,2})$/i.exec(date)) {
// YYYY MMM dd
year = parseInt(match[1], 10);
month = MONTH_NAMES.indexOf(match[2]) + 1;
day = parseInt(match[3], 10);
} else if (match = /^Released in (\d{4})$/.exec(date)) {
// Released in YYYY
// https://rainverse.wiki/wiki/Momokomo?oldid=21531
year = parseInt(match[1], 10);
month = 1;
day = 1;
} else {
throw new Error(`parseDate(): Failed to parse date: ${JSON.stringify(date)}`);
}
if (year < 1000) {
throw new RangeError(`parseDate(): Invalid year: ${year}`);
}
if (month < 1 || month > 12) {
throw new RangeError(`parseDate(): Invalid month: ${month}`);
}
if (day < 1 || day > 31) {
throw new RangeError(`parseDate(): Invalid day: ${day}`);
}
return new Date(year, month - 1, day);
}
function extractDataFromWikitext(wikitext) {
let fullName = /\|\s*full name\s*=([\s\S]*?)\n\s*(?:\||}})/.exec(wikitext);
if (fullName) {
fullName = plainText(fullName[1]).replace(/^[\s\S]*\n/, "").replace(/ \(.+/, "").trim();
}
let dateString = /\|\s*birthDate\s*=([\s\S]*?)\n\s*(?:\||}})/.exec(wikitext);
let dateDate = null;
if (dateString) {
dateString = plainText(dateString[1]).trim();
}
if (dateString) {
dateDate = parseDate(dateString);
}
return {fullName, dateString, dateDate};
}
async function generateTable(api, contents) {
let sortedContents = [];
for (let [title, {stories, wikitext}] of contents.entries()) {
sortedContents.push({
title,
stories,
...extractDataFromWikitext(wikitext),
});
}
sortedContents.sort((a, b) => (b.dateDate || Number.MIN_SAFE_INTEGER) - (a.dateDate || Number.MIN_SAFE_INTEGER));
let wikitext = '{| class="wikitable sortable"\n';
wikitext += "! Character\n";
wikitext += "! From comics\n";
wikitext += "! Date of birth\n";
for (let {title, stories, fullName, dateString, dateDate} of sortedContents) {
wikitext += "|-\n";
wikitext += `| [[${title}|${fullName || title}]]\n`;
wikitext += `| ${stories.join(", ")}\n`;
wikitext += `| data-sort-value=${dateDate ? dateDate.getTime() : Number.MIN_SAFE_INTEGER} | {{#formatdate: ${dateString || ""} | dmy}}\n`;
}
wikitext += "|}";
let data = await api.post({
action: "parse",
text: wikitext,
prop: ["text", "headhtml"],
useskin: "vector",
contentmodel: "wikitext",
debug: (new URLSearchParams(window.location.search)).get("debug") || undefined,
});
return data.parse;
}
/*** MAIN ***/
async function main() {
let api = new API(API_ENDPOINT);
let contents = new Map();
for (let rainverseStory of RAINVERSE_STORIES) {
setStatus(`Fetching characters for ${rainverseStory}...`);
let categoryContents = await getCategoryMemberContents(api, `Category:${rainverseStory} characters`);
for (let [title, wikitext] of categoryContents.entries()) {
let titleContents = contents.get(title);
if (!titleContents) {
titleContents = {stories: [], wikitext};
contents.set(title, titleContents);
}
titleContents.stories.push(rainverseStory);
}
}
setStatus("Creating table...");
let oldHead = document.head;
let oldBody = document.body;
let parse = await generateTable(api, contents);
// Welcome to ✨ War Crimes with Claire ✨
// Fix license link before we undergo metamorphosis
let licenseLink = document.querySelector("#licenseLink");
licenseLink.href = licenseLink.href;
// Undergo metamorphosis
document.documentElement.innerHTML = parse.headhtml.replace("<head>", `<head><base href="${WIKI_BASE}">`).replace(/<link rel="icon" .+?>/, "");
// Remove MediaWiki title
document.head.querySelector("title").remove();
// Activate Javascript (no, script.cloneNode(true) does not work)
for (let script of document.querySelectorAll("script")) {
let newScript = document.createElement("script");
for (let attr of script.attributes) {
newScript.setAttribute(attr.name, attr.value);
}
newScript.append(...script.childNodes);
script.replaceWith(newScript);
}
document.head.append(...oldHead.children);
hideStatus();
document.body.append(...oldBody.children);
let div = document.createElement("div");
div.id = "mw-content-text";
div.className = "mw-body-content";
div.innerHTML = parse.text;
document.body.append(div);
}
main().catch(handleError);