js-plus-plus/frontend.js

261 lines
6.4 KiB
JavaScript
Raw Normal View History

2024-02-28 18:40:33 -06:00
//Created by Samuel Lord (NodeMixaholic/Sparksammy)
2024-02-28 18:41:08 -06:00
//Licensed under Samuel Public License with <3
2024-02-28 18:40:33 -06:00
//Just like readTextFile("path/to/file.txt"); except based off of the WWW and needs a full URL. Also: requires JQuery
2024-03-09 13:56:28 -06:00
var litext = "";
2024-02-28 18:40:33 -06:00
function returner(valueToReturn) {
return valueToReturn;
}
function readInternetText(url) {
2024-03-09 13:56:28 -06:00
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState == 4 && request.status == 200) {
litext += request.responseText;
}
}
request.open("GET", url, true);
request.send();
2024-02-28 18:40:33 -06:00
}
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();
2024-03-01 17:50:42 -06:00
let file2read = new File([""], file);
let reader = new FileReader();
let lastReadOutput = reader.readAsText(file2read, "UTF-8");
2024-02-28 18:40:33 -06:00
return lastReadOutput;
2024-03-09 13:56:28 -06:00
}
2024-02-28 18:40:33 -06:00
var varifyOutput = "";
function varify(value) {
varifyOutput = value;
}
//Example: readDataFile("path/to/file.txt");
function readDataFile(file) {
file = file.toString();
2024-03-01 17:50:42 -06:00
let file2read = new File([""], file);
let reader = new FileReader();
let lastReadOutput = reader.readAsDataURL(file2read);
2024-02-28 18:40:33 -06:00
return lastReadOutput;
}
function writeToBody(html) {
2024-03-09 13:56:28 -06:00
document.body.innerHTML += html.toString();
2024-02-28 18:40:33 -06:00
}
function overwriteBody(html) {
2024-03-09 13:56:28 -06:00
document.body.innerHTML = html.toString();
2024-02-28 18:40:33 -06:00
}
function randomPOS(elementID) {
2024-03-01 17:50:42 -06:00
let top=Math.floor(Math.random() * Math.floor(90));
let left=Math.floor(Math.random() * Math.floor(90));
2024-03-09 13:56:28 -06:00
document.getElementById(elementID).style.top=top+"%";
document.getElementById(elementID).style.left=left+"%"
2024-02-28 18:40:33 -06:00
}
function pos(elementID,x,y) {
2024-03-01 17:50:42 -06:00
let top = y;
let left = x;
2024-03-09 13:56:28 -06:00
document.getElementById(elementID).style.top=top+"%";
document.getElementById(elementID).style.left=left+"%"
2024-02-28 18:40:33 -06:00
}
2024-03-09 13:56:28 -06:00
// Selects a random value in an array
2024-02-28 18:40:33 -06:00
// Example: randomSelectArray(Array); (Change the array!)
function randomSelectArray(avar){
2024-03-09 13:56:28 -06:00
var isarray = Array.isArray(avar);
2024-02-28 18:40:33 -06:00
if (isarray == true) {
2024-03-01 17:50:42 -06:00
let rnfa = Math.floor(Math.random()*avar.length);
2024-03-09 13:56:28 -06:00
rrfa = avar[rnfa];
2024-02-28 18:40:33 -06:00
return rrfa;
} else if (isarray == false){
2024-03-09 13:56:28 -06:00
console.log(`Error, ${avar} is not an Array...`);
2024-02-28 18:40:33 -06:00
}
}
function sleep(ms) {
2024-03-09 13:56:28 -06:00
setTimeout(function () {}, ms);
2024-02-28 18:40:33 -06:00
}
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) {
2024-03-09 13:56:28 -06:00
let e = document.getElementById(elemID);
e.style[prop] = value;
2024-02-28 18:40:33 -06:00
}
function createParagraph(elementID) {
2024-03-09 13:56:28 -06:00
let e = document.createElement("p");
e.setAttribute("id", elementID);
document.body.appendChild(e);
2024-02-28 18:40:33 -06:00
}
function createHeader(num, elementID) {
2024-03-09 13:56:28 -06:00
let e = document.createElement(`h${num}`);
e.setAttribute("id", elementID);
document.body.appendChild(e);
2024-02-28 18:40:33 -06:00
}
function createElement(tagName, elementID) {
2024-03-09 13:56:28 -06:00
let e = document.createElement(tagName);
e.setAttribute("id", elementID);
document.body.appendChild(e);
2024-02-28 18:40:33 -06:00
}
function alignSelf(elemID, alignDirection) {
2024-03-09 13:56:28 -06:00
let e = document.getElementById(elemID);
e.style.alignSelf = alignDirection;
2024-02-28 18:40:33 -06:00
}
function alignContent(elemID, alignDirection) {
2024-03-09 13:56:28 -06:00
let e = document.getElementById(elemID);
e.style.alignContent = alignDirection;
2024-02-28 18:40:33 -06:00
}
function alignAll(elemID, alignDirection) {
2024-03-09 13:56:28 -06:00
alignSelf(elemID, alignDirection);
alignContent(elemID, alignDirection);
2024-02-28 18:40:33 -06:00
}
function writeTimeAndDate(elemID, hourFormat) {
2024-03-09 13:56:28 -06:00
let e = document.getElementById(elemID);
let d = new Date();
let locale = "en-GB";
2024-02-28 18:40:33 -06:00
if (hourFormat == 24) {
2024-03-09 13:56:28 -06:00
locale = locale; //leave it the same.
2024-02-28 18:40:33 -06:00
} else {
2024-03-09 13:56:28 -06:00
locale = "en-US";
2024-02-28 18:40:33 -06:00
}
2024-03-09 13:56:28 -06:00
let tdStr = d.toLocaleString(locale);
e.innerText = tdStr;
2024-02-28 18:40:33 -06:00
}
function writeText(elemID, str) {
2024-03-09 13:56:28 -06:00
let e = document.getElementById(elemID);
e.innerText = String(str);
2024-02-28 18:40:33 -06:00
}
function writeHTML(elemID, str) {
2024-03-09 13:56:28 -06:00
let e = document.getElementById(elemID);
e.innerHTML = String(str);
2024-02-28 18:40:33 -06:00
}
function clearPage() {
2024-03-09 13:56:28 -06:00
document.body.innerHTML = "";
2024-02-28 18:40:33 -06:00
}
function createList(listID, jsArray) {
2024-03-09 13:56:28 -06:00
let listParent = document.createElement("ul");
listParent.setAttribute("id", listID);
document.body.appendChild(listParent);
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
}
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-02-28 18:40:33 -06:00
for (i = 0; i < (ms / 360); i++) {
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();
}