// Created by Samuel Lord (NodeMixaholic/Sparksammy) // Licensed under Samuel Public License with <3 // Functions for commonly used elements // Generalized element creation function createElement(tagName, elementID, attributes = {}) { const element = document.createElement(tagName); element.id = elementID; for (const [name, value] of Object.entries(attributes)) { element.setAttribute(name, value); } document.body.appendChild(element); return element; } function createDiv(elementID) { createElement("div", elementID); } function createParagraph(elementID, text) { let elem = createElement("p", elementID); elem.innerText = `${text}` return elem } function createButton(elementID, text) { let elem = createElement("button", elementID); elem.innerText = `${text}` return elem; } function createImage(elementID, src) { return createElement("img", elementID, { src: src }); } // Get URL parameters function getURLParameters() { return new URLSearchParams(window.location.search); } // Read file contents function readFileContents(file) { file = file.toString(); const fileToRead = new File([""], file); const reader = new FileReader(); return new Promise((resolve, reject) => { reader.onload = () => resolve(reader.result); reader.onerror = () => reject(reader.error); reader.readAsText(fileToRead, "UTF-8"); }); } // Read data file as data URL function readDataFile(file) { file = file.toString(); const fileToRead = new File([""], file); const reader = new FileReader(); return new Promise((resolve, reject) => { reader.onload = () => resolve(reader.result); reader.onerror = () => reject(reader.error); reader.readAsDataURL(fileToRead); }); } function writeToBody(html) { document.body.innerHTML += html.toString(); } function overwriteBody(html) { document.body.innerHTML = html.toString(); } function randomPOS(elementID) { const top = Math.floor(Math.random() * 90); const left = Math.floor(Math.random() * 90); document.getElementById(elementID).style.top = `${top}%`; document.getElementById(elementID).style.left = `${left}%`; } function pos(elementID, x, y) { document.getElementById(elementID).style.top = `${y}%`; document.getElementById(elementID).style.left = `${x}%`; } // Select a random value in an array (handles non-arrays) function randomSelectArray(array) { if (Array.isArray(array)) { const randomIndex = Math.floor(Math.random() * array.length); return array[randomIndex]; } else { console.log(`Error: ${array} is not an Array!`); return null; // Or throw an error } } function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } async function asyncSleep(ms) { await new Promise(r => setTimeout(r, ms)); } // Check if a variable is a function function isFunction(item) { return typeof item === 'function'; } function applyCSS(elementID, prop, value) { document.getElementById(elementID).style[prop] = value; } function writeTimeAndDate(elementID, hourFormat) { const element = document.getElementById(elementID); const date = new Date(); const locale = hourFormat === 24 ? "en-GB" : "en-US"; element.innerText = date.toLocaleString(locale); } function writeText(elementID, str) { document.getElementById(elementID).innerText = String(str); } function writeHTML(elementID, str) { document.getElementById(elementID).innerHTML = String(str); } function clearPage() { document.body.innerHTML = ""; } function createList(listID, items) { const list = document.createElement("ul"); list.id = listID; document.body.appendChild(list); items.forEach(item => (list.innerHTML += `