@@ -24,10 +24,10 @@ export async function chooseRandomVideo(channelId, firedFromPopup, progressTextE
2424 try {
2525 // While chooseRandomVideo is running, we need to keep the service worker alive
2626 // Otherwise, it will get stopped after 30 seconds and we will get an error if fetching the videos takes longer
27- // So every 25 seconds, we send a message to the service worker to keep it alive
27+ // So every 20 seconds, we send a message to the service worker to keep it alive
2828 var keepServiceWorkerAlive = setInterval ( ( ) => {
2929 chrome . runtime . sendMessage ( { command : "connectionTest" } ) ;
30- } , 25000 ) ;
30+ } , 20000 ) ;
3131 /* c8 ignore stop */
3232
3333 // Each user has a set amount of quota they can use per day.
@@ -123,7 +123,7 @@ export async function chooseRandomVideo(channelId, firedFromPopup, progressTextE
123123
124124 let chosenVideos ;
125125 var encounteredDeletedVideos ;
126- ( { chosenVideos, playlistInfo, shouldUpdateDatabase, encounteredDeletedVideos } = await chooseRandomVideosFromPlaylist ( playlistInfo , channelId , shouldUpdateDatabase ) ) ;
126+ ( { chosenVideos, playlistInfo, shouldUpdateDatabase, encounteredDeletedVideos } = await chooseRandomVideosFromPlaylist ( playlistInfo , channelId , shouldUpdateDatabase , progressTextElement ) ) ;
127127
128128 // Save the playlist to the database and locally
129129 playlistInfo = await handlePlaylistDatabaseUpload ( playlistInfo , uploadsPlaylistId , shouldUpdateDatabase , databaseSharing , encounteredDeletedVideos ) ;
@@ -674,7 +674,7 @@ async function isShort(videoId) {
674674 return videoIsShort ;
675675}
676676
677- // Requests the API key from the background script
677+ // Requests an API key from the background script
678678async function getAPIKey ( useAPIKeyAtIndex = null ) {
679679 const msg = {
680680 command : "getAPIKey" ,
@@ -701,7 +701,7 @@ async function getAPIKey(useAPIKeyAtIndex = null) {
701701 return { APIKey, isCustomKey, keyIndex } ;
702702}
703703
704- async function chooseRandomVideosFromPlaylist ( playlistInfo , channelId , shouldUpdateDatabase ) {
704+ async function chooseRandomVideosFromPlaylist ( playlistInfo , channelId , shouldUpdateDatabase , progressTextElement ) {
705705 let activeShuffleFilterOption = configSync . channelSettings [ channelId ] ?. activeOption ?? "allVideosOption" ;
706706 let activeOptionValue ;
707707
@@ -763,16 +763,21 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd
763763
764764 console . log ( `Choosing ${ numVideosToChoose } random video(s).` ) ;
765765
766+ // We keep track of the progress of determining the video types if there is a shorts handling filter, to display on the button
767+ let numVideosProcessed = 0 ;
768+ const initialTotalNumVideos = videosToShuffle . length ;
769+
766770 // We use this label to break out of both the for loop and the while loop if there are no more videos after encountering a deleted video
767771 outerLoop:
768772 for ( let i = 0 ; i < numVideosToChoose ; i ++ ) {
769773 if ( videosToShuffle . length === 0 ) {
770- // All available videos were chosen from , so we need to terminate the loop early
774+ // All available videos were chosen, so we need to terminate the loop early
771775 console . log ( `No more videos to choose from (${ numVideosToChoose - i } videos too few uploaded on channel).` ) ;
772776 break outerLoop;
773777 }
774778
775779 randomVideo = videosToShuffle [ Math . floor ( Math . random ( ) * videosToShuffle . length ) ] ;
780+ numVideosProcessed ++ ;
776781
777782 // If the video does not exist, remove it from the playlist and choose a new one, until we find one that exists
778783 if ( ! await testVideoExistence ( randomVideo , allVideos [ randomVideo ] ) ) {
@@ -786,6 +791,7 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd
786791 // Remove the deleted video from the videosToShuffle array and choose a new random video
787792 videosToShuffle . splice ( videosToShuffle . indexOf ( randomVideo ) , 1 ) ;
788793 randomVideo = videosToShuffle [ Math . floor ( Math . random ( ) * videosToShuffle . length ) ] ;
794+ numVideosProcessed ++ ;
789795
790796 console . log ( `The chosen video does not exist anymore, so it will be removed from the database. A new random video has been chosen: ${ randomVideo } ` ) ;
791797
@@ -818,8 +824,11 @@ async function chooseRandomVideosFromPlaylist(playlistInfo, channelId, shouldUpd
818824 if ( playlistInfo [ "videos" ] [ "unknownType" ] [ randomVideo ] !== undefined ) {
819825 const videoIsShort = await isShort ( randomVideo ) ;
820826
821- // What follows is dependent on if the video is a short or not, and the user's settings
827+ // We display either the percentage of videos processed or the percentage of videos chosen (vs. needed), whichever is higher
828+ const percentage = Math . max ( Math . round ( chosenVideos . length / numVideosToChoose * 100 ) , Math . round ( numVideosProcessed / initialTotalNumVideos * 100 ) ) ;
829+ updateProgressTextElement ( progressTextElement , `\xa0Sorting: ${ percentage } %` , `${ percentage } %` ) ;
822830
831+ // What follows is dependent on if the video is a short or not, and the user's settings
823832 // Case 1: !isShort && ignoreShorts => Success
824833 if ( ! videoIsShort && configSync . shuffleIgnoreShortsOption == "2" ) {
825834 // Move the video to the knownVideos subdictionary
0 commit comments