From e0020ffa6670e868ae0d1ff4a8c6c10ac2039613 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Fri, 10 Feb 2023 15:20:08 -0500 Subject: [PATCH] SponsorBlock Notification (#47) * SponsorBlock Notification * Lint fix * Reuse animation for multiple skips * Small change * Rename color utils * Lint fix --------- Co-authored-by: github-action linter --- CHANGELOG.md | 3 + package-lock.json | 4 +- package.json | 3 +- .../SponsorBlockNotification.bs | 65 +++++++++ .../SponsorBlockNotification.xml | 47 +++++++ .../SponsorBlockNotificationManager.bs | 27 ++++ .../src/components/VideoPlayer/Video.bs | 3 + .../src/components/VideoPlayer/VideoPlayer.bs | 4 +- .../VideoPlayer/VideoPlayerSponsorBlock.bs | 5 + .../WebServer/Task/WebServerTask.bs | 11 +- .../WebServer/Task/WebServerTask.xml | 1 - .../src/config/sponsorblock_config.json | 115 ++++++++++++++++ .../images/LogoSponsorBlockSimple256px.png | Bin 0 -> 4440 bytes .../src/source/services/SponsorBlock.bs | 69 +++++----- playlet-lib/src/source/utils/ColorUtils.bs | 19 +++ playlet-lib/src/source/utils/Debug.bs | 17 --- tools/generate-sponsorblock-config.js | 129 ++++++++++++++++++ 17 files changed, 460 insertions(+), 62 deletions(-) create mode 100644 playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotification.bs create mode 100644 playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotification.xml create mode 100644 playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotificationManager.bs create mode 100644 playlet-lib/src/config/sponsorblock_config.json create mode 100644 playlet-lib/src/images/LogoSponsorBlockSimple256px.png create mode 100644 playlet-lib/src/source/utils/ColorUtils.bs delete mode 100644 playlet-lib/src/source/utils/Debug.bs create mode 100644 tools/generate-sponsorblock-config.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 89810c53..bbad93d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Added +- SponsorBlock notification + ### Fixed - Crash in case GetLocalIpAddress() returns invalid diff --git a/package-lock.json b/package-lock.json index 027bf003..05f8976a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "playlet", - "version": "0.6.0", + "version": "0.7.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "playlet", - "version": "0.6.0", + "version": "0.7.0", "hasInstallScript": true, "dependencies": { "bslib": "npm:@rokucommunity/bslib@^0.1.1", diff --git a/package.json b/package.json index 87333d60..8fd64304 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "sign-package": "node tools/sign-package.js", "screenshot": "node tools/screenshot.js", "profile-sync": "node tools/profile-sync.js", + "generate-sponsorblock-config": "node tools/generate-sponsorblock-config.js", "postinstall": "npx ropm copy && cd playlet-web && npm install" } -} +} \ No newline at end of file diff --git a/playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotification.bs b/playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotification.bs new file mode 100644 index 00000000..68174498 --- /dev/null +++ b/playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotification.bs @@ -0,0 +1,65 @@ +import "pkg:/source/services/SponsorBlock.bs" + +function Init() + m.categoryLabel = m.top.findNode("categoryLabel") + m.translationAnimation = m.top.findNode("translationAnimation") + m.translationAnimationInterpolator = m.top.findNode("translationAnimationInterpolator") + m.animationTimer = m.top.findNode("animationTimer") + + m.top.translation = [1280, 20] + m.translationAnimation.observeField("state", "OnAnimationState") + m.animationTimer.observeField("fire", "OnAnimationTimer") +end function + +function OnSegmentSet() + category = m.top.segment.category + categoryTitle = SponsorBlock.SegmentShortTitle(category) + m.categoryLabel.text = "Skipped" + chr(10) + categoryTitle + AnimateIn() +end function + +function OnAnimationTimer() + AnimateOut() +end function + +function AnimateIn() + m.translationAnimation.unobserveField("state") + m.animationTimer.control = "stop" + m.animationTimer.control = "start" + m.translationAnimation.observeField("state", "OnAnimationState") + + Animate(false) +end function + +function AnimateOut() + Animate(true) +end function + +function Animate(reverse as boolean) as void + ' We are already animating in the requested direction + if m.translationAnimationInterpolator.reverse = reverse and m.translationAnimation.state = "running" + return + end if + + ' Check if we are already at the destination + if m.translationAnimation.state = "stopped" + if reverse + destination = m.translationAnimationInterpolator.keyValue[0][0] + else + destination = m.translationAnimationInterpolator.keyValue[1][0] + end if + current = m.top.translation[0] + if current = destination + return + end if + end if + + m.translationAnimationInterpolator.reverse = reverse + m.translationAnimation.control = "start" +end function + +function OnAnimationState() + if m.translationAnimation.state = "stopped" and m.translationAnimationInterpolator.reverse = true + m.top.getParent().removeChild(m.top) + end if +end function diff --git a/playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotification.xml b/playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotification.xml new file mode 100644 index 00000000..f619f436 --- /dev/null +++ b/playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotification.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotificationManager.bs b/playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotificationManager.bs new file mode 100644 index 00000000..625ea8da --- /dev/null +++ b/playlet-lib/src/components/SponsorBlockNotification/SponsorBlockNotificationManager.bs @@ -0,0 +1,27 @@ +import "pkg:/components/VideoPlayer/Video.bs" + +function ShowSponsorBlockNotifcation(segment as object) + videoContainer = GetVideoContainer() + notification = videoContainer.findNode("sponsorBlockNotification") + if notification = invalid + notification = videoContainer.createChild("SponsorBlockNotification") + notification.id = "sponsorBlockNotification" + end if + notification.segment = segment +end function + +function RemoveSponsorBlockNotifcation() + videoContainer = GetVideoContainer() + notification = videoContainer.findNode("sponsorBlockNotification") + if notification <> invalid + videoContainer.RemoveChild(notification) + end if +end function + +function SponsorBlockNotifcationSetVisible(visible as boolean) + videoContainer = GetVideoContainer() + notification = videoContainer.findNode("sponsorBlockNotification") + if notification <> invalid + notification.visible = visible + end if +end function diff --git a/playlet-lib/src/components/VideoPlayer/Video.bs b/playlet-lib/src/components/VideoPlayer/Video.bs index 26988b29..fff546ef 100644 --- a/playlet-lib/src/components/VideoPlayer/Video.bs +++ b/playlet-lib/src/components/VideoPlayer/Video.bs @@ -1,5 +1,6 @@ import "pkg:/source/services/Invidious.bs" import "pkg:/components/Dialog/DialogUtils.bs" +import "pkg:/components/SponsorBlockNotification/SponsorBlockNotificationManager.bs" function PlayVideo(videoId as string, args = invalid as object) sender = args?.sender @@ -23,6 +24,7 @@ function PlayVideo(videoId as string, args = invalid as object) videoContainer.sender = sender end if videoContainer.videoPlayer = videoContainer.createChild("VideoPlayer") + videoContainer.insertChild(videoContainer.videoPlayer, 0) videoContainer.videoPlayer.id = "VideoPlayer" videoContainer.videoPlayer.metadata = metadata videoContainer.videoPlayer.videoId = videoId @@ -49,6 +51,7 @@ function CloseVideo(setFocus = true as boolean) videoContainer.sender.SetFocus(true) end if end if + SponsorBlockNotifcationSetVisible(false) end function function ToggleVideoPictureInPicture(sender = invalid as object) as boolean diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs index 979a6e73..56883160 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayer.bs @@ -4,6 +4,8 @@ import "pkg:/components/VideoPlayer/Video.bs" import "pkg:/components/VideoPlayer/VideoDetailsTask.bs" function Init() + m.videoContainer = GetVideoContainer() + SetupSponsorBlock() SetupAnimation() SetupBusySpinner() @@ -12,7 +14,6 @@ function Init() m.top.bufferingBar.filledBarBlendColor = "0xFF0000FF" m.top.retrievingBar.filledBarBlendColor = "0xFF0000FF" - m.videoContainer = GetVideoContainer() m.top.enableUI = m.videoContainer.fullscreen m.videoContainer.observeFieldScoped("fullscreen", "OnFullScreenChange") @@ -335,4 +336,5 @@ function OnFullScreenChange() m.translationAnimation.control = "start" m.top.enableUI = m.videoContainer.fullscreen + SponsorBlockNotifcationSetVisible(m.videoContainer.fullscreen) end function diff --git a/playlet-lib/src/components/VideoPlayer/VideoPlayerSponsorBlock.bs b/playlet-lib/src/components/VideoPlayer/VideoPlayerSponsorBlock.bs index 1e7d5667..7f6d6cec 100644 --- a/playlet-lib/src/components/VideoPlayer/VideoPlayerSponsorBlock.bs +++ b/playlet-lib/src/components/VideoPlayer/VideoPlayerSponsorBlock.bs @@ -1,9 +1,13 @@ import "pkg:/source/asyncTask/asyncTask.bs" import "pkg:/components/VideoPlayer/SponsorBlockTask.bs" import "pkg:/components/VideoPlayer/SponsorBlockTrackingTask.bs" +import "pkg:/components/SponsorBlockNotification/SponsorBlockNotificationManager.bs" function SetupSponsorBlock() PositionChapterLabel() + + SponsorBlockNotifcationSetVisible(m.videoContainer.fullscreen) + m.timeLabel = FindTimeLabel() m.top.trickPlayBar.observeField("visible", "OnTrickPlayBarVisible") m.chapterLabelTimer = m.top.findNode("chapterLabelTimer") @@ -123,6 +127,7 @@ function OnPositionChangeSkipSponsorBlockSections() as void ' TODO: if segmentEnd is at the end of the video, close video m.top.seek = segmentEnd print("Skipping section:" + segment["category"]) + ShowSponsorBlockNotifcation(segment) SendSkippedSectionEvent(segment.UUID) return end if diff --git a/playlet-lib/src/components/WebServer/Task/WebServerTask.bs b/playlet-lib/src/components/WebServer/Task/WebServerTask.bs index 306b7b99..efa9b61d 100644 --- a/playlet-lib/src/components/WebServer/Task/WebServerTask.bs +++ b/playlet-lib/src/components/WebServer/Task/WebServerTask.bs @@ -43,11 +43,12 @@ function WebServerLoop() return true end function) - if IsDebugMode() + #if DEBUG allowedOrigin = "*" - else + #else allowedOrigin = `http://${GetLocalIpAddress()}:8888` - end if + #end if + m.server.UseRouter(new Http.CorsMiddleware(allowedOrigin)) #if WEB_SOCKETS m.server.UseRouter(new Http.WebSocketMiddleware()) @@ -58,13 +59,13 @@ function WebServerLoop() m.server.UseRouter(new Http.StateApiRouter()) m.server.UseRouter(new Http.InvidiousRouter()) m.server.UseRouter(new Http.HttpStaticFilesRouter("/", m.settings.WwwRoot, false, false, false)) - if IsDebugMode() + #if DEBUG m.server.UseRouter(new Http.HttpStaticFilesRouter("/debug/libpkg", "libpkg:/", true, true, false)) m.server.UseRouter(new Http.HttpStaticFilesRouter("/debug/pkg", "pkg:/", true, true)) m.server.UseRouter(new Http.HttpStaticFilesRouter("/debug/tmp", "tmp:/", true, true)) m.server.UseRouter(new Http.HttpStaticFilesRouter("/debug/cachefs", "cachefs:/", true, true)) m.server.UseRouter(new Http.HttpStaticFilesRouter("/debug/common", "common:/", true, true)) - end if + #end if #if WEB_SERVER_BASIC_AUTH m.server.UseRouter(new Http.BasicAuthMiddleware()) #end if diff --git a/playlet-lib/src/components/WebServer/Task/WebServerTask.xml b/playlet-lib/src/components/WebServer/Task/WebServerTask.xml index df185c42..af65b7ea 100644 --- a/playlet-lib/src/components/WebServer/Task/WebServerTask.xml +++ b/playlet-lib/src/components/WebServer/Task/WebServerTask.xml @@ -5,5 +5,4 @@ -