Skip to content

Commit

Permalink
Fixed percentage when sorting shorts
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM committed May 31, 2024
1 parent a8cbf3e commit 1298607
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<!--Releasenotes start-->
- Fixed an animation in the popup.
- Fixed the progress percentage displayed on the button when ignoring or only shuffling from shorts.
<!--Releasenotes end-->

## v3.1.3
Expand Down
1 change: 0 additions & 1 deletion src/html/shufflingPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions src/shuffleVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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(
{
Expand All @@ -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);
Expand Down

0 comments on commit 1298607

Please sign in to comment.