Fix not getting youtube video id from url

This commit is contained in:
Hongbo Wu
2022-05-17 21:51:03 +08:00
parent 1a51c27350
commit ca662964e6
2 changed files with 10 additions and 16 deletions

View File

@ -11,7 +11,7 @@ const YOUTUBE_URL_MATCH =
function getYoutubeVideoId(url) {
const u = new URL(url);
const videoId = u.searchParams['v']
const videoId = u.searchParams.get('v');
if (!videoId) {
const match = url.toString().match(YOUTUBE_URL_MATCH)
if (match === null || match.length < 6 || !match[5]) {
@ -24,13 +24,10 @@ function getYoutubeVideoId(url) {
exports.getYoutubeVideoId = getYoutubeVideoId
exports.youtubeHandler = {
shouldPrehandle: (url, env) => {
return YOUTUBE_URL_MATCH.test(url.toString())
},
prehandle: async (url, env) => {
const videoId = getYoutubeVideoId(url)
if (!videoId) {

View File

@ -10,27 +10,24 @@ const YOUTUBE_URL_MATCH =
/^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube\.com|youtu.be))(\/(?:[\w-]+\?v=|embed\/|v\/)?)([\w-]+)(\S+)?$/
function getYoutubeVideoId(url) {
const u = new URL(url);
const videoId = u.searchParams['v']
if (!videoId) {
const match = url.toString().match(YOUTUBE_URL_MATCH)
if (match === null || match.length < 6 || !match[5]) {
return undefined
}
return match[5]
const u = new URL(url);
const videoId = u.searchParams.get('v');
if (!videoId) {
const match = url.toString().match(YOUTUBE_URL_MATCH)
if (match === null || match.length < 6 || !match[5]) {
return undefined
}
return videoId
return match[5]
}
return videoId
}
exports.getYoutubeVideoId = getYoutubeVideoId
exports.youtubeHandler = {
shouldPrehandle: (url, env) => {
return YOUTUBE_URL_MATCH.test(url.toString())
},
prehandle: async (url, env) => {
const videoId = getYoutubeVideoId(url)
if (!videoId) {