@@ -84,8 +84,7 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
8484 } ;
8585 type PageElements = { [ key : string ] : HTMLElement } & InputPageElements
8686
87- /** If true, the content script is in the process of creating a new segment. */
88- let creatingSegment = false ;
87+ let stopLoadingAnimation = null ;
8988
9089 //the start and end time pairs (2d)
9190 let sponsorTimes : SponsorTime [ ] = [ ] ;
@@ -393,7 +392,6 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
393392 messageHandler . sendMessage ( tabs [ 0 ] . id , { message : 'getVideoID' } , function ( result ) {
394393 if ( result !== undefined && result . videoID ) {
395394 currentVideoID = result . videoID ;
396- creatingSegment = result . creatingSegment ;
397395
398396 loadTabData ( tabs , updating ) ;
399397 } else if ( result === undefined && chrome . runtime . lastError ) {
@@ -429,6 +427,12 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
429427 }
430428
431429 async function infoFound ( request : IsInfoFoundMessageResponse ) {
430+ // End any loading animation
431+ if ( stopLoadingAnimation != null ) {
432+ stopLoadingAnimation ( ) ;
433+ stopLoadingAnimation = null ;
434+ }
435+
432436 if ( chrome . runtime . lastError ) {
433437 //This page doesn't have the injected content script, or at least not yet
434438 displayNoVideo ( ) ;
@@ -490,10 +494,8 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
490494 }
491495
492496 function startSponsorCallback ( response : SponsorStartResponse ) {
493- creatingSegment = response . creatingSegment ;
494-
495497 // Only update the segments after a segment was created
496- if ( ! creatingSegment ) {
498+ if ( ! response . creatingSegment ) {
497499 sponsorTimes = Config . config . unsubmittedSegments [ currentVideoID ] || [ ] ;
498500 }
499501
@@ -728,9 +730,16 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
728730 PageElements . showNoticeAgain . style . display = "none" ;
729731 }
730732
733+ function isCreatingSegment ( ) : boolean {
734+ const segments = Config . config . unsubmittedSegments [ currentVideoID ] ;
735+ if ( ! segments ) return false ;
736+ const lastSegment = segments [ segments . length - 1 ] ;
737+ return lastSegment && lastSegment ?. segment ?. length !== 2 ;
738+ }
739+
731740 /** Updates any UI related to segment editing and submission according to the current state. */
732741 function updateSegmentEditingUI ( ) {
733- PageElements . sponsorStart . innerText = chrome . i18n . getMessage ( creatingSegment ? "sponsorEnd" : "sponsorStart" ) ;
742+ PageElements . sponsorStart . innerText = chrome . i18n . getMessage ( isCreatingSegment ( ) ? "sponsorEnd" : "sponsorStart" ) ;
734743
735744 PageElements . submitTimes . style . display = sponsorTimes && sponsorTimes . length > 0 ? "unset" : "none" ;
736745 PageElements . submissionHint . style . display = sponsorTimes && sponsorTimes . length > 0 ? "unset" : "none" ;
@@ -929,11 +938,13 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
929938 } ) ;
930939 }
931940
932- async function refreshSegments ( ) {
933- const stopAnimation = AnimationUtils . applyLoadingAnimation ( PageElements . refreshSegmentsButton , 0.3 ) ;
941+ function startLoadingAnimation ( ) {
942+ stopLoadingAnimation = AnimationUtils . applyLoadingAnimation ( PageElements . refreshSegmentsButton , 0.3 ) ;
943+ }
934944
935- infoFound ( await sendTabMessageAsync ( { message : 'refreshSegments' } ) as IsInfoFoundMessageResponse )
936- stopAnimation ( ) ;
945+ function refreshSegments ( ) {
946+ startLoadingAnimation ( ) ;
947+ sendTabMessage ( { message : 'refreshSegments' } ) ;
937948 }
938949
939950 function skipSegment ( actionType : ActionType , UUID : SegmentUUID , element ?: HTMLElement ) : void {
@@ -1058,6 +1069,24 @@ async function runThePopup(messageListener?: MessageListener): Promise<void> {
10581069 case "infoUpdated" :
10591070 infoFound ( msg ) ;
10601071 break ;
1072+ case "videoChanged" :
1073+ currentVideoID = msg . videoID
1074+ sponsorTimes = Config . config . unsubmittedSegments [ currentVideoID ] ?? [ ] ;
1075+ updateSegmentEditingUI ( ) ;
1076+
1077+ if ( msg . whitelisted ) {
1078+ PageElements . whitelistChannel . style . display = "none" ;
1079+ PageElements . unwhitelistChannel . style . display = "unset" ;
1080+ PageElements . whitelistToggle . checked = true ;
1081+ document . querySelectorAll ( '.SBWhitelistIcon' ) [ 0 ] . classList . add ( "rotated" ) ;
1082+ }
1083+
1084+ // Clear segments list & start loading animation
1085+ // We'll get a ping once they're loaded
1086+ startLoadingAnimation ( ) ;
1087+ PageElements . videoFound . innerHTML = chrome . i18n . getMessage ( "Loading" ) ;
1088+ displayDownloadedSponsorTimes ( [ ] , 0 ) ;
1089+ break ;
10611090 }
10621091 }
10631092}
0 commit comments