SponsorBlock categories (#634)

* SponsorBlock categories

* perf

* remove todo

* tweak

* UI is working

* Lint fix

* changelog

* Bug fix

* Restore timer

* Locale

* Comment

* tweaks

* web app

* remove unsued svelte ignore

* lint web app

---------

Co-authored-by: github-action linter <githubaction@githubaction.com>
This commit is contained in:
Brahim Hadricheandgithub-action linter authored and GitHub committed 2025-06-01 15:30:58 -04:00
1 parent 0bc5ab6117
commit fcae05a0da
34 files changed
+1039 -422

No files matched your search

@@ -2,6 +2,7 @@ import "pkg:/components/Services/SponsorBlock/Notifications/SponsorBlockNotifica
import "pkg:/components/Services/SponsorBlock/SponsorBlockService.bs"
import "pkg:/source/AsyncTask/AsyncTask.bs"
import "pkg:/source/AsyncTask/Tasks.bs"
import "SponsorBlockSegmentAction.bs"
import "VideoPlayer.bs"
namespace SponsorBlock
@@ -12,18 +13,62 @@ namespace SponsorBlock
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 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
showNotifications: m.preferences["sponsorblock.show_notifications"]
categories: m.preferences["sponsorblock.categories"]
}, OnSponsorBlockTaskResults)
end function
function OnSponsorBlockTaskResults(output as object) as void
m.sponsorBlockTask = invalid
if output.cancelled or m.onCloseCalled = true
return
end if
if not output.success
error = ErrorUtils.Format(output.error)
LogError(error)
return
end if
skipSegments = output.result.skipSegments
if skipSegments = invalid or skipSegments.Count() = 0
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.ObserveFieldScoped("position", FuncName(OnPositionChangeSkipSponsorBlockSections))
if m.top.hasField("trickPlayPosition")
PositionChapterLabel()
m.chapterLabelTimer = m.top.findNode("chapterLabelTimer")
m.chapterLabelTimer.observeField("fire", FuncName(OnChapterLabelTimer))
m._sponsorBlockLastTimeChapterUpdated = 0.0#
m.top.trickPlayBar.observeField("visible", FuncName(OnTrickPlayBarVisible))
end if
end function
function PositionChapterLabel()
m.chapterLabel = m.top.findNode("chapterLabel")
' We move the chapter label under m.top.trickPlayBar
@@ -43,162 +88,137 @@ namespace SponsorBlock
end function
function OnChapterLabelTimer() as void
if m.skipSegments = invalid
trickPlayPosition = m.top.trickPlayPosition
if m._sponsorBlockLastTimeChapterUpdated = trickPlayPosition
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)
m._sponsorBlockLastTimeChapterUpdated = trickPlayPosition
UpdateSponsorBlockChapter(trickPlayPosition)
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 = Tr(ValidString(m.sponsorBlockService.SegmentTitle(category)))
return
end if
end for
m.top.chapter = ""
segment = FindSegmentByTime(segments, time, 0)
if segment = invalid
m.top.chapter = ""
return
end if
m.top.chapter = segment.title
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 or m.onCloseCalled = true
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.ObserveFieldScoped("position", FuncName(OnPositionChangeSkipSponsorBlockSections))
function FindSegmentByTime(segments as object, time as dynamic, endOffset as integer) as dynamic
' Binary search for the segment that contains the time.
' This search sometimes fails if the segments overlap.
' For most cases, this is acceptable, since it is not common for segments to overlap.
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 + endOffset)
low = middle + 1
else
return segment
end if
end while
return invalid
end function
function OnPositionChangeSkipSponsorBlockSections() as void
segments = m.skipSegments
if segments = invalid
position = m.top.position
' We add a small offset to the end position of the segment.
' This is so that when we skip a segment, we don't get
' another notification to skip it.
' Seeking accuracy is not perfect, so we need to account for that.
segment = FindSegmentByTime(segments, position, -1)
if segment = 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
action = segment.option
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
if segment.showNotification
SponsorBlock.ShowNotifcation(m.notifications, segment, action)
if action = SegmentAction.ManualSkip
' We'll save the segment here, and handle it in case the user
' presses the OK button on the notification.
m.manualSkipSegment = segment
return
end if
end for
end if
if action = SegmentAction.AutoSkip
SkipSegment(segment, position)
end if
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"
function SkipSegment(segment as object, position as dynamic) as void
segmentEnd = segment.end
duration = m.top.duration
' Sometimes segmentEnd is greater than duration
if duration > 0 and segmentEnd > duration
segmentEnd = duration
end if
return ""
' If we're skipping a very short distance, it's not worth it
' since we can cause rebuffering
if Abs(segmentEnd - position) < 2
LogInfo("Skip position is within 2 seconds of segment end, not skipping")
return
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")
OnVideoFinished()
else
LogInfo("Skipping section:", segment.category)
m.top.seek = segmentEnd
end if
SendSkippedSectionEvent(segment.UUID)
end function
function ManualSkipSegment() as boolean
segment = m.manualSkipSegment
if segment = invalid
return false
end if
m.manualSkipSegment = invalid
notification = SponsorBlock.GetCurrentNotification(m.notifications)
if notification = invalid
return false
end if
notificationSegmentId = notification.segmentId
if notificationSegmentId <> segment.UUID
return false
end if
position = m.top.position
if (position < segment.start) or (segment.end < position)
return false
end if
SkipSegment(segment, position)
SponsorBlock.RemoveNotifcation(m.notifications)
return true
end function
function SendSkippedSectionEvent(segmentId as string)
AsyncTask.Start(Tasks.SponsorBlockSegmentViewedTask, { segmentId: segmentId })
if m.sponsorSkipEventTask <> invalid
m.sponsorSkipEventTask.cancel = true
end if
m.sponsorSkipEventTask = AsyncTask.Start(Tasks.SponsorBlockSegmentViewedTask, { segmentId: segmentId })
end function
end namespace
@@ -0,0 +1,16 @@
namespace SponsorBlock
enum SegmentAction
' Disable: we do not care about this segment.
Disable = "disable"
' AutoSkip: we want to skip this segment automatically.
AutoSkip = "auto_skip"
' ManualSkip: we want to skip this segment manually, by showing a notification.
' The user would have to press the skip button on the notification.
ManualSkip = "manual_skip"
' ShowInSeekbar: we want to show this segment in the seekbar, but not skip it.
' No notification will be shown
ShowInSeekbar = "show_in_seekbar"
end enum
end namespace
@@ -518,6 +518,10 @@ function HideUIAndStopTimer()
end function
function HandleOKPress() as boolean
if SponsorBlock.ManualSkipSegment()
return false
end if
if not m.showUIEnabled
return false
end if
@@ -19,7 +19,7 @@
</interface>
<children>
<Label id="chapterLabel" width="350" height="25" horizAlign="center" vertAlign="center" font="font:SmallestSystemFont" />
<Timer id="chapterLabelTimer" repeat="true" duration="0.25" />
<Timer id="chapterLabelTimer" repeat="true" duration="0.5" />
<Animation id="minimizeAnimation" duration="0.3" optional="true">
<FloatFieldInterpolator id="widthInterpolator" key="[0.0, 0.5, 1.0]" fieldToInterp="VideoPlayer.width" />
<FloatFieldInterpolator id="heightInterpolator" key="[0.0, 0.5, 1.0]" fieldToInterp="VideoPlayer.height" />