-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
100 lines (92 loc) · 2.62 KB
/
Copy pathindex.html
File metadata and controls
100 lines (92 loc) · 2.62 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Probabilistic Novel Generator</title>
<style>
body {
font-family: "American Typewriter", "Times New Roman", serif;
font-size: 12px;
background: white;
color: black;
margin: 20px;
}
#generate {
font-family: "American Typewriter", "Times New Roman", serif;
font-size: 12px;
padding: 6px 12px;
margin-bottom: 10px;
}
#output {
white-space: pre-wrap;
}
</style>
</head>
<body>
<button id="generate">Loading time…</button>
<div id="output">Loading sources…</div>
<script>
// Update button label with device time
function updateTime() {
const now = new Date();
const timeStr = now.toLocaleTimeString();
document.getElementById("generate").textContent = timeStr;
}
setInterval(updateTime, 1000);
updateTime();
// Load sources
async function loadSources() {
const sources = {
bible: "pg10681-images.html",
dict: "pg30-images.html",
jsons: ["wordnet_1.json", "wordnet_2.json"]
};
async function fetchText(url) {
try {
const r = await fetch(url);
return await r.text();
} catch (e) {
console.error("Error fetching", url, e);
return "";
}
}
const bible = await fetchText(sources.bible);
const dict = await fetchText(sources.dict);
const jsonParts = await Promise.all(sources.jsons.map(fetchText));
const jsonText = jsonParts.join("\n");
return { bible, dict, jsonText };
}
// Generate random text
function generateNovel(sources) {
const rand = Math.random();
let chosen;
if (rand < 0.09) {
chosen = sources.bible;
} else if (rand < 0.59) {
chosen = sources.dict;
} else {
chosen = sources.jsonText;
}
const words = chosen.split(/\s+/);
let out = [];
for (let i = 0; i < 100; i++) {
out.push(words[Math.floor(Math.random() * words.length)]);
}
return out.join(" ");
}
// Main
let sourcesCache = null;
loadSources().then(src => {
sourcesCache = src;
document.getElementById("output").textContent = "Sources loaded. Click the time button to generate.";
});
document.getElementById("generate").addEventListener("click", () => {
if (sourcesCache) {
document.getElementById("output").textContent = generateNovel(sourcesCache);
} else {
document.getElementById("output").textContent = "Still loading sources…";
}
});
</script>
</body>
</html>