import "pkg:/components/Services/SponsorBlock/Notifications/SponsorBlockNotificationUtils.bs" import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs" import "pkg:/source/AsyncTask/AsyncTask.bs" import "pkg:/source/AsyncTask/Tasks.bs" namespace SponsorBlock function SetupSponsorBlock() as void if not m.preferences["sponsorblock.enabled"] return end if m.sponsorBlockService = new SponsorBlock.SponsorBlockService() PositionChapterLabel() m.top.trickPlayBar.observeField("visible", FuncName(OnTrickPlayBarVisible)) m.chapterLabelTimer = m.top.findNode("chapterLabelTimer") m.chapterLabelTimer.observeField("fire", FuncName(OnChapterLabelTimer)) m._sponsorBlockLastTime = 0.0# ' TODO:P1 more precision on when notifications should be visible ' Should notifcations be visible when the video is not fullscreen? SponsorBlock.SetVisible(m.notifications, m.container.fullscreen) end function function PositionChapterLabel() m.chapterLabel = m.top.findNode("chapterLabel") ' We move the chapter label under m.top.trickPlayBar ' We could move it to m.top.pivotNode, but that is only available on Roku OS 11.5+ m.chapterLabel.reparent(m.top.trickPlayBar, false) trickPlayBarWidth = m.top.trickPlayBar.boundingRect().width m.chapterLabel.translation = [trickPlayBarWidth / 4 - m.chapterLabel.width / 2, 25] end function function OnTrickPlayBarVisible() if m.top.trickPlayBar.visible m.top.chapter = "" m.chapterLabelTimer.control = "start" else m.chapterLabelTimer.control = "stop" end if end function function OnChapterLabelTimer() as void if m.skipSegments = invalid return end if ' trickPlayPosition is not documented on Roku website, this is why we're checking if not IsDouble(m.top.trickPlayPosition) return end if if m._sponsorBlockLastTime = m.top.trickPlayPosition return end if m._sponsorBlockLastTime = m.top.trickPlayPosition UpdateSponsorBlockChapter(m._sponsorBlockLastTime) end function function UpdateSponsorBlockChapter(time as double) as void segments = m.skipSegments for each segment in segments category = segment["category"] segmentRange = segment["segment"] segmentStart = segmentRange[0] segmentEnd = segmentRange[1] ' Usually segmentStart and segmentEnd are equal for poi_highlight ' We pad the duration for poi_highlight segments so that the chapter label ' can be visible when seeking in trickplay if category = "poi_highlight" segmentPaddedDuration = 5 duration = m.top.duration if duration > 0 if duration < 120 segmentPaddedDuration = 1 else if duration < 300 segmentPaddedDuration = 2 else if duration < 900 segmentPaddedDuration = 5 else segmentPaddedDuration = 10 end if end if segmentDuration = Abs(segmentEnd - segmentStart) padding = Abs(segmentPaddedDuration - segmentDuration) / 2 segmentStart = segmentStart - padding segmentEnd = segmentEnd + padding end if if (segmentStart <= time) and (segmentEnd >= time) m.top.chapter = m.sponsorBlockService.SegmentTitle(category) return end if end for m.top.chapter = "" end function function FetchSponsorBlock(metadata as object) as void if not m.preferences["sponsorblock.enabled"] return end if if m.sponsorBlockTask <> invalid m.sponsorBlockTask.cancel = true end if m.sponsorBlockTask = AsyncTask.Start(Tasks.SponsorBlockTask, { metadata: metadata }, OnSponsorBlockTaskResults) end function function OnSponsorBlockTaskResults(output as object) as void m.sponsorBlockTask = invalid if output.cancelled return end if if not output.success error = ErrorUtils.Format(output.error) LogError(error) return end if skipSegments = output.result.skipSegments if skipSegments = invalid return end if m.skipSegments = skipSegments m.top.seekMode = "accurate" barPath = output.result.barPath if barPath <> invalid m.top.trickPlayBar.trackImageUri = barPath m.top.trickPlayBar.filledBarBlendColor = "#FF000080" end if m.top.UnobserveFieldScoped("position") m.top.ObserveFieldScoped("position", FuncName(OnPositionChangeSkipSponsorBlockSections)) end function function OnPositionChangeSkipSponsorBlockSections() as void segments = m.skipSegments if segments = invalid return end if for each segment in segments segmentRange = segment["segment"] segmentStart = segmentRange[0] segmentEnd = segmentRange[1] currentPosition = m.top.position if (segmentStart < currentPosition) and ((segmentEnd - 1) > currentPosition) action = GetActionForSegment(segment) if m.preferences["sponsorblock.show_notifications"] = true SponsorBlock.ShowNotifcation(m.notifications, segment, action) end if if action = "AutoSkip" 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 end function function GetActionForSegment(segment as object) as dynamic category = segment["category"] if category = "sponsor" return "AutoSkip" else if category = "poi_highlight" return "ManualSkip" else if category = "exclusive_access" return "ShowOverlay" else if category = "chapter" return "ShowOverlay" end if return "" end function function SendSkippedSectionEvent(segmentId as string) AsyncTask.Start(Tasks.SponsorBlockSegmentViewedTask, { segmentId: segmentId }) end function end namespace