From c70e7bf05b2ad5da0955f07e69a3e1c624297f8c Mon Sep 17 00:00:00 2001 From: reda777 <60011372+reda777@users.noreply.github.com> Date: Tue, 21 May 2024 21:13:17 +0100 Subject: [PATCH] Add support for timestamped youtube links --- assets/chat/js/hashlinkconverter.js | 11 ++++++----- assets/chat/js/hashlinkconverter.test.js | 15 +++++++++++++++ 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/assets/chat/js/hashlinkconverter.js b/assets/chat/js/hashlinkconverter.js index ab0f0bc7..7e6c6966 100644 --- a/assets/chat/js/hashlinkconverter.js +++ b/assets/chat/js/hashlinkconverter.js @@ -42,13 +42,14 @@ class HashLinkConverter { case 'www.youtube.com': case 'youtube.com': match = pathname.match(this.youtubeRegex); - if (match) { - return `#youtube/${match[1]}`; - } - videoId = url.searchParams.get('v'); timestamp = url.searchParams.get('t'); + videoId = url.searchParams.get('v'); if (!videoId) { - throw new Error(MISSING_VIDEO_ID_ERROR); + if (match) { + videoId = match[1]; + } else { + throw new Error(MISSING_VIDEO_ID_ERROR); + } } return timestamp ? `#youtube/${videoId}?t=${timestamp}` diff --git a/assets/chat/js/hashlinkconverter.test.js b/assets/chat/js/hashlinkconverter.test.js index 1d5c9c94..f9887942 100644 --- a/assets/chat/js/hashlinkconverter.test.js +++ b/assets/chat/js/hashlinkconverter.test.js @@ -54,16 +54,31 @@ describe('Valid embeds', () => { 'https://www.youtube.com/live/EHs-_2ddcUQ?feature=share', '#youtube/EHs-_2ddcUQ', ], + [ + 'Youtube live stream link with timestamp', + 'https://www.youtube.com/live/XstSLjhzKX8?t=11447', + '#youtube/XstSLjhzKX8?t=11447', + ], [ 'Youtube shorts link', 'https://youtube.com/shorts/Bg1JpTqc0iA?si=7pzY1RnY2fEe9A8_', '#youtube/Bg1JpTqc0iA', ], + [ + 'Youtube shorts link with timestamp', + 'https://www.youtube.com/shorts/Bg1JpTqc0iA?t=4&feature=share', + '#youtube/Bg1JpTqc0iA?t=4', + ], [ 'Youtube embed link', 'https://www.youtube.com/embed/Akala8bkIu8', '#youtube/Akala8bkIu8', ], + [ + 'Youtube embed link timestamp', + 'https://www.youtube.com/embed/Akala8bkIu8?t=5', + '#youtube/Akala8bkIu8?t=5', + ], [ 'Rumble embed', 'https://rumble.com/embed/v26pcdc/?pub=4',