@@ -367,7 +367,20 @@ extension.features.watchLaterButtons = function (event) {
367367 return button ;
368368 }
369369
370- return thumbnail . querySelector ( 'ytd-thumbnail-overlay-toggle-button-renderer button' ) ;
370+ var fallbackButton = thumbnail . querySelector ( 'ytd-thumbnail-overlay-toggle-button-renderer button' ) ;
371+
372+ if ( fallbackButton ) {
373+ var label = ( fallbackButton . getAttribute ( 'aria-label' ) || '' ) . toLowerCase ( ) ,
374+ title = ( fallbackButton . getAttribute ( 'title' ) || '' ) . toLowerCase ( ) ;
375+
376+ if (
377+ label . indexOf ( 'watch later' ) !== - 1 ||
378+ title . indexOf ( 'watch later' ) !== - 1 ||
379+ ( label . indexOf ( 'queue' ) === - 1 && title . indexOf ( 'queue' ) === - 1 )
380+ ) {
381+ return fallbackButton ;
382+ }
383+ }
371384 }
372385
373386 function getYtConfigValue ( key ) {
@@ -415,6 +428,7 @@ extension.features.watchLaterButtons = function (event) {
415428 }
416429
417430 if ( ! apiKey || ! context ) {
431+ console . warn ( '[ImprovedTube] Unable to resolve Innertube API key/context for Watch Later button' ) ;
418432 button . dataset . state = 'unavailable' ;
419433 return ;
420434 }
@@ -436,6 +450,9 @@ extension.features.watchLaterButtons = function (event) {
436450 } ]
437451 } )
438452 } ) . then ( function ( response ) {
453+ if ( ! response . ok ) {
454+ console . warn ( '[ImprovedTube] Innertube Watch Later request failed with status:' , response . status ) ;
455+ }
439456 button . dataset . state = response . ok ? 'added' : 'unavailable' ;
440457 } ) . catch ( function ( ) {
441458 button . dataset . state = 'unavailable' ;
@@ -476,8 +493,26 @@ extension.features.watchLaterButtons = function (event) {
476493 clickEvent . stopImmediatePropagation ( ) ;
477494
478495 if ( nativeButton && nativeButton !== this ) {
496+ var initialAriaPressed = nativeButton . getAttribute ( 'aria-pressed' ) ,
497+ initialAriaLabel = nativeButton . getAttribute ( 'aria-label' ) ,
498+ buttonRef = this ,
499+ attempts = 0 ;
500+
479501 nativeButton . click ( ) ;
480- this . dataset . state = 'added' ;
502+
503+ ( function checkToggle ( ) {
504+ var currentAriaPressed = nativeButton . getAttribute ( 'aria-pressed' ) ,
505+ currentAriaLabel = nativeButton . getAttribute ( 'aria-label' ) ;
506+
507+ if ( currentAriaPressed !== initialAriaPressed || currentAriaLabel !== initialAriaLabel ) {
508+ buttonRef . dataset . state = 'added' ;
509+ } else if ( attempts < 10 ) {
510+ attempts ++ ;
511+ setTimeout ( checkToggle , 100 ) ;
512+ } else {
513+ addWithInnertube ( id , buttonRef ) ;
514+ }
515+ } ) ( ) ;
481516 } else {
482517 addWithInnertube ( id , this ) ;
483518 }
0 commit comments