From acd5b69087a163d32f27f6fc03eb95628f7bcc89 Mon Sep 17 00:00:00 2001 From: Scroogey-SN Date: Fri, 29 Aug 2025 17:54:39 +0000 Subject: [PATCH] fix #2443: add parseYoutubeStart() to convert start times (#2447) * fix #2443: add parseYoutubeStart() to convert start times * handle null parameter * add spaces for lint * switch to regex, handles more cases, simply don't touch unexpected input * force braces for lint --- lib/url.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/url.js b/lib/url.js index 4c5baa17..2ead6386 100644 --- a/lib/url.js +++ b/lib/url.js @@ -78,6 +78,17 @@ export function parseInternalLinks (href) { return {} } +export function parseYoutubeStart (t) { + // https://stackoverflow.com/questions/17379268/youtube-dropped-t-start-time-support-in-direct-url-and-embed-videos + // https://developers.google.com/youtube/player_parameters#start + if (!t || !t.match(/^([0-9]+[smh])+$/g)) return t + let r = 0 + for (const m of t.matchAll(/([0-9]+)([smh])/g)) { + r += parseInt(m[1]) * Math.pow(60, 'smh'.indexOf(m[2])) + } + return r.toString() +} + export function parseEmbedUrl (href) { if (!href) return null @@ -132,7 +143,7 @@ export function parseEmbedUrl (href) { id: searchParams.get('v'), meta: { href, - start: searchParams.get('t') + start: parseYoutubeStart(searchParams.get('t')) } } } @@ -152,7 +163,7 @@ export function parseEmbedUrl (href) { id: pathname.slice(1), // remove leading slash meta: { href, - start: searchParams.get('t') + start: parseYoutubeStart(searchParams.get('t')) } } }