From f581d568d9b2263b685e626934d8afc73cf0d9aa Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 19 Dec 2023 20:02:15 -0500 Subject: [PATCH] Fix sponsor skip infinite loop bug (#231) --- CHANGELOG.md | 1 + .../SponsorBlock/SponsorBlockService.bs | 1 + .../components/VideoPlayer/SponsorBlock.bs | 21 ++++++++-- .../src/components/VideoPlayer/VideoPlayer.bs | 40 +++++++++++-------- 4 files changed, 42 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b91bd13..63dc1a44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - (Regression) Issue where auto-play keeps playing the same videos in a loop - Small search box styling bug for Safari +- A bug where the skipping of a sponsored section at the end of a video leads to an infinite loop ### Removed diff --git a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs index 6f90380d..f0d58b0f 100644 --- a/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs +++ b/playlet-lib/src/components/Services/SponsorBlock/SponsorBlockService.bs @@ -9,6 +9,7 @@ namespace SponsorBlock class SponsorBlockService function new() + ' TODO:P2 this is being read and parsed on every skipped segment. It should be cached. m.sponsorBlockConfig = ParseJson(ReadAsciiFile("libpkg:/config/sponsorblock_config.json5")) end function diff --git a/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs b/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs index 637d3e7e..2d075493 100644 --- a/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs +++ b/playlet-lib/src/components/VideoPlayer/SponsorBlock.bs @@ -135,10 +135,23 @@ namespace SponsorBlock end if if action = "AutoSkip" - m.top.seek = segmentEnd - LogInfo("Skipping section:", segment["category"]) - SendSkippedSectionEvent(segment.UUID) - return + duration = m.top.duration + ' Sometimes segmentEnd is greater than duration + if duration > 0 and segmentEnd > duration + segmentEnd = duration + end if + ' If the segment is at the end of the video, skip the video instead of seeking + if duration > 0 and Abs(segmentEnd - duration) < 2 + LogInfo("Skipping section:", segment["category"], "at end of video") + SendSkippedSectionEvent(segment.UUID) + OnVideoFinished() + return + else + LogInfo("Skipping section:", segment["category"]) + SendSkippedSectionEvent(segment.UUID) + m.top.seek = segmentEnd + return + end if end if end if end for diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs index 2db5ca42..fc49987b 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs @@ -185,23 +185,8 @@ function OnVideoPlayerStateChange() as void end if if state = "finished" - if content = invalid - m.playQueue@.watchedVideosClear() - VideoUtils.CloseVideo() - return - end if - - metadata = content.metadata - if metadata = invalid - m.playQueue@.watchedVideosClear() - VideoUtils.CloseVideo() - return - end if - - if not PlayNext(metadata.recommendedVideos) - m.playQueue@.watchedVideosClear() - VideoUtils.CloseVideo() - end if + OnVideoFinished() + return end if if state = "error" @@ -225,6 +210,27 @@ function OnVideoPlayerStateChange() as void end if end function +function OnVideoFinished() as void + content = m.top.content + if content = invalid + m.playQueue@.watchedVideosClear() + VideoUtils.CloseVideo() + return + end if + + metadata = content.metadata + if metadata = invalid + m.playQueue@.watchedVideosClear() + VideoUtils.CloseVideo() + return + end if + + if not PlayNext(metadata.recommendedVideos) + m.playQueue@.watchedVideosClear() + VideoUtils.CloseVideo() + end if +end function + function PlayNext(recommendedVideos as object) as boolean if m.playQueue@.PlayNextItem() return true