mirror of
https://git.kittycat.homes/zoe/hugo-battheme.git
synced 2024-08-15 03:25:18 +00:00
38 lines
937 B
JavaScript
38 lines
937 B
JavaScript
const buttons = document.getElementsByClassName("randomword-button");
|
|
|
|
export function randomizeWords() {
|
|
registerButtons();
|
|
}
|
|
|
|
function registerButtons() {
|
|
for (let button of buttons) {
|
|
button.addEventListener("click", function () {
|
|
button.innerHTML = processWordlist(
|
|
button.getAttribute("data-wordlist"),
|
|
button.innerHTML
|
|
);
|
|
// do this so people dont have to click twice
|
|
});
|
|
button.click();
|
|
}
|
|
}
|
|
|
|
// takes all the words and returns only one
|
|
function processWordlist(wordlist, old) {
|
|
let seperated = wordlist.split(",");
|
|
for (let word of wordlist) {
|
|
word = word.trim();
|
|
}
|
|
if (seperated.length <= 0) {
|
|
return "error! empty";
|
|
}
|
|
if (seperated.length == 1) {
|
|
return seperated[0];
|
|
}
|
|
seperated = seperated.filter((e) => e !== old);
|
|
return seperated.random();
|
|
}
|
|
|
|
Array.prototype.random = function () {
|
|
return this[Math.floor(Math.random() * this.length)];
|
|
};
|