From 95a03f9668175d29355f205f81910c346f254f03 Mon Sep 17 00:00:00 2001 From: JimmyLv Date: Tue, 7 Mar 2023 15:32:31 +0800 Subject: [PATCH] feat: support youtube url redirect from url address --- pages/[...slug].tsx | 15 ++++++++++----- ...{getValidatedUrl.tsx => getVideoIdFromUrl.tsx} | 8 ++++++-- 2 files changed, 16 insertions(+), 7 deletions(-) rename utils/{getValidatedUrl.tsx => getVideoIdFromUrl.tsx} (62%) diff --git a/pages/[...slug].tsx b/pages/[...slug].tsx index ae247c6f..51c7054e 100644 --- a/pages/[...slug].tsx +++ b/pages/[...slug].tsx @@ -15,7 +15,7 @@ import { useToast } from "~/hooks/use-toast"; import { useSummarize } from "~/hooks/useSummarize"; import { VideoService } from "~/lib/types"; import { extractUrl } from "~/utils/extractUrl"; -import { getValidatedUrl } from "~/utils/getValidatedUrl"; +import { getVideoIdFromUrl } from "~/utils/getVideoIdFromUrl"; import getVideoId from "get-video-id"; export const Home: NextPage = () => { @@ -42,14 +42,17 @@ export const Home: NextPage = () => { useEffect(() => { // https://www.youtube.com/watch?v=DHhOgWPKIKU // todo: support redirect from www.youtube.jimmylv.cn/watch?v=DHhOgWPKIKU - const validatedUrl = getValidatedUrl( + const validatedUrl = getVideoIdFromUrl( router.isReady, currentVideoUrl, - urlState + urlState, + searchParams ); + console.log("========validatedUrl========", validatedUrl); + validatedUrl && generateSummary(validatedUrl); - }, [router.isReady, urlState]); + }, [router.isReady, urlState, searchParams]); const validateUrl = (url?: string) => { // note: auto refactor by ChatGPT @@ -65,6 +68,7 @@ export const Home: NextPage = () => { return; } + // 来自输入框 if (!url) { // -> '/video/BV12Y4y127rj' const curUrl = String(videoUrl.split(".com")[1]); @@ -75,6 +79,8 @@ export const Home: NextPage = () => { }; const generateSummary = async (url?: string) => { resetSummary(); + validateUrl(url); + const videoUrl = url || currentVideoUrl; const { id, service } = getVideoId(videoUrl); if (service === VideoService.Youtube && id) { @@ -86,7 +92,6 @@ export const Home: NextPage = () => { return; } - validateUrl(url); const bvId = extractUrl(videoUrl); if (!bvId) { return; diff --git a/utils/getValidatedUrl.tsx b/utils/getVideoIdFromUrl.tsx similarity index 62% rename from utils/getValidatedUrl.tsx rename to utils/getVideoIdFromUrl.tsx index 0608a71c..9bb56a44 100644 --- a/utils/getValidatedUrl.tsx +++ b/utils/getVideoIdFromUrl.tsx @@ -1,7 +1,8 @@ -export function getValidatedUrl( +export function getVideoIdFromUrl( isReady: boolean, currentVideoUrl: string, - urlState?: string | string[] + urlState?: string | string[], + searchParams?: URLSearchParams ): string | undefined { const isValidatedUrl = isReady && @@ -11,6 +12,9 @@ export function getValidatedUrl( urlState.every((subslug: string) => typeof subslug === "string"); if (isValidatedUrl) { + if (urlState[0] === "watch") { + return "https://youtube.com/watch?v=" + searchParams?.get("v"); + } return `https://bilibili.com/${(urlState as string[]).join("/")}`; } }