From c7ed471d5495638f36ad709d214a4cec3fcb82de Mon Sep 17 00:00:00 2001 From: rlauuzo <46294892+rlauuzo@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:11:31 +0200 Subject: [PATCH] Start the next episode directly (#205) --- .../Configuration/inject.js | 36 ++++++++++++++----- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js b/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js index d35137d..9984e48 100644 --- a/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js +++ b/ConfusedPolarBear.Plugin.IntroSkipper/Configuration/inject.js @@ -237,7 +237,7 @@ introSkipper.throttle = function (func, limit) { }; } /** Seeks to the end of the intro. */ -introSkipper.doSkip = introSkipper.throttle(function (e) { +introSkipper.doSkip = introSkipper.throttle(async function () { introSkipper.d("Skipping intro"); introSkipper.d(introSkipper.skipSegments); const segment = introSkipper.getCurrentSegment(introSkipper.videoPlayer.currentTime); @@ -245,16 +245,34 @@ introSkipper.doSkip = introSkipper.throttle(function (e) { console.warn("[intro skipper] doSkip() called without an active segment"); return; } + const seekToSegmentEnd = () => new Promise((resolve) => { + const onSeeked = () => { + setTimeout(() => { + introSkipper.allowEnter = true; + resolve(); + }, 50); // Wait 50ms after seek completes + }; + introSkipper.videoPlayer.addEventListener('seeked', onSeeked, { once: true }); + introSkipper.videoPlayer.currentTime = segment.IntroEnd; + }); // Disable keydown events introSkipper.allowEnter = false; - introSkipper.videoPlayer.currentTime = segment.IntroEnd; - // Listen for the seeked event to re-enable keydown events - const onSeeked = async () => { - await new Promise(resolve => setTimeout(resolve, 50)); // Wait 50ms - introSkipper.allowEnter = true; - introSkipper.videoPlayer.removeEventListener('seeked', onSeeked); - }; - introSkipper.videoPlayer.addEventListener('seeked', onSeeked); + // Check if the segment is "Credits" and skipping would leave less than 3 seconds of video + if (segment.SegmentType === "Credits" && introSkipper.videoPlayer.duration - segment.IntroEnd < 3) { + // Simulate 'N' key press to go to the next episode + document.dispatchEvent(new KeyboardEvent('keydown', { key: 'N', shiftKey: true, bubbles: true })); + // Check if the next episode actually starts loading + const nextEpisodeLoaded = await Promise.race([ + new Promise(resolve => introSkipper.videoPlayer.addEventListener('loadstart', () => resolve(true), { once: true })), + new Promise(resolve => setTimeout(() => resolve(false), 100)) + ]); + // If the next episode didn't load, just seek to the end of the current segment + if (!nextEpisodeLoaded) await seekToSegmentEnd(); + else introSkipper.allowEnter = true; + return; + } + // Default behavior: seek to the end of the current segment + await seekToSegmentEnd(); }, 3000); /** Tests if an element with the provided selector exists. */ introSkipper.testElement = function (selector) { return document.querySelector(selector); }