@@ -127,13 +127,14 @@ const prepareCaptions = function(video) {
127
127
/**
128
128
* Removes visible Picture in Picture mode captions
129
129
* @param {HTMLVideoElement } video - video element showing captions
130
+ * @param {boolean } workaround - apply Safari bug workaround
130
131
*/
131
- const removeCaptions = function ( video ) {
132
+ const removeCaptions = function ( video , workaround = true ) {
132
133
track . mode = 'showing' ;
133
134
while ( track . activeCues . length ) track . removeCue ( track . activeCues [ 0 ] ) ;
134
135
135
136
// Workaround Safari bug; 'removeCue' doesn't immediately remove captions shown in Picture in Picture mode
136
- track . addCue ( new VTTCue ( video . currentTime , video . currentTime , '' ) ) ;
137
+ if ( workaround ) track . addCue ( new VTTCue ( video . currentTime , video . currentTime + 60 , '' ) ) ;
137
138
}
138
139
139
140
/**
@@ -145,11 +146,9 @@ const processCaptions = function() {
145
146
const captionElement = currentResource . captionElement ( ) ;
146
147
const video = /** @type {?HTMLVideoElement } */ ( currentResource . videoElement ( ) ) ;
147
148
148
- // Remove old captions
149
- removeCaptions ( video ) ;
150
-
151
- // Show native captions if no longer showing captions or encountered an error
149
+ // Remove Picture in Picture mode captions and show native captions if no longer showing captions or encountered an error
152
150
if ( ! showingCaptions || ! captionElement ) {
151
+ removeCaptions ( video ) ;
153
152
if ( captionElement ) captionElement . style . visibility = '' ;
154
153
return ;
155
154
}
@@ -162,6 +161,9 @@ const processCaptions = function() {
162
161
if ( unprocessedCaption == lastUnprocessedCaption ) return ;
163
162
lastUnprocessedCaption = unprocessedCaption ;
164
163
164
+ // Remove old captions and apply Safari bug fix if caption has no content as otherwise causes flicker
165
+ removeCaptions ( video , ! unprocessedCaption ) ;
166
+
165
167
// Performance optimisation - early exit if caption has no content
166
168
if ( ! unprocessedCaption ) return ;
167
169
@@ -653,29 +655,26 @@ const resources = {
653
655
654
656
// Workaround Safari bug; old captions persist in Picture in Picture mode when MediaSource buffers change
655
657
const video = /** @type {?HTMLVideoElement } */ ( currentResource . videoElement ( ) ) ;
656
- document . addEventListener ( 'spfrequest ' , function ( ) {
658
+ window . addEventListener ( 'yt-navigate-start ' , function ( ) {
657
659
showingCaptions = false ;
658
660
removeCaptions ( video ) ;
659
661
} ) ;
660
- document . addEventListener ( 'spfdone ' , function ( ) {
662
+ window . addEventListener ( 'yt-navigate-finish ' , function ( ) {
661
663
showingCaptions = video . webkitPresentationMode == 'picture-in-picture' ;
662
664
} ) ;
663
665
} ,
664
666
buttonInsertBefore : function ( /** Element */ parent ) {
665
667
return parent . lastChild ;
666
668
} ,
667
669
buttonParent : function ( ) {
668
- const e = document . getElementById ( 'movie_player' ) || document . getElementById ( 'player' ) ;
669
- return e && e . querySelector ( '.ytp-right-controls' ) ;
670
+ return document . querySelector ( '.ytp-right-controls' ) ;
670
671
} ,
671
672
buttonScale : 0.68 ,
672
673
captionElement : function ( ) {
673
- const e = document . getElementById ( 'movie_player' ) || document . getElementById ( 'player' ) ;
674
- return e && e . querySelector ( '.captions-text' ) ;
674
+ return document . querySelector ( '.caption-window' ) ;
675
675
} ,
676
676
videoElement : function ( ) {
677
- const e = document . getElementById ( 'movie_player' ) || document . getElementById ( 'player' ) ;
678
- return e && e . querySelector ( 'video.html5-main-video' ) ;
677
+ return document . querySelector ( 'video.html5-main-video' ) ;
679
678
} ,
680
679
} ,
681
680
} ;
0 commit comments