forked from BRETT/BRETT
Initial Commit
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
export function getYoutubeId(href: string): string | null {
|
||||
try {
|
||||
const url = new URL(href)
|
||||
if (url.hostname === 'youtu.be') {
|
||||
return url.pathname.slice(1).split('/')[0] || null
|
||||
}
|
||||
if (url.hostname === 'youtube.com' || url.hostname.endsWith('.youtube.com')) {
|
||||
if (url.pathname === '/watch') return url.searchParams.get('v')
|
||||
const m = url.pathname.match(/^\/(shorts|embed|live)\/([\w-]+)/)
|
||||
if (m?.[2]) return m[2]
|
||||
}
|
||||
return null
|
||||
}
|
||||
catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function youtubeEmbedUrl(videoId: string) {
|
||||
return `https://www.youtube.com/embed/${videoId}`
|
||||
}
|
||||
|
||||
export function youtubeThumbUrl(videoId: string) {
|
||||
return `https://i.ytimg.com/vi/${videoId}/mqdefault.jpg`
|
||||
}
|
||||
Reference in New Issue
Block a user