From b99358b426c6ebe6f47cdb0cc444be5f37a34f49 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Sat, 30 Dec 2023 12:28:48 +0800 Subject: [PATCH] feat: fetch rss content without no-cors --- src/lib/rss.ts | 6 ++---- src/lib/utils.ts | 10 ++++++++++ src/tabs/preview.tsx | 26 +++++++++++--------------- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/lib/rss.ts b/src/lib/rss.ts index b94a8cbf..1eb83d9a 100644 --- a/src/lib/rss.ts +++ b/src/lib/rss.ts @@ -1,5 +1,5 @@ import type { RSSData } from "./types" -import { parseRSS } from "./utils" +import { fetchRSSContent, parseRSS } from "./utils" export async function getPageRSS(data: { html: string @@ -150,9 +150,7 @@ export async function getPageRSS(data: { await Promise.all(uncertain.map((feed) => { return new Promise(async (resolve) => { try { - const content = await (await fetch(feed.url, { - mode: "no-cors", - })).text() + const content = await fetchRSSContent(feed.url) const result = parseRSS(content) if (result) { if (result.title) { diff --git a/src/lib/utils.ts b/src/lib/utils.ts index e92b2bdf..4c9e72b4 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -26,3 +26,13 @@ export function parseRSS(content: string) { return null } } + +export async function fetchRSSContent(url: string) { + let content + try { + content = await (await fetch(url)).text() + } catch (error) { + // TODO + } + return content +} \ No newline at end of file diff --git a/src/tabs/preview.tsx b/src/tabs/preview.tsx index 993f8cab..7dbf12c9 100644 --- a/src/tabs/preview.tsx +++ b/src/tabs/preview.tsx @@ -12,6 +12,7 @@ import { CardTitle, } from "~/lib/components/Card" import RSSItem from "~/popup/RSSItem"; +import { fetchRSSContent } from "~/lib/utils" const parser = new Parser(); @@ -24,21 +25,16 @@ function PreviewPage() { const [error, setError] = useState(); useEffect(() => { - fetch(url, { - mode: "no-cors", - }).then(res => { - res.text().then(text => { - parser.parseString(text).then(result => { - setParsed(result); - }).catch((e) => { - setError(e) - }) - }).catch((e) => { - setError(e) - }) - }).catch((e) => { - setError(e) - }) + const run = async () => { + try { + let content = await fetchRSSContent(url) + const result = await parser.parseString(content) + setParsed(result) + } catch (error) { + setError(error) + } + }; + run(); }, []) return (