diff --git a/frontend.js b/frontend.js new file mode 100644 index 0000000..c3c97e5 --- /dev/null +++ b/frontend.js @@ -0,0 +1,268 @@ +//Created by Samuel Lord (NodeMixaholic/Sparksammy) +//Licensed under MIT with <3 + +//Just like readTextFile("path/to/file.txt"); except based off of the WWW and needs a full URL. Also: requires JQuery +var litext; +var itext = ""; +function returner(valueToReturn) { + return valueToReturn; +} + +function readInternetText(url) { + var request = new XMLHttpRequest(); + request.onreadystatechange = function() { + src = request.responseText; + itext += src; + setTimeout("return itext", 13); + } + + 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 + + +//Example: readTextFile("path/to/file.txt"); +function readTextFile(file) { + file = file.toString(); + var file2read = new File([""], file); + var reader = new FileReader(); + var lastReadOutput = reader.readAsText(file2read, "UTF-8"); + return lastReadOutput; + } +var varifyOutput = ""; +function varify(value) { + varifyOutput = value; +} + +//Example: readDataFile("path/to/file.txt"); +function readDataFile(file) { + file = file.toString(); + var file2read = new File([""], file); + var reader = new FileReader(); + var lastReadOutput = reader.readAsDataURL(file2read); + return lastReadOutput; +} + +function writeToBody(html) { + document.append(html.toString()) +} + +function overwriteBody(html) { + document.body.innerHTML = html.toString() +} + +function randomPOS(elementID) { + var top=Math.floor(Math.random() * Math.floor(90)); + var left=Math.floor(Math.random() * Math.floor(90)); + document.getElementById(elementID.toString).style.top=top+"%"; + document.getElementById(elementID.toString).style.left=left+"%" +} + +function pos(elementID,x,y) { + var top=y; + var left=x; + document.getElementById(elementID.toString).style.top=top+"%"; + document.getElementById(elementID.toString).style.left=left+"%" +} + +// Selects a random value in a array +// Example: randomSelectArray(Array); (Change the array!) +function randomSelectArray(avar){ + var isarray = Array.isArray(avar) + if (isarray == true) { + var rnfa = Math.floor(Math.random()*avar.length); + var rrfa = avar[rnfa] + return rrfa; + } else if (isarray == false){ + console.log(`Error, ${avar} is not a Array...`); + } +} + + +function sleep(ms) { + setTimeout(function () {}, ms) +} + +async function asyncSleep(ms) { + await new Promise(r => setTimeout(r, ms)); +} + +// Remade by Sparksammy +// Example: isFunction(el) +function isFunction(item) { + if (typeof item === 'function') { + return true; + } else { + return false; + } +} + +function applyCSS(elemID, prop, value) { + let e = document.getElementById(elemID) + e.style[prop] = value +} + +function createParagraph(elementID) { + let e = document.createElement("p") + e.setAttribute("id", elementID) + document.body.appendChild(e) +} + +function createHeader(num, elementID) { + let e = document.createElement(`h${num}`) + e.setAttribute("id", elementID) + document.body.appendChild(e) +} + +function createElement(tagName, elementID) { + let e = document.createElement(tagName) + 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() { + document.body.innerHTML = "" +} + +function createList(listID, jsArray) { + let listParent = document.createElement("ul") + listParent.setAttribute("id", listID) + document.body.appendChild(listParent) + jsArray.forEach(item => { + listParent.innerHTML = listParent.innerHTML + `