Skip to content

Commit

Permalink
code now only starts running after images loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicJinn committed Jul 4, 2023
1 parent 7578a26 commit 1ceb73b
Showing 1 changed file with 18 additions and 25 deletions.
43 changes: 18 additions & 25 deletions mrbeastify.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,27 @@ function applyOverlayToThumbnails() {
});
}

function checkImageAmountInDirectory() { // Checks for all images in the images folder instead of using a preset array, making the extension infinitely scalable
let imageIndex = 1;

function checkImageExistence() {
const testedURL = chrome.runtime.getURL(`${imagesPath}${imageIndex}.png`);
fetch(testedURL)
.then(response => {
if (response.status === 200) {
// Image exists, add it to the images array
images.push(testedURL);
// Check the next image in the directory
imageIndex++;
checkImageExistence();
}
})
.catch(() => {
applyOverlayToThumbnails();
});
}
checkImageExistence();
function checkImageExistence(index = 1) { // Checks for all images in the images folder instead of using a preset array, making the extension infinitely scalable
const testedURL = chrome.runtime.getURL(`${imagesPath}${index}.png`);
fetch(testedURL).then(response => {
if (response.status === 200) {
// Image exists, add it to the images array
images.push(testedURL);
// Check the next image in the directory
checkImageExistence(index + 1);
}
}).catch(error => {
setInterval(applyOverlayToThumbnails, 100);
console.log("MrBeastify Loaded Successfully, " + (index - 1) + " images detected.");
});
}


checkImageExistence();


// Get a random image URL from a directory
function getRandomImageFromDirectory() {
const randomIndex = Math.floor(Math.random() * images.length);
return images[randomIndex];
}

checkImageAmountInDirectory()

console.log("MrBeastify Loaded Successfully");
}

0 comments on commit 1ceb73b

Please sign in to comment.