From 129860748aeb762cba4890511f0e95b670789b00 Mon Sep 17 00:00:00 2001 From: NikkelM <57323886+NikkelM@users.noreply.github.com> Date: Fri, 31 May 2024 17:39:54 +0100 Subject: [PATCH] Fixed percentage when sorting shorts --- CHANGELOG.md | 1 + src/html/shufflingPage.js | 1 - src/shuffleVideo.js | 18 +++++++++++++----- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5a50d4b..d3abd005 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Fixed an animation in the popup. +- Fixed the progress percentage displayed on the button when ignoring or only shuffling from shorts. ## v3.1.3 diff --git a/src/html/shufflingPage.js b/src/html/shufflingPage.js index 54bdefd0..a4cfdaef 100644 --- a/src/html/shufflingPage.js +++ b/src/html/shufflingPage.js @@ -93,7 +93,6 @@ async function shuffleButtonClicked() { setDOMTextWithDelay(shuffleButtonTextElement, "Still shuffling...", 20000, () => { return ((shuffleButtonTextElement.innerText === "Should be done soon..." || shuffleButtonTextElement.innerText === "Fetching: 100%")); }); } - // await delay(100000); await chooseRandomVideo(configSync.currentChannelId, true, shuffleButtonTextElement); // Focus this tab when the shuffle completes diff --git a/src/shuffleVideo.js b/src/shuffleVideo.js index 0c3b157f..c7317f51 100644 --- a/src/shuffleVideo.js +++ b/src/shuffleVideo.js @@ -10,6 +10,9 @@ import { } from "./utils.js"; import { configSync, setSyncStorageValue, getUserQuotaRemainingToday } from "./chromeStorage.js"; +// The time when the shuffle started +let shuffleStartTime = null; + // --------------- Public --------------- // Chooses a random video uploaded on the current YouTube channel export async function chooseRandomVideo(channelId, firedFromPopup, progressTextElement) { @@ -30,6 +33,8 @@ export async function chooseRandomVideo(channelId, firedFromPopup, progressTextE }, 20000); /* c8 ignore stop */ + shuffleStartTime = new Date(); + // Each user has a set amount of quota they can use per day. // If they exceed it, they need to provide a custom API key, or wait until the quota resets the next day let userQuotaRemainingToday = await getUserQuotaRemainingToday(); @@ -824,10 +829,6 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd if (playlistInfo["videos"]["unknownType"][randomVideo] !== undefined) { const videoIsShort = await isShort(randomVideo); - // We display either the percentage of videos processed or the percentage of videos chosen (vs. needed), whichever is higher - const percentage = Math.max(Math.round(chosenVideos.length / numVideosToChoose * 100), Math.round(numVideosProcessed / initialTotalNumVideos * 100)); - updateProgressTextElement(progressTextElement, `\xa0Sorting: ${percentage}%`, `${percentage}%`); - // What follows is dependent on if the video is a short or not, and the user's settings // Case 1: !isShort && ignoreShorts => Success if (!videoIsShort && configSync.shuffleIgnoreShortsOption == "2") { @@ -879,7 +880,7 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd // We need to decrement i, as we did not choose a video in this iteration i--; - /* c8 ignore start - This should never happen, but we want to test it anyway */ + /* c8 ignore start - This should never happen */ } else { throw new RandomYoutubeVideoError( { @@ -897,6 +898,13 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd chosenVideos.push(randomVideo); videosToShuffle.splice(videosToShuffle.indexOf(randomVideo), 1); } + + // Only if the shuffle started more than 1 second ago, update the button text + if (new Date() - shuffleStartTime > 1000) { + // We display either the percentage of videos processed or the percentage of videos chosen (vs. needed), whichever is higher + const percentage = Math.max(Math.round(chosenVideos.length / numVideosToChoose * 100), Math.round(numVideosProcessed / initialTotalNumVideos * 100)); + updateProgressTextElement(progressTextElement, `\xa0Sorting: ${percentage}%`, `${percentage}%`); + } } else { // We are not ignoring shorts and the video exists chosenVideos.push(randomVideo);