From 472a2d7e76330371a712c37d473a3772d4edc1a3 Mon Sep 17 00:00:00 2001 From: broquemonsieur Date: Sat, 5 Aug 2023 02:53:36 -0700 Subject: [PATCH] Use markers to implement timestamps --- assets/js/player.js | 17 +++++-- assets/js/watch.js | 68 +++++++++++++------------- src/invidious/database/compilations.cr | 12 +++++ src/invidious/routes/watch.cr | 7 +++ src/invidious/views/watch.ecr | 8 ++- 5 files changed, 72 insertions(+), 40 deletions(-) diff --git a/assets/js/player.js b/assets/js/player.js index bb53ac24..36dd3eea 100644 --- a/assets/js/player.js +++ b/assets/js/player.js @@ -200,13 +200,20 @@ if (!video_data.params.listen && video_data.vr && video_data.params.vr_mode) { // Add markers if (video_data.params.video_start > 0 || video_data.params.video_end > 0) { var markers = [{ time: video_data.params.video_start, text: 'Start' }]; - - if (video_data.params.video_end < 0) { - markers.push({ time: video_data.length_seconds - 0.5, text: 'End' }); - } else { - markers.push({ time: video_data.params.video_end, text: 'End' }); + if (video_data.starting_timestamp_seconds) { + markers = [{ time: video_data.starting_timestamp_seconds, text: 'Start' }]; } + if (!video_data.ending_timestamp_seconds) { + if (video_data.params.video_end < 0) { + markers.push({ time: video_data.length_seconds - 0.5, text: 'End' }); + } else { + markers.push({ time: video_data.params.video_end, text: 'End' }); + } + } else { + markers.push({ time: video_data.ending_timestamp_seconds, text: 'End' }); + } + player.markers({ onMarkerReached: function (marker) { if (marker.text === 'End') diff --git a/assets/js/watch.js b/assets/js/watch.js index 809a9f19..4930d6a7 100644 --- a/assets/js/watch.js +++ b/assets/js/watch.js @@ -103,7 +103,7 @@ function continue_autoplay(event) { } function get_compilation(compid) { - var compilation = document.getElementById('compilations'); + var compilation = document.getElementById('compilation'); compilation.innerHTML = spinnerHTMLwithHR; @@ -111,47 +111,49 @@ function get_compilation(compid) { compid_url = '/api/v1/compilations/' + compid + '?index=' + video_data.index + '&continuation=' + video_data.id + - '&format=html&hl=' + video_data.preferences.locale; + '&format=html&hl=' + video_data.preferences.locale; + + console.log("Send "+compid_url); helpers.xhr('GET', compid_url, {retries: 5, entity_name: 'compilation'}, { on200: function (response) { compilation.innerHTML = response.compilationHtml; - if (!response.nextVideo) return; + if (!response.nextVideo) return; - var nextVideo = document.getElementById(response.nextVideo); - nextVideo.parentNode.parentNode.scrollTop = nextVideo.offsetTop; + var nextVideo = document.getElementById(response.nextVideo); + nextVideo.parentNode.parentNode.scrollTop = nextVideo.offsetTop; - player.on('ended', function () { - var url = new URL('https://example.com/watch?v=' + response.nextVideo); + player.on('ended', function () { + var url = new URL('https://example.com/watch?v=' + response.nextVideo); - url.searchParams.set('list', compid); - if (!plid.startsWith('RD')) - url.searchParams.set('index', response.index); - if (video_data.params.autoplay || video_data.params.continue_autoplay) - url.searchParams.set('autoplay', '1'); - if (video_data.params.listen !== video_data.preferences.listen) - url.searchParams.set('listen', video_data.params.listen); - if (video_data.params.speed !== video_data.preferences.speed) - url.searchParams.set('speed', video_data.params.speed); - if (video_data.params.local !== video_data.preferences.local) - url.searchParams.set('local', video_data.params.local); + url.searchParams.set('list', compid); + if (!compid.startsWith('RD')) + url.searchParams.set('index', response.index); + if (video_data.params.autoplay || video_data.params.continue_autoplay) + url.searchParams.set('autoplay', '1'); + if (video_data.params.listen !== video_data.preferences.listen) + url.searchParams.set('listen', video_data.params.listen); + if (video_data.params.speed !== video_data.preferences.speed) + url.searchParams.set('speed', video_data.params.speed); + if (video_data.params.local !== video_data.preferences.local) + url.searchParams.set('local', video_data.params.local); - location.assign(url.pathname + url.search); - }); - }, - onNon200: function (xhr) { - compilation.innerHTML = ''; - document.getElementById('continue').style.display = ''; - }, - onError: function (xhr) { - compilation.innerHTML = spinnerHTMLwithHR; - }, - onTimeout: function (xhr) { - compilation.innerHTML = spinnerHTMLwithHR; - } - }); - } + location.assign(url.pathname + url.search); + }); + }, + onNon200: function (xhr) { + compilation.innerHTML = ''; + document.getElementById('continue').style.display = ''; + }, + onError: function (xhr) { + compilation.innerHTML = spinnerHTMLwithHR; + }, + onTimeout: function (xhr) { + compilation.innerHTML = spinnerHTMLwithHR; + } + }); +} function get_playlist(plid) { var playlist = document.getElementById('playlist'); diff --git a/src/invidious/database/compilations.cr b/src/invidious/database/compilations.cr index 8ccc532f..aa7a7341 100644 --- a/src/invidious/database/compilations.cr +++ b/src/invidious/database/compilations.cr @@ -258,6 +258,18 @@ module Invidious::Database::CompilationVideos return PG_DB.query_all(request, compid, index, video_index, offset, limit, as: CompilationVideo) end + def select_timestamps(compid : String, vid : String) + + request = <<-SQL + SELECT starting_timestamp_seconds,ending_timestamp_seconds FROM compilation_videos + WHERE compid = $1 AND id = $2 + LIMIT 1 + SQL + + return PG_DB.query_one?(request, compid, vid, as: {Int32,Int32}) + + end + def select_id_from_order_index(order_index : Int32) request = <<-SQL SELECT id FROM compilation_videos diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index bd0f46e1..e5bf1668 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -44,6 +44,13 @@ module Invidious::Routes::Watch continuation = process_continuation(env.params.query, plid, id) elsif env.params.query["list"]?.try &.starts_with? "IVCMP" compid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "") + if (!compid.nil?) + timestamps = Invidious::Database::CompilationVideos.select_timestamps(compid, id) + if (!timestamps.nil?) + starting_timestamp_seconds=timestamps[0] + ending_timestamp_seconds=timestamps[1] + end + end continuation = process_continuation(env.params.query, compid, id) end diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index 890732e4..4c844534 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -51,6 +51,8 @@ we're going to need to do it here in order to allow for translations. "index" => continuation, "plid" => plid, "compid" => compid, + "starting_timestamp_seconds" => starting_timestamp_seconds, + "ending_timestamp_seconds" => ending_timestamp_seconds, "length_seconds" => video.length_seconds.to_f, "play_next" => !video.related_videos.empty? && !plid && params.continue, "next_video" => video.related_videos.select { |rv| rv["id"]? }[0]?.try &.["id"], @@ -313,16 +315,18 @@ we're going to need to do it here in order to allow for translations. - <% if params.related_videos || plid %> + <% if params.related_videos || plid || compid%>
<% if plid %>
+ <% elsif compid %> +
<% end %> <% if params.related_videos %>
<% if !video.related_videos.empty? %> -
style="display:none"<% end %>> +
style="display:none"<% end %>>
checked<% end %>>