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
zio/stable
Mary 2024-01-19 12:25:57 +07:00 committed by GitHub
parent 0dfe740dd3
commit 4bd95b5ef2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 9 deletions

View File

@ -463,44 +463,44 @@ describe('parseEmbedPlayerFromUrl', () => {
type: 'youtube_video', type: 'youtube_video',
source: 'youtube', source: 'youtube',
playerUri: 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', type: 'youtube_video',
source: 'youtube', source: 'youtube',
playerUri: 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', type: 'youtube_video',
source: 'youtube', source: 'youtube',
playerUri: 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', type: 'youtube_video',
source: 'youtube', source: 'youtube',
playerUri: 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', type: 'youtube_video',
source: 'youtube', source: 'youtube',
playerUri: 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', type: 'youtube_short',
source: 'youtubeShorts', source: 'youtubeShorts',
hideDetails: true, hideDetails: true,
playerUri: 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', type: 'youtube_video',
source: 'youtube', source: 'youtube',
playerUri: playerUri:
'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1', 'https://www.youtube.com/embed/videoId?autoplay=1&playsinline=1&start=0',
}, },
undefined, undefined,

View File

@ -68,11 +68,12 @@ export function parseEmbedPlayerFromUrl(
// youtube // youtube
if (urlp.hostname === 'youtu.be') { if (urlp.hostname === 'youtu.be') {
const videoId = urlp.pathname.split('/')[1] const videoId = urlp.pathname.split('/')[1]
const seek = encodeURIComponent(urlp.searchParams.get('t') ?? 0)
if (videoId) { if (videoId) {
return { return {
type: 'youtube_video', type: 'youtube_video',
source: 'youtube', 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 [_, page, shortVideoId] = urlp.pathname.split('/')
const videoId = const videoId =
page === 'shorts' ? shortVideoId : (urlp.searchParams.get('v') as string) page === 'shorts' ? shortVideoId : (urlp.searchParams.get('v') as string)
const seek = encodeURIComponent(urlp.searchParams.get('t') ?? 0)
if (videoId) { if (videoId) {
return { return {
type: page === 'shorts' ? 'youtube_short' : 'youtube_video', type: page === 'shorts' ? 'youtube_short' : 'youtube_video',
source: page === 'shorts' ? 'youtubeShorts' : 'youtube', source: page === 'shorts' ? 'youtubeShorts' : 'youtube',
hideDetails: page === 'shorts' ? true : undefined, 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}`,
} }
} }
} }