-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (30 loc) · 851 Bytes
/
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
const output = document.getElementById("output");
let words = [];
fetch("word_generation/output.txt")
.then(response => response.text())
.then(text => {
words = text.split("\n");
//get title from url
let results = new RegExp("[\?&]id=([^&#]*)").exec(window.location.href);
if (results) {
displayTitle(num);
} else {
displayTitle(getRandomId());
}
}).catch(error => {
console.error(error);
});
function getRandomId() {
return Math.floor(Math.random() * words.length);
}
function displayTitle(id) {
output.innerText = getTitleById(id);
updateUrl(id);
}
function getTitleById(id) {
return words[id];
}
function updateUrl(id) {
const url = window.location.href.split("?")[0] + "?id=" + id.toString();
history.replaceState("", url, url);
}