Wobbly-Website/script.js

90 lines
2.6 KiB
JavaScript

function randomBackground(backgrounds) {
let background = selectRandom(backgrounds);
console.log("Choosen random background image:", background);
if (!background.includes("backgrounds/")) {
background = "backgrounds/" + background
}
document.getElementById("background").style.backgroundImage = "url(" + background + ")";
// let div_height = document.getElementById("background").offsetHeight;
// let half_div_height = (0 - div_height) / 2;
// document.getElementById("background").style.backgroundPosition = "0% " + half_div_height.toString() + "px";
}
function selectRandom(array) {
let choosen = array[Math.floor(Math.random() * array.length)];
return choosen;
}
function copyText(text) {
try {
navigator.clipboard.writeText(text);
console.log('Content copied to clipboard: "' + text + '"');
} catch (error) {
console.error('Failed to copy "' + text + '":', error);
}
}
function strBetweenStrings(startStr, endStr, str) {
pos = str.indexOf(startStr) + startStr.length;
return str.substring(pos, str.indexOf(endStr, pos));
}
function getStringOfPageContent(url, callbackFunction) {
$.get(url, function(data) {
callbackFunction(data);
});
};
function getFilesInFolder(pageContent) { // Needs auto file indexing
// console.log(pageContent);
let contentArray = pageContent.split("\n");
let files = [];
for (let line in contentArray) {
if (contentArray[line].includes("<li>")) {
files.push(strBetweenStrings('"', '"', contentArray[line]));
}
}
files.shift();
// console.log(files);
return files;
};
function setBackground(bla = "") {
let currentDate = new Date();
let currentMonth = currentDate.getMonth();
let currentDay = currentDate.getDay();
if (currentMonth == 9) {
if (currentDay > 0 && currentDay < 20) {
getStringOfPageContent(bla + "backgrounds/halloween", function(pageContent) {
let backgrounds = getFilesInFolder(pageContent);
let backgroundNumber = 0;
while (backgroundNumber < backgrounds.length) {
backgrounds[backgroundNumber] = bla + "backgrounds/halloween/" + backgrounds[backgroundNumber];
backgroundNumber++;
}
randomBackground(backgrounds);
});
}
} else {
getStringOfPageContent("backgrounds", function(pageContent) {
let backgrounds = getFilesInFolder(pageContent);
randomBackground(backgrounds);
});
}
}
// getStringOfPageContent("backgrounds", getFilesInFolder)