Update 'frontend.js'

This commit is contained in:
nodemixaholic 2024-03-31 19:43:25 +00:00
parent ff666f3716
commit 3e243a9246

View file

@ -1,173 +1,137 @@
//Created by Samuel Lord (NodeMixaholic/Sparksammy) // Created by Samuel Lord (NodeMixaholic/Sparksammy)
//Licensed under Samuel Public License with <3 // Licensed under Samuel Public License with <3
//Just like readTextFile("path/to/file.txt"); except based off of the WWW and needs a full URL. Also: requires JQuery // Functions for commonly used elements
var litext = "";
function returner(valueToReturn) { // Generalized element creation
return valueToReturn; 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 readInternetText(url) { function createDiv(elementID) {
var request = new XMLHttpRequest(); createElement("div", elementID);
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
litext += request.responseText;
}
}
request.open("GET", url, true);
request.send();
} }
var urlParams = new URLSearchParams(window.location.search); // Added, just in case someone wants to do something to the urlparams function createParagraph(elementID, text) {
let elem = createElement("p", elementID);
//Example: readTextFile("path/to/file.txt"); elem.innerText = `${text}`
function readTextFile(file) {
file = file.toString();
let file2read = new File([""], file);
let reader = new FileReader();
let lastReadOutput = reader.readAsText(file2read, "UTF-8");
return lastReadOutput;
} }
var varifyOutput = ""; function createButton(elementID, text) {
function varify(value) { createElement("button", elementID, { innerText: text });
varifyOutput = value;
} }
//Example: readDataFile("path/to/file.txt"); function createImage(elementID, src) {
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) { function readDataFile(file) {
file = file.toString(); file = file.toString();
let file2read = new File([""], file); const fileToRead = new File([""], file);
let reader = new FileReader(); const reader = new FileReader();
let lastReadOutput = reader.readAsDataURL(file2read); return new Promise((resolve, reject) => {
return lastReadOutput; reader.onload = () => resolve(reader.result);
reader.onerror = () => reject(reader.error);
reader.readAsDataURL(fileToRead);
});
} }
function writeToBody(html) { function writeToBody(html) {
document.body.innerHTML += html.toString(); document.body.innerHTML += html.toString();
} }
function overwriteBody(html) { function overwriteBody(html) {
document.body.innerHTML = html.toString(); document.body.innerHTML = html.toString();
} }
function randomPOS(elementID) { function randomPOS(elementID) {
let top=Math.floor(Math.random() * Math.floor(90)); const top = Math.floor(Math.random() * 90);
let left=Math.floor(Math.random() * Math.floor(90)); const left = Math.floor(Math.random() * 90);
document.getElementById(elementID).style.top=top+"%"; document.getElementById(elementID).style.top = `${top}%`;
document.getElementById(elementID).style.left=left+"%" document.getElementById(elementID).style.left = `${left}%`;
} }
function pos(elementID,x,y) { function pos(elementID, x, y) {
let top = y; document.getElementById(elementID).style.top = `${y}%`;
let left = x; document.getElementById(elementID).style.left = `${x}%`;
document.getElementById(elementID).style.top=top+"%";
document.getElementById(elementID).style.left=left+"%"
} }
// Selects a random value in an array // Select a random value in an array (handles non-arrays)
// Example: randomSelectArray(Array); (Change the array!) function randomSelectArray(array) {
function randomSelectArray(avar){ if (Array.isArray(array)) {
var isarray = Array.isArray(avar); const randomIndex = Math.floor(Math.random() * array.length);
if (isarray == true) { return array[randomIndex];
let rnfa = Math.floor(Math.random()*avar.length); } else {
rrfa = avar[rnfa]; console.log(`Error: ${array} is not an Array!`);
return rrfa; return null; // Or throw an error
} else if (isarray == false){
console.log(`Error, ${avar} is not an Array...`);
} }
} }
function sleep(ms) { function sleep(ms) {
setTimeout(function () {}, ms); return new Promise(resolve => setTimeout(resolve, ms));
} }
async function asyncSleep(ms) { async function asyncSleep(ms) {
await new Promise(r => setTimeout(r, ms)); await new Promise(r => setTimeout(r, ms));
} }
// Remade by Sparksammy // Check if a variable is a function
// Example: isFunction(el)
function isFunction(item) { function isFunction(item) {
if (typeof item === 'function') { return typeof item === 'function';
return true;
} else {
return false;
}
} }
function applyCSS(elemID, prop, value) { function applyCSS(elementID, prop, value) {
let e = document.getElementById(elemID); document.getElementById(elementID).style[prop] = value;
e.style[prop] = value;
} }
function createParagraph(elementID) { function writeTimeAndDate(elementID, hourFormat) {
let e = document.createElement("p"); const element = document.getElementById(elementID);
e.setAttribute("id", elementID); const date = new Date();
document.body.appendChild(e); const locale = hourFormat === 24 ? "en-GB" : "en-US";
element.innerText = date.toLocaleString(locale);
} }
function createHeader(num, elementID) { function writeText(elementID, str) {
let e = document.createElement(`h${num}`); document.getElementById(elementID).innerText = String(str);
e.setAttribute("id", elementID);
document.body.appendChild(e);
} }
function createElement(tagName, elementID) { function writeHTML(elementID, str) {
let e = document.createElement(tagName); document.getElementById(elementID).innerHTML = String(str);
e.setAttribute("id", elementID);
document.body.appendChild(e);
}
function alignSelf(elemID, alignDirection) {
let e = document.getElementById(elemID);
e.style.alignSelf = alignDirection;
}
function alignContent(elemID, alignDirection) {
let e = document.getElementById(elemID);
e.style.alignContent = alignDirection;
}
function alignAll(elemID, alignDirection) {
alignSelf(elemID, alignDirection);
alignContent(elemID, alignDirection);
}
function writeTimeAndDate(elemID, hourFormat) {
let e = document.getElementById(elemID);
let d = new Date();
let locale = "en-GB";
if (hourFormat == 24) {
locale = locale; //leave it the same.
} else {
locale = "en-US";
}
let tdStr = d.toLocaleString(locale);
e.innerText = tdStr;
}
function writeText(elemID, str) {
let e = document.getElementById(elemID);
e.innerText = String(str);
}
function writeHTML(elemID, str) {
let e = document.getElementById(elemID);
e.innerHTML = String(str);
} }
function clearPage() { function clearPage() {
document.body.innerHTML = ""; document.body.innerHTML = "";
} }
function createList(listID, jsArray) { function createList(listID, items) {
let listParent = document.createElement("ul"); const list = document.createElement("ul");
listParent.setAttribute("id", listID); list.id = listID;
document.body.appendChild(listParent); document.body.appendChild(list);
jsArray.forEach(item => { items.forEach(item => (list.innerHTML += `<li>${item}</li>`));
listParent.innerHTML += `<li>${item}</li>`;
});
} }
function addToList(listID, jsArray) { function addToList(listID, jsArray) {
@ -217,7 +181,7 @@ function fadeIn(el, ms) {
function spin(el, ms){ function spin(el, ms){
elem = getElementById(el); elem = getElementById(el);
for (i = 0; i < (ms / 360); i++) { for (i = 0; i < (ms / 361); i++) {
elem.style.transform = 'rotate(' + i + 'deg)'; elem.style.transform = 'rotate(' + i + 'deg)';
} }
} }