js-plus-plus/frontend.js

225 lines
5.4 KiB
JavaScript
Raw Normal View History

2024-03-31 14:43:25 -05:00
// Created by Samuel Lord (NodeMixaholic/Sparksammy)
// Licensed under Samuel Public License with <3
2024-02-28 18:40:33 -06:00
2024-03-31 14:43:25 -05:00
// Functions for commonly used elements
2024-02-28 18:40:33 -06:00
2024-03-31 14:43:25 -05:00
// 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;
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function createDiv(elementID) {
createElement("div", elementID);
2024-03-09 13:56:28 -06:00
}
2024-03-31 14:43:25 -05:00
function createParagraph(elementID, text) {
let elem = createElement("p", elementID);
elem.innerText = `${text}`
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function createButton(elementID, text) {
createElement("button", elementID, { innerText: text });
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function createImage(elementID, src) {
createElement("img", elementID, { src: src });
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
// Get URL parameters
function getURLParameters() {
return new URLSearchParams(window.location.search);
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
// 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");
});
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
// 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);
});
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function writeToBody(html) {
document.body.innerHTML += html.toString();
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function overwriteBody(html) {
document.body.innerHTML = html.toString();
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
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}%`;
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function pos(elementID, x, y) {
document.getElementById(elementID).style.top = `${y}%`;
document.getElementById(elementID).style.left = `${x}%`;
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
// 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
}
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
async function asyncSleep(ms) {
await new Promise(r => setTimeout(r, ms));
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
// Check if a variable is a function
function isFunction(item) {
return typeof item === 'function';
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function applyCSS(elementID, prop, value) {
document.getElementById(elementID).style[prop] = value;
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
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);
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function writeText(elementID, str) {
document.getElementById(elementID).innerText = String(str);
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function writeHTML(elementID, str) {
document.getElementById(elementID).innerHTML = String(str);
2024-02-28 18:40:33 -06:00
}
function clearPage() {
2024-03-31 14:43:25 -05:00
document.body.innerHTML = "";
2024-02-28 18:40:33 -06:00
}
2024-03-31 14:43:25 -05:00
function createList(listID, items) {
const list = document.createElement("ul");
list.id = listID;
document.body.appendChild(list);
items.forEach(item => (list.innerHTML += `<li>${item}</li>`));
2024-02-28 18:40:33 -06:00
}
function addToList(listID, jsArray) {
2024-03-09 13:56:28 -06:00
let listParent = document.getElementById(listID);
2024-02-28 18:40:33 -06:00
jsArray.forEach(item => {
2024-03-09 13:56:28 -06:00
listParent.innerHTML += `<li>${item}</li>`;
});
2024-02-28 18:40:33 -06:00
}
2024-03-09 13:56:28 -06:00
// Gets the value of an attribute
2024-02-28 18:40:33 -06:00
// Example: getAttribute(document.getElementById("link"), "href");
function getAttribute(el, att) {
2024-03-01 17:50:42 -06:00
let result = el.getAttribute(att);
2024-02-28 18:40:33 -06:00
return result;
}
// Show/Hide Elements
// Example: hideShow(el)
function hideShow(el) {
if (el.style.display == 'none') {
el.style.display = '';
} else{
el.style.display = 'none';
}
}
// Example: fadeOut(el, 1000)
function fadeOut(el, ms) {
2024-03-09 13:56:28 -06:00
let elem = getElementById(el);
ms = parseInt(ms);
2024-02-28 18:40:33 -06:00
for (i = 0; i < (ms + 1); i++) {
2024-03-09 13:56:28 -06:00
elem.style.opacity -= (i / 100);
sleep(1);
2024-02-28 18:40:33 -06:00
}
}
// Example: fadeIn(el, 1000);
function fadeIn(el, ms) {
2024-03-09 13:56:28 -06:00
elem = getElementById(el);
2024-02-28 18:40:33 -06:00
elem.style.opacity = 0;
ms = parseInt(ms);
for (i = 0; i < (ms + 1); i++) {
2024-03-09 13:56:28 -06:00
elem.style.opacity += (i / 100);
sleep(1);
2024-02-28 18:40:33 -06:00
}
}
2024-03-09 13:56:28 -06:00
2024-02-28 18:40:33 -06:00
function spin(el, ms){
2024-03-09 13:56:28 -06:00
elem = getElementById(el);
2024-03-31 14:43:25 -05:00
for (i = 0; i < (ms / 361); i++) {
2024-02-28 18:40:33 -06:00
elem.style.transform = 'rotate(' + i + 'deg)';
}
}
//Eval alternative
//Example: exec("alert('Hello, world!')")
function exec(jsCode) {
2024-03-09 13:56:28 -06:00
let js = jsCode.toString();
setTimeout(js, 1);
2024-02-28 18:40:33 -06:00
}
2024-03-01 17:50:42 -06:00
function requir3(jsURL) {
let req = readInternetText(jsURL);
exec(req);
2024-02-28 18:40:33 -06:00
}
// Example: getFileSize(path/to/file)
function getFileSize(file) {
file = file.toString();
2024-03-01 17:50:42 -06:00
file = new File([""], file);
2024-02-28 18:40:33 -06:00
return file.getFileSize;
}
function lastModified(file) {
file = file.toString();
2024-03-01 17:50:42 -06:00
file = new File([""], file);
2024-02-28 18:40:33 -06:00
return file.lastModified;
}
2024-03-10 14:43:59 -05:00
// Example: playAudio("https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3", 0.4);
2024-02-28 18:40:33 -06:00
function playAudio(audio, speed) {
2024-03-01 17:50:42 -06:00
let ma = new Audio(audio);
2024-02-28 18:40:33 -06:00
ma.playbackRate = speed;
2024-03-09 13:56:28 -06:00
ma.play();
2024-02-28 18:40:33 -06:00
}
// Example: redir(url);
function redir(url) {
window.location.href = url.toString();
}