diff --git a/out/PiPer.safariextz b/out/PiPer.safariextz index f39598e..16cb8e5 100644 Binary files a/out/PiPer.safariextz and b/out/PiPer.safariextz differ diff --git a/src/scripts/main.js b/src/scripts/main.js index e76e6a5..1b2ca1f 100644 --- a/src/scripts/main.js +++ b/src/scripts/main.js @@ -124,15 +124,32 @@ const prepareCaptions = function(video) { video.addEventListener('webkitendfullscreen', toggleCaptions); }; +/** + * Removes visible Picture in Picture mode captions + * @param {HTMLVideoElement} video - video element showing captions + */ +const removeCaptions = function(video) { + track.mode = 'showing'; + while (track.activeCues.length) track.removeCue(track.activeCues[0]); + + // Workaround Safari bug; 'removeCue' doesn't immediately remove captions shown in Picture in Picture mode + track.addCue(new VTTCue(video.currentTime, video.currentTime, '')); +} + /** * Updates visible captions */ const processCaptions = function() { - const captionElement = currentResource.captionElement(); - // Hide Picture in Picture mode captions and show native captions if no longer showing captions or encountered an error + // Get handles to caption and video elements + const captionElement = currentResource.captionElement(); + const video = /** @type {?HTMLVideoElement} */ (currentResource.videoElement()); + + // Remove old captions + removeCaptions(video); + + // Show native captions if no longer showing captions or encountered an error if (!showingCaptions || !captionElement) { - track.mode = 'disabled'; if (captionElement) captionElement.style.visibility = ''; return; } @@ -144,16 +161,9 @@ const processCaptions = function() { const unprocessedCaption = captionElement.textContent; if (unprocessedCaption == lastUnprocessedCaption) return; lastUnprocessedCaption = unprocessedCaption; - - // Get handle to video (called before accessing 'track' to guarentee valid) - const video = /** @type {?HTMLVideoElement} */ (currentResource.videoElement()); - - // Remove old captions - track.mode = 'showing'; - while (track.activeCues.length) track.removeCue(track.activeCues[0]); - - // Line commented out to workaround Safari bug; 'removeCue' doesn't immediately remove captions shown in Picture in Picture mode - //if (!unprocessedCaption) return; + + // Performance optimisation - early exit if caption has no content + if (!unprocessedCaption) return; // Show correctly spaced and formatted Picture in Picture mode caption let caption = ''; diff --git a/update.plist b/update.plist index fcd6181..195a9ba 100644 --- a/update.plist +++ b/update.plist @@ -10,7 +10,7 @@ CFBundleShortVersionString 0.2.1 CFBundleVersion - 60 + 61 Developer Identifier BQ6Q24MF9X URL