import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/components/VideoPlayer/SponsorBlockTask.bs" import "pkg:/components/VideoPlayer/SponsorBlockTrackingTask.bs" import "pkg:/components/SponsorBlockNotification/SponsorBlockNotificationManager.bs" import "pkg:/components/SettingsScreen/Settings.bs" function SetupSponsorBlock() as void LoadSponsorBlockSettings() if not m.sponsorBlockEnabled = true return end if PositionChapterLabel() SponsorBlockNotifcationSetVisible(m.videoContainer.fullscreen) m.timeLabel = FindTimeLabel() m.top.trickPlayBar.observeField("visible", "OnTrickPlayBarVisible") m.chapterLabelTimer = m.top.findNode("chapterLabelTimer") m.chapterLabelTimer.observeField("fire", "OnChapterLabelTimer") m.top.observeField("videoId", "StartSponsorBlockTask") end function function LoadSponsorBlockSettings() as void settings = FindAndGetPreferenceValues(["sponsorblock.enabled", "sponsorblock.show_notifications"]) if settings = invalid m.sponsorBlockEnabled = false m.sponsorBlockShowNotifications = false return end if m.sponsorBlockEnabled = settings["sponsorblock.enabled"] m.sponsorBlockShowNotifications = settings["sponsorblock.show_notifications"] end function function PositionChapterLabel() m.chapterLabel = m.top.findNode("chapterLabel") m.chapterLabel.reparent(m.top.trickPlayBar, false) trickPlayBarWidth = m.top.trickPlayBar.boundingRect().width #if DASH_THUMBNAILS yPos = 25 #else yPos = 55 #end if m.chapterLabel.translation = [trickPlayBarWidth / 2 - m.chapterLabel.width / 2, yPos] end function ' This is a hack to access the position of the trickplay function FindTimeLabel() as object children = m.top.trickPlayBar.getChildren(m.top.trickPlayBar.getChildCount(), 0) label = invalid for each child in children ' It's very unfortunate to have to find a label with 0:00 text, ' and that's on the left side of the screen. This might need completely custom trickPlayBar if child.isSubtype("Label") and child.text = "0:00" if label = invalid label = child else if child.translation[0] < label.translation[0] label = child end if end if end if end for return label 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.timeLabel = invalid or m.skipSegments = invalid return end if if m.sponsorBlockLastTime = m.timeLabel.text return end if m.sponsorBlockLastTime = m.timeLabel.text time = TimeUtils.ParseTime(m.timeLabel.text) UpdateSponsorBlockChapter(time) end function function UpdateSponsorBlockChapter(time as integer) as void segments = m.skipSegments for each segment in segments segmentRange = segment["segment"] segmentStart = segmentRange[0] segmentEnd = segmentRange[1] if (segmentStart <= time) and (segmentEnd >= time) m.top.chapter = SponsorBlock.SegmentTitle(segment["category"]) return end if end for m.top.chapter = "" end function function StartSponsorBlockTask() if m.sponsorBlockTask <> invalid m.sponsorBlockTask.control = "stop" end if m.sponsorBlockTask = StartAsyncTask(SponsorBlockTask, { videoId: m.top.videoId }, OnSponsorBlockTaskResults) end function function OnSponsorBlockTaskResults(output as object) skipSegments = output.result.skipSegments barPath = output.result.barPath if skipSegments <> invalid m.skipSegments = skipSegments m.top.seekMode = "accurate" if barPath <> invalid m.top.trickPlayBar.trackImageUri = barPath m.top.trickPlayBar.filledBarBlendColor = "0xFF000080" end if m.top.ObserveField("position", "OnPositionChangeSkipSponsorBlockSections") end if 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.sponsorBlockShowNotifications = true ShowSponsorBlockNotifcation(segment, action) end if if action = "AutoSkip" m.top.seek = segmentEnd print("Skipping section:" + segment["category"]) SendSkippedSectionEvent(segment.UUID) return 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) StartAsyncTask(SponsorBlockTrackingTask, { segmentId: segmentId }) end function