From a1ccbbefa316dd34ed3d4295a884e9b2f0b7dfdf Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Sun, 4 Oct 2020 21:31:16 +0200 Subject: [PATCH] Support time --- index.html | 15 ++++++++++++++- src/Core.elm | 45 ++++++++++++++++++++++++++++++++++++++++++--- src/Ports.elm | 2 +- 3 files changed, 57 insertions(+), 5 deletions(-) diff --git a/index.html b/index.html index 4a623ac..5f7eb68 100644 --- a/index.html +++ b/index.html @@ -17,9 +17,22 @@ flags: { width: window.innerWidth, height: window.innerHeight } }); + var lastId, player; + app.ports.registerVideo.subscribe(function(args) { + var time = parseInt(args[2], 10) || undefined; + requestAnimationFrame(function() { - vd.setup(args[0], args[1] + "/manifest.mpd") + if (args[0] !== lastId) { + lastId = args[0]; + + player = vd.setup(args[0], { + v: args[1] + "/manifest.mpd", + t: parseInt(args[2], 10) || 0 + }); + } else if (time !== undefined ){ + player.currentTime(time); + } }); }); diff --git a/src/Core.elm b/src/Core.elm index 86faa53..29325fc 100644 --- a/src/Core.elm +++ b/src/Core.elm @@ -2,6 +2,7 @@ module Core exposing (FullModel(..), Model, Msg(..), Page(..), init, subscriptio import Browser.Events as Events import Browser.Navigation as Nav +import Dict exposing (Dict) import Element import Ports import Task @@ -85,9 +86,32 @@ update msg model = ( UrlReceived url, Loaded m ) -> let - split = - String.split "/" (Maybe.withDefault "" url.fragment) + splits = + String.split "?" (Maybe.withDefault "" url.fragment) + ( split, args ) = + case splits of + h1 :: h2 :: _ -> + ( String.split "/" h1, parseQueryString h2 ) + + h1 :: _ -> + ( String.split "/" h1, Dict.empty ) + + _ -> + ( [], Dict.empty ) + + time = + case Maybe.map String.toInt (Dict.get "t" args) of + Just (Just 0) -> + Nothing + + Just (Just t) -> + Just t + + _ -> + Nothing + + -- String.split "/" (Maybe.withDefault "" (List.head splits)) ( playlistName, videoName ) = case split of p :: v :: _ -> @@ -114,7 +138,7 @@ update msg model = case ( playlist, video ) of ( Just p, Just v ) -> ( Video p v - , Ports.registerVideo ( Twitch.videoId v, v.url ) + , Ports.registerVideo ( Twitch.videoId v, v.url, time ) ) ( Just p, Nothing ) -> @@ -127,3 +151,18 @@ update msg model = _ -> ( model, Cmd.none ) + + +splitter : String -> Maybe ( String, String ) +splitter input = + case String.split "=" input of + h :: t -> + Just ( h, String.join "=" t ) + + _ -> + Nothing + + +parseQueryString : String -> Dict String String +parseQueryString input = + Dict.fromList (List.filterMap splitter (String.split "&" input)) diff --git a/src/Ports.elm b/src/Ports.elm index 5b1719f..45e3bbc 100644 --- a/src/Ports.elm +++ b/src/Ports.elm @@ -1,4 +1,4 @@ port module Ports exposing (registerVideo) -port registerVideo : ( String, String ) -> Cmd msg +port registerVideo : ( String, String, Maybe Int ) -> Cmd msg