From dc0ae1f324212369894bc43362bbe182afc7b4c9 Mon Sep 17 00:00:00 2001 From: Brandon Fowler Date: Mon, 10 Jul 2023 19:13:55 -0400 Subject: [PATCH 1/3] Use exponential search to get largest image index --- mrbeastify.js | 83 ++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/mrbeastify.js b/mrbeastify.js index 2c2b882..d49dcda 100644 --- a/mrbeastify.js +++ b/mrbeastify.js @@ -1,5 +1,6 @@ const imagesPath = "images/"; -const images = []; + +let highestImageIndex; // Apply the overlay function applyOverlay(thumbnailElement, overlayImageUrl, flip) { @@ -46,28 +47,70 @@ function applyOverlayToThumbnails() { }); } +// Get the URL of an image +function getImageURL(index) { + return chrome.runtime.getURL(`${imagesPath}${index}.png`); +} + // Get a random image URL from a directory function getRandomImageFromDirectory() { - const randomIndex = Math.floor(Math.random() * images.length); - return images[randomIndex]; + const randomIndex = Math.floor(Math.random() * highestImageIndex) + 1; + + return getImageURL(randomIndex); } -// Checks for all images in the images folder instead of using a preset array, making the extension infinitely scalable -function checkImageExistence(index = 1) { - const testedURL = chrome.runtime.getURL(`${imagesPath}${index}.png`); - fetch(testedURL) - .then((response) => { - // Image exists, add it to the images array - images.push(testedURL); - // Check the next image in the directory - checkImageExistence(index + 1); - }) - .catch((error) => { // The function encountered a missing image. Start applying overlays - setInterval(applyOverlayToThumbnails, 100); - console.log( - "MrBeastify Loaded Successfully, " + (index - 1) + " images detected." - ); - }); +// Checks if an image exists in the image folder +async function checkImageExistence(index = 1) { + const testedURL = getImageURL(index) + + try { + // See if the image exists + await fetch(testedURL); + + // Image exists + return true; + } catch { + // Image does not exist + return false; + } +} + +// Gets the highest index of an image in the image folder starting from 1 +async function getHighestImageIndex() { + // Avoid exponential search for smaller values + i = 4; + + // Increase i until i is greater than the number of images + while (await checkImageExistence(i)) { + i *= 2; + } + + // Possible min and max values + min = i <= 4 ? 1 : i / 2; + max = i; + + // Binary search + while (min <= max) { + // Find the midpoint of possible max and min + let mid = Math.floor((min + max) / 2); + + // Check if the midpoint exists + if (await checkImageExistence(mid)) { + // If it does, next min to check is one greater + min = mid + 1; + } else { + // If it doesn't, max must be at least one less + max = mid - 1; + } + } + + // Max is the size of the image array + highestImageIndex = max; } -checkImageExistence(); \ No newline at end of file +getHighestImageIndex().then(() => { + setInterval(applyOverlayToThumbnails, 100); + console.log( + "MrBeastify Loaded Successfully, " + highestImageIndex + " images detected." + ); +}) \ No newline at end of file From 9de333c0b3d60e25c3dae3162469a6e6c16c600e Mon Sep 17 00:00:00 2001 From: MagicJinn Date: Tue, 11 Jul 2023 23:17:31 +0200 Subject: [PATCH 2/3] refactoring --- mrbeastify.js | 92 ++++++++++++++++++++++++--------------------------- 1 file changed, 43 insertions(+), 49 deletions(-) diff --git a/mrbeastify.js b/mrbeastify.js index d49dcda..cb566c4 100644 --- a/mrbeastify.js +++ b/mrbeastify.js @@ -1,7 +1,5 @@ const imagesPath = "images/"; -let highestImageIndex; - // Apply the overlay function applyOverlay(thumbnailElement, overlayImageUrl, flip) { // Create a new img element for the overlay @@ -12,14 +10,11 @@ function applyOverlay(thumbnailElement, overlayImageUrl, flip) { overlayImage.style.left = "0"; overlayImage.style.width = "100%"; overlayImage.style.height = "100%"; - overlayImage.style.zIndex = "0"; // Ensure overlay is on top - + overlayImage.style.zIndex = "0"; // Ensure overlay is on top but below the time indicator if (flip) { overlayImage.style.transform = "scaleX(-1)"; // Flip the image horizontally } - - // Style the thumbnailElement to handle absolute positioning - thumbnailElement.style.position = "relative"; + thumbnailElement.style.position = "relative"; // Style the thumbnailElement to handle absolute positioning // Append the overlay to the parent of the thumbnail thumbnailElement.parentElement.appendChild(overlayImage); @@ -55,62 +50,61 @@ function getImageURL(index) { // Get a random image URL from a directory function getRandomImageFromDirectory() { const randomIndex = Math.floor(Math.random() * highestImageIndex) + 1; - + return getImageURL(randomIndex); } // Checks if an image exists in the image folder async function checkImageExistence(index = 1) { const testedURL = getImageURL(index) - + try { // See if the image exists await fetch(testedURL); - - // Image exists - return true; + return true // Image exists } catch { - // Image does not exist - return false; + return false // Image does not exist } } +let highestImageIndex; // Gets the highest index of an image in the image folder starting from 1 async function getHighestImageIndex() { - // Avoid exponential search for smaller values - i = 4; - - // Increase i until i is greater than the number of images - while (await checkImageExistence(i)) { - i *= 2; - } - - // Possible min and max values - min = i <= 4 ? 1 : i / 2; - max = i; - - // Binary search - while (min <= max) { - // Find the midpoint of possible max and min - let mid = Math.floor((min + max) / 2); - - // Check if the midpoint exists - if (await checkImageExistence(mid)) { - // If it does, next min to check is one greater - min = mid + 1; - } else { - // If it doesn't, max must be at least one less - max = mid - 1; - } - } - - // Max is the size of the image array - highestImageIndex = max; + // Avoid exponential search for smaller values + let i = 4; + + // Increase i until i is greater than the number of images + while (await checkImageExistence(i)) { + i *= 2; + } + + // Possible min and max values + min = i <= 4 ? 1 : i / 2; + max = i; + + // Binary search + while (min <= max) { + // Find the midpoint of possible max and min + let mid = Math.floor((min + max) / 2); + + // Check if the midpoint exists + if (await checkImageExistence(mid)) { + // If it does, next min to check is one greater + min = mid + 1; + } else { + // If it doesn't, max must be at least one less + max = mid - 1; + } + } + + // Max is the size of the image array + highestImageIndex = max; } -getHighestImageIndex().then(() => { - setInterval(applyOverlayToThumbnails, 100); - console.log( - "MrBeastify Loaded Successfully, " + highestImageIndex + " images detected." - ); -}) \ No newline at end of file +getHighestImageIndex() + .then(() => { + setInterval(applyOverlayToThumbnails, 100); + console.log( + "MrBeastify Loaded Successfully, " + highestImageIndex + " images detected." + ); + }) \ No newline at end of file From 9568fdcecdec209419bf6238fdeb53c4d06cb021 Mon Sep 17 00:00:00 2001 From: Brandon Fowler Date: Wed, 12 Jul 2023 20:32:47 -0400 Subject: [PATCH 3/3] Add missing let statements --- mrbeastify.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mrbeastify.js b/mrbeastify.js index cb566c4..3505c18 100644 --- a/mrbeastify.js +++ b/mrbeastify.js @@ -79,8 +79,8 @@ async function getHighestImageIndex() { } // Possible min and max values - min = i <= 4 ? 1 : i / 2; - max = i; + let min = i <= 4 ? 1 : i / 2; + let max = i; // Binary search while (min <= max) {