Skip to content

Commit

Permalink
feat: fetch rss content without no-cors
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Dec 30, 2023
1 parent 4c16440 commit b99358b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
6 changes: 2 additions & 4 deletions src/lib/rss.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -150,9 +150,7 @@ export async function getPageRSS(data: {
await Promise.all(uncertain.map((feed) => {
return new Promise<void>(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) {
Expand Down
10 changes: 10 additions & 0 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 11 additions & 15 deletions src/tabs/preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
CardTitle,
} from "~/lib/components/Card"
import RSSItem from "~/popup/RSSItem";
import { fetchRSSContent } from "~/lib/utils"

const parser = new Parser();

Expand All @@ -24,21 +25,16 @@ function PreviewPage() {
const [error, setError] = useState<Event>();

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 (
Expand Down

0 comments on commit b99358b

Please sign in to comment.