-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
64 lines (60 loc) · 2.39 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Random Name Generator</title>
<script>
"use strict";
const selectRandom = (x /* @type {Iterable<any>} */) => {
const a = Array.isArray(x) ? x : [...x];
const i = Math.random() * (a.length - 1) | 0;
console.debug(i);
return a[i];
};
const types = {
expectArray(x) {
return Array.isArray(x) ? x : (() => { throw new TypeError("Response isn't an array"); } )();
}
};
const getModifierJSON = () => fetch("https://raw.githubusercontent.com/KisaragiEffective/random-name-generator/master/data/modifier.json")
.then(x => x.json())
.then(types.expectArray);
const getNounJson = () => fetch("https://raw.githubusercontent.com/KisaragiEffective/random-name-generator/master/data/noun.json")
.then(x => x.json())
.then(types.expectArray);
const jsonCache = Object.seal({
modifier: undefined,
noun: undefined,
});
window.addEventListener("DOMContentLoaded", () => {
console.debug("loaded");
document.getElementById("new").addEventListener("click", () => {
console.debug("clicked");
Promise.all([
jsonCache.modifier ?? getModifierJSON(),
jsonCache.noun ?? getNounJson()
])
.then(([m, n]) => {
jsonCache.modifier ??= m;
jsonCache.noun ??= n;
document.getElementById("purgecache").disabled = false;
document.getElementById("gen").value = `${selectRandom(m)}-${selectRandom(n)}`;
})
.catch(console.error);
});
document.getElementById("purgecache").addEventListener("click", () => {
jsonCache.modifier = undefined;
jsonCache.noun = undefined;
document.getElementById("purgecache").disabled = true;
});
});
</script>
</head>
<body>
<label>
<input type="text" readonly id="gen" placeholder="Click [New Item] to get one...">
<input type="button" value="New Item" id="new">
<input type="button" value="Purge cache" id="purgecache" disabled>
</label>
</body>
</html>