From 4bd95b5ef24506edf9d6690d3f12f4afc05afca1 Mon Sep 17 00:00:00 2001 From: Mary <148872143+mary-ext@users.noreply.github.com> Date: Fri, 19 Jan 2024 12:25:57 +0700 Subject: [PATCH] Set start time on YouTube embeds (#2565) * fix: set start time on yt embeds * fix: re-encode to be on the safe side * chore: fix embed tests --- __tests__/lib/string.test.ts | 14 +++++++------- src/lib/strings/embed-player.ts | 6 ++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/__tests__/lib/string.test.ts b/__tests__/lib/string.test.ts index edddcb3e..b3aa48f4 100644 --- a/__tests__/lib/string.test.ts +++ b/__tests__/lib/string.test.ts @@ -463,44 +463,44 @@ describe('parseEmbedPlayerFromUrl', () => { type: 'youtube_video', source: 'youtube', playerUri: - 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1', + 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1&start=0', }, { type: 'youtube_video', source: 'youtube', playerUri: - 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1', + 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1&start=0', }, { type: 'youtube_video', source: 'youtube', playerUri: - 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1', + 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1&start=0', }, { type: 'youtube_video', source: 'youtube', playerUri: - 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1', + 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1&start=0', }, { type: 'youtube_video', source: 'youtube', playerUri: - 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1', + 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1&start=0', }, { type: 'youtube_short', source: 'youtubeShorts', hideDetails: true, playerUri: - 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1', + 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1&start=0', }, { type: 'youtube_video', source: 'youtube', playerUri: - 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1', + 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1&start=0', }, undefined, diff --git a/src/lib/strings/embed-player.ts b/src/lib/strings/embed-player.ts index 0f97eb08..3270b6f0 100644 --- a/src/lib/strings/embed-player.ts +++ b/src/lib/strings/embed-player.ts @@ -68,11 +68,12 @@ export function parseEmbedPlayerFromUrl( // youtube if (urlp.hostname === 'youtu.be') { const videoId = urlp.pathname.split('/')[1] + const seek = encodeURIComponent(urlp.searchParams.get('t') ?? 0) if (videoId) { return { type: 'youtube_video', source: 'youtube', - playerUri: `https://www.youtube.com/embed/${videoId}?autoplay=1&playsinline=1`, + playerUri: `https://www.youtube.com/embed/${videoId}?autoplay=1&playsinline=1&start=${seek}`, } } } @@ -84,13 +85,14 @@ export function parseEmbedPlayerFromUrl( const [_, page, shortVideoId] = urlp.pathname.split('/') const videoId = page === 'shorts' ? shortVideoId : (urlp.searchParams.get('v') as string) + const seek = encodeURIComponent(urlp.searchParams.get('t') ?? 0) if (videoId) { return { type: page === 'shorts' ? 'youtube_short' : 'youtube_video', source: page === 'shorts' ? 'youtubeShorts' : 'youtube', hideDetails: page === 'shorts' ? true : undefined, - playerUri: `https://www.youtube.com/embed/${videoId}?autoplay=1&playsinline=1`, + playerUri: `https://www.youtube.com/embed/${videoId}?autoplay=1&playsinline=1&start=${seek}`, } } }