SponsorBlock Fix

This commit is contained in:
Brahim Hadriche committed 2026-10-09 14:29:52 -04:00
1 parent 810ef60770
commit c581a4767a
14 files changed
+763 -208

No files matched your search

@@ -1,6 +1,7 @@
import "pkg:/components/JobSystem/Jobs.bs"
import "pkg:/components/JobSystem/JobSystem.bs"
import "pkg:/components/Services/SponsorBlock/Notifications/SponsorBlockNotificationUtils.bs"
import "pkg:/components/Services/SponsorBlock/SponsorBlockTimeline.bs"
import "pkg:/components/VideoPlayer/SponsorBlockSegmentAction.bs"
import "pkg:/source/utils/Logging.bs"
import "pkg:/source/utils/Types.bs"
@@ -15,9 +16,15 @@ namespace SponsorBlockController
' Bar fill tint (semi-transparent black over the red fill) pushed onto the trickplay bar when segments arrive.
const BAR_FILL_BLEND = &hFF000080
' TrySkip returns this for "the segment is at the video end -> finish" (vs a seek ms).
' OnPosition/TrySkip return a seek target (ms), or one of these.
const NO_SKIP = -1
' the segment runs to the video end: finish playback instead of seeking.
const FINISH_SENTINEL = -2
' Video.seek and Video.position are single-precision: on a long video the tick echoing a seek target can read
' a few ms past it.
const SEEK_ECHO_SLACK_MS = 100
class Controller
' deps the coordinator owns; top = the Video node (seekMode + duration).
private jobQueue as object = invalid
@@ -27,11 +34,19 @@ namespace SponsorBlockController
' segment state — stays here, never copied onto a field.
private segments as object = invalid
private timeline as SponsorBlockTimeline = invalid
private enabled as boolean = false
private manualSkipSegment as object = invalid
' a manual notice the user dismissed with Back: don't re-arm/re-show it while still inside this segment.
private dismissedSegmentId as string = ""
private sponsorSkipEventJob as object = invalid
' the manual segment whose prompt is armed for the next OK (index into segments, -1 = none).
private manualSkipIndex as integer = -1
' the manual segment dismissed with Back (index into segments, -1 = none): its prompt stays down until the
' position leaves the segment's range.
private dismissedIndex as integer = -1
' after a skip, no segment acts until the position passes this ms or the user seeks (-1 = none). Without
' accurate seek the player lands on the sync frame BEFORE the target — inside the skipped segment,
' before a short one's start, or in an earlier one — and re-arming there loops the skip.
private quietUntilMs as integer = -1
' UUIDs already reported to the service: one report per segment per viewing.
private reportedSegmentIds as object = {}
function new(deps as object)
m.jobQueue = deps.jobQueue
@@ -73,92 +88,129 @@ namespace SponsorBlockController
return
end if
m.segments = segments
m.timeline = info.data.timeline
if m.top <> invalid
m.top.seekMode = "accurate"
end if
LogDebug("segments ready:", segments.Count())
end function
' The real position (ms) while idle. Returns { skipSeekMs } (auto-skip), { finish: true } (segment at
' the video end), or invalid. A manual segment only shows the notification + stashes for the next OK
' (TrySkip). Reports the skip to the service on an actual auto-skip.
function OnPosition(positionMs as integer) as object
' The real position (ms) while idle. Returns the auto-skip target (ms), FINISH_SENTINEL (segment at the
' video end), or NO_SKIP. A manual segment only shows the notification + arms it for the next OK
' (TrySkip), and withdraws it once OK can no longer skip. Reports the skip to the service on an actual
' auto-skip.
function OnPosition(positionMs as integer) as integer
if not m.enabled or m.segments = invalid
return invalid
return SponsorBlockController.NO_SKIP
end if
if m.quietUntilMs >= 0
' inclusive: the first tick after a seek echoes the target before reporting the actual landing.
if positionMs <= m.quietUntilMs + SponsorBlockController.SEEK_ECHO_SLACK_MS
return SponsorBlockController.NO_SKIP
end if
m.quietUntilMs = -1
end if
positionS = positionMs / 1000.0
segment = SponsorBlockController.FindSegmentByTime(m.segments, positionS)
if segment = invalid
' left the segment: a Back-dismissal only suppresses the current pass — re-entering later re-arms.
m.dismissedSegmentId = ""
return invalid
if m.dismissedIndex <> -1
dismissed = m.segments[m.dismissedIndex]
if positionS < dismissed.start or positionS > dismissed.end
m.dismissedIndex = -1
end if
end if
if segment.showNotification and Abs(segment.end - positionS) >= 1 and segment.UUID <> m.dismissedSegmentId
index = SponsorBlock.FindTimelineSegmentIndex(m.timeline, positionS)
segment = invalid
targetMs = SponsorBlockController.NO_SKIP
if index <> -1 and index <> m.dismissedIndex
segment = m.segments[index]
if SponsorBlock.IsSkippable(segment)
targetMs = SponsorBlockController.SkipTargetMs(segment, positionS, m.Duration())
end if
end if
if m.manualSkipIndex <> -1 and (targetMs = SponsorBlockController.NO_SKIP or m.manualSkipIndex <> index)
m.WithdrawManual()
end if
if targetMs = SponsorBlockController.NO_SKIP
return targetMs
end if
if segment.showNotification
SponsorBlock.ShowNotifcation(m.notifications, segment)
if segment.option = SponsorBlock.SegmentAction.ManualSkip
m.manualSkipSegment = segment
return invalid
end if
end if
if segment.option = SponsorBlock.SegmentAction.AutoSkip
result = SponsorBlockController.SkipTargetForSegment(segment, positionS, m.Duration())
if result <> invalid
m.SendSegmentViewed(segment.UUID)
end if
return result
if segment.option = SponsorBlock.SegmentAction.ManualSkip
m.manualSkipIndex = index
return SponsorBlockController.NO_SKIP
end if
return invalid
m.OnSkipped(segment, targetMs)
return targetMs
end function
' The OK routing calls this first. Returns the seek target (ms) for the stashed manual segment if the
' position is still inside it (FINISH_SENTINEL at the video end), else -1. Reports the skip on success.
' The coordinator applied a seek. Any seek but our own skip wakes the segments a skip quieted.
function OnSeek(seekMs as integer) as void
if seekMs <> m.quietUntilMs
m.quietUntilMs = -1
end if
end function
' The OK routing calls this first. Returns the seek target (ms) for the armed manual segment if the
' position is still inside it (FINISH_SENTINEL at the video end), else NO_SKIP. Reports the skip on success.
function TrySkip(positionMs as integer) as integer
segment = m.manualSkipSegment
if segment = invalid
return -1
end if
m.manualSkipSegment = invalid
notification = SponsorBlock.GetCurrentNotification(m.notifications)
if notification = invalid or notification.segmentId <> segment.UUID
return -1
index = m.manualSkipIndex
m.manualSkipIndex = -1
if not m.IsManualPromptShowing(index)
return SponsorBlockController.NO_SKIP
end if
segment = m.segments[index]
positionS = positionMs / 1000.0
if positionS < segment.start or segment.end < positionS
return -1
return SponsorBlockController.NO_SKIP
end if
result = SponsorBlockController.SkipTargetForSegment(segment, positionS, m.Duration())
targetMs = SponsorBlockController.SkipTargetMs(segment, positionS, m.Duration())
SponsorBlock.RemoveNotifcation(m.notifications)
if result = invalid
return -1
if targetMs <> SponsorBlockController.NO_SKIP
m.OnSkipped(segment, targetMs)
end if
m.SendSegmentViewed(segment.UUID)
if result.finish = true
return SponsorBlockController.FINISH_SENTINEL
end if
return result.skipSeekMs
return targetMs
end function
' Back while the manual-skip notice is up: dismiss the notice without skipping (and without it
' re-arming until the position leaves the segment). Returns true if a notice was dismissed — the
' caller then swallows the Back instead of acting on the player.
' re-arming until the position leaves the segment). Returns true if a notice was dismissed — the caller
' then swallows the Back instead of acting on the player.
function DismissManual() as boolean
segment = m.manualSkipSegment
if segment = invalid
index = m.manualSkipIndex
if not m.IsManualPromptShowing(index)
return false
end if
m.manualSkipIndex = -1
m.dismissedIndex = index
SponsorBlock.RemoveNotifcation(m.notifications)
return true
end function
function OnSkipped(segment as object, targetMs as integer) as void
if targetMs <> SponsorBlockController.FINISH_SENTINEL
m.quietUntilMs = targetMs
end if
m.SendSegmentViewed(segment.UUID)
end function
function WithdrawManual() as void
if m.IsManualPromptShowing(m.manualSkipIndex)
SponsorBlock.RemoveNotifcation(m.notifications)
end if
m.manualSkipIndex = -1
end function
function IsManualPromptShowing(index as integer) as boolean
if index = -1
return false
end if
notification = SponsorBlock.GetCurrentNotification(m.notifications)
if notification = invalid or notification.segmentId <> segment.UUID
return false
end if
m.manualSkipSegment = invalid
m.dismissedSegmentId = ValidString(segment.UUID)
SponsorBlock.RemoveNotifcation(m.notifications)
return true
return notification <> invalid and notification.segmentId = m.segments[index].UUID
end function
' The chapter label text for a cursor (ms): the segment title inside a segment, else "".
@@ -166,11 +218,11 @@ namespace SponsorBlockController
if not m.enabled or m.segments = invalid
return ""
end if
segment = SponsorBlockController.FindSegmentByTime(m.segments, cursorMs / 1000.0)
if segment = invalid
index = SponsorBlock.FindTimelineSegmentIndex(m.timeline, cursorMs / 1000.0)
if index = -1
return ""
end if
return ValidString(segment.title)
return ValidString(m.segments[index].title)
end function
function HasSegments() as boolean
@@ -184,38 +236,24 @@ namespace SponsorBlockController
end if
end function
' Reset on close / new content.
function Clear() as void
' Take the notice down and drop every held node ref on close so the controller — and the player node it
' points at — can free.
function Dispose() as void
if m.notifications <> invalid
SponsorBlock.RemoveNotifcation(m.notifications)
end if
m.segments = invalid
m.manualSkipSegment = invalid
m.dismissedSegmentId = ""
m.enabled = false
end function
' Drop every held node ref on close so the controller — and the player node it points at — can free.
function Dispose() as void
m.Clear()
' a skip can fire just before close and must still report — release the ref, don't cancel the in-flight job.
m.sponsorSkipEventJob = invalid
m.top = invalid
m.jobQueue = invalid
m.preferences = invalid
m.notifications = invalid
end function
' Report a skipped segment to the SponsorBlock service (fire-and-forget).
function SendSegmentViewed(segmentId as string) as void
if m.jobQueue = invalid
if m.jobQueue = invalid or m.reportedSegmentIds.DoesExist(segmentId)
return
end if
if m.sponsorSkipEventJob <> invalid
m.sponsorSkipEventJob.cancel = true
end if
m.sponsorSkipEventJob = JobSystem.CreateCallback()
JobSystem.QueueJob(m.jobQueue, Jobs.SponsorBlockSegmentViewedJob, { segmentId: segmentId }, m.sponsorSkipEventJob)
m.reportedSegmentIds[segmentId] = true
JobSystem.QueueJob(m.jobQueue, Jobs.SponsorBlockSegmentViewedJob, { segmentId: segmentId })
end function
function Duration() as float
@@ -227,40 +265,21 @@ namespace SponsorBlockController
end class
' Binary search for the segment containing `time` (seconds); segments are sorted by start. May miss
' overlapping segments (acceptable — they are rare).
function FindSegmentByTime(segments as object, time as dynamic) as dynamic
low = 0
high = segments.Count() - 1
while low <= high
middle = (low + high) \ 2
segment = segments[middle]
if time < segment.start
high = middle - 1
else if time > segment.end
low = middle + 1
else
return segment
end if
end while
return invalid
end function
' The seek decision for a segment at `positionS` (seconds): { skipSeekMs } | { finish: true } (segment at
' the video end) | invalid (too short — a skip would just rebuffer). `duration` clamps a segment end past
' the video end.
function SkipTargetForSegment(segment as object, positionS as dynamic, duration as dynamic) as object
' The seek decision for a segment at `positionS` (seconds): the target (ms), FINISH_SENTINEL (segment at the
' video end), or NO_SKIP (too short — a skip would just rebuffer). `duration` clamps a segment end past the
' video end.
function SkipTargetMs(segment as object, positionS as dynamic, duration as dynamic) as integer
segmentEnd = segment.end
if duration > 0 and segmentEnd > duration
segmentEnd = duration
end if
if Abs(segmentEnd - positionS) < 2
return invalid
return SponsorBlockController.NO_SKIP
end if
if duration > 0 and Abs(segmentEnd - duration) < 2
return { finish: true }
return SponsorBlockController.FINISH_SENTINEL
end if
return { skipSeekMs: Int(segmentEnd * 1000) }
return Int(segmentEnd * 1000)
end function
end namespace