From 5034eda3861675bb3be0c93e96553da87ac420ba Mon Sep 17 00:00:00 2001 From: Jan Cernik <66427541+jancernik@users.noreply.github.com> Date: Mon, 3 Jul 2023 10:39:37 -0300 Subject: [PATCH] FIX: Allow to use `%h%m%s` for youtube `t` param (#22299) --- .../discourse/components/lazy-iframe.js | 18 ++++++++++++++++-- .../javascripts/lib/lazy-video-attributes.js | 2 +- .../lib/lazy-videos/lazy_youtube.rb | 5 +++-- 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/plugins/discourse-lazy-videos/assets/javascripts/discourse/components/lazy-iframe.js b/plugins/discourse-lazy-videos/assets/javascripts/discourse/components/lazy-iframe.js index b75ad65fa66..d5507392d79 100644 --- a/plugins/discourse-lazy-videos/assets/javascripts/discourse/components/lazy-iframe.js +++ b/plugins/discourse-lazy-videos/assets/javascripts/discourse/components/lazy-iframe.js @@ -5,8 +5,8 @@ export default class LazyVideo extends Component { switch (this.args.providerName) { case "youtube": let url = `https://www.youtube.com/embed/${this.args.videoId}?autoplay=1`; - if (this.args.startTime > 0) { - url += `&start=${this.args.startTime}`; + if (this.args.startTime) { + url += `&start=${this.convertToSeconds(this.args.startTime)}`; } return url; case "vimeo": @@ -17,4 +17,18 @@ export default class LazyVideo extends Component { return `https://www.tiktok.com/embed/v2/${this.args.videoId}`; } } + + convertToSeconds(time) { + const match = time.toString().match(/(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?/); + const [hours, minutes, seconds] = match.slice(1); + + if (hours || minutes || seconds) { + const h = parseInt(hours, 10) || 0; + const m = parseInt(minutes, 10) || 0; + const s = parseInt(seconds, 10) || 0; + + return h * 3600 + m * 60 + s; + } + return time; + } } diff --git a/plugins/discourse-lazy-videos/assets/javascripts/lib/lazy-video-attributes.js b/plugins/discourse-lazy-videos/assets/javascripts/lib/lazy-video-attributes.js index c8e9bc272c8..2efc85d80c0 100644 --- a/plugins/discourse-lazy-videos/assets/javascripts/lib/lazy-video-attributes.js +++ b/plugins/discourse-lazy-videos/assets/javascripts/lib/lazy-video-attributes.js @@ -8,7 +8,7 @@ export default function getVideoAttributes(cooked) { const thumbnail = img?.getAttribute("src"); const dominantColor = img?.dataset?.dominantColor; const title = cooked.dataset.videoTitle; - const startTime = cooked.dataset.videoStartTime || 0; + const startTime = cooked.dataset.videoStartTime; const providerName = cooked.dataset.providerName; const id = cooked.dataset.videoId; diff --git a/plugins/discourse-lazy-videos/lib/lazy-videos/lazy_youtube.rb b/plugins/discourse-lazy-videos/lib/lazy-videos/lazy_youtube.rb index 78217a6d51f..334afc69d76 100644 --- a/plugins/discourse-lazy-videos/lib/lazy-videos/lazy_youtube.rb +++ b/plugins/discourse-lazy-videos/lib/lazy-videos/lazy_youtube.rb @@ -21,7 +21,8 @@ class Onebox::Engine::YoutubeOnebox end escaped_title = ERB::Util.html_escape(video_title) - escaped_start_time = ERB::Util.html_escape(params["t"] || 0) + escaped_start_time = ERB::Util.html_escape(params["t"]) + t_param = "&t=#{escaped_start_time}" if escaped_start_time.present? <<~HTML