diff --git a/assets/js/player.js b/assets/js/player.js index bb53ac24..16bb2752 100644 --- a/assets/js/player.js +++ b/assets/js/player.js @@ -98,11 +98,13 @@ if (video_data.params.quality === 'dash') { /** * Function for add time argument to url + * * @param {String} url + * @param {String} [base] * @returns {URL} urlWithTimeArg */ -function addCurrentTimeToURL(url) { - var urlUsed = new URL(url); +function addCurrentTimeToURL(url, base) { + var urlUsed = new URL(url, base); urlUsed.searchParams.delete('start'); var currentTime = Math.ceil(player.currentTime()); if (currentTime > 0) @@ -112,6 +114,50 @@ function addCurrentTimeToURL(url) { return urlUsed; } +/** + * Global variable to save the last timestamp (in full seconds) at which the external + * links were updated by the 'timeupdate' callback below. + * + * It is initialized to 5s so that the video will always restart from the beginning + * if the user hasn't really started watching before switching to the other website. + */ +var timeupdate_last_ts = 5; + +/** + * Callback that updates the timestamp on all external links + */ +player.on('timeupdate', function () { + // Only update once every second + let current_ts = Math.floor(player.currentTime()); + if (current_ts > timeupdate_last_ts) timeupdate_last_ts = current_ts; + else return; + + // YouTube links + + let elem_yt_watch = document.getElementById('link-yt-watch'); + let elem_yt_embed = document.getElementById('link-yt-embed'); + + let base_url_yt_watch = elem_yt_watch.getAttribute('data-base-url'); + let base_url_yt_embed = elem_yt_embed.getAttribute('data-base-url'); + + elem_yt_watch.href = addCurrentTimeToURL(base_url_yt_watch); + elem_yt_embed.href = addCurrentTimeToURL(base_url_yt_embed); + + // Invidious links + + let domain = window.location.origin; + + let elem_iv_embed = document.getElementById('link-iv-embed'); + let elem_iv_other = document.getElementById('link-iv-other'); + + let base_url_iv_embed = elem_iv_embed.getAttribute('data-base-url'); + let base_url_iv_other = elem_iv_other.getAttribute('data-base-url'); + + elem_iv_embed.href = addCurrentTimeToURL(base_url_iv_embed, domain); + elem_iv_other.href = addCurrentTimeToURL(base_url_iv_other, domain); +}); + + var shareOptions = { socials: ['fbFeed', 'tw', 'reddit', 'email'], diff --git a/src/invidious/routes/watch.cr b/src/invidious/routes/watch.cr index e5cf3716..3d935f0a 100644 --- a/src/invidious/routes/watch.cr +++ b/src/invidious/routes/watch.cr @@ -30,14 +30,6 @@ module Invidious::Routes::Watch return env.redirect "/" end - embed_link = "/embed/#{id}" - if env.params.query.size > 1 - embed_params = HTTP::Params.parse(env.params.query.to_s) - embed_params.delete_all("v") - embed_link += "?" - embed_link += embed_params.to_s - end - plid = env.params.query["list"]?.try &.gsub(/[^a-zA-Z0-9_-]/, "") continuation = process_continuation(env.params.query, plid, id) diff --git a/src/invidious/views/watch.ecr b/src/invidious/views/watch.ecr index 62a154a4..07474896 100644 --- a/src/invidious/views/watch.ecr +++ b/src/invidious/views/watch.ecr @@ -113,19 +113,36 @@ we're going to need to do it here in order to allow for translations.
- <% if env.get("preferences").as(Preferences).automatic_instance_redirect%> - "><%= translate(locale, "Switch Invidious Instance") %> - <% else %> - <%= translate(locale, "Switch Invidious Instance") %> - <% end %> + <%- link_iv_other = IV::Frontend::Misc.redirect_url(env) -%> + <%= translate(locale, "Switch Invidious Instance") %>
+- <%= translate(locale, "videoinfo_invidious_embed_link") %> + <%- + params_iv_embed = env.params.query.dup + params_iv_embed.delete_all("v") + + link_iv_embed = URI.new(path: "/embed/#{id}") + link_iv_embed = IV::HttpServer::Utils.add_params_to_url(link_iv_embed, params_iv_embed) + -%> + <%= translate(locale, "videoinfo_invidious_embed_link") %>
+