feat: parse uncertain rss in content script

This commit is contained in:
DIYgod
2024-02-01 18:59:51 +08:00
parent 427fa6aca7
commit 2ab1d93df7
5 changed files with 70 additions and 7 deletions
+35 -3
View File
@@ -60,22 +60,54 @@ export const getCachedRSS = (tabId) => {
return savedRSS[tabId]
}
export const setRSS = (tabId, data: {
function applyRSS(tabId, data: {
pageRSS: RSSData[],
pageRSSHub: RSSData[],
websiteRSSHub: RSSData[],
}) => {
}) {
savedRSS[tabId] = data
let text = ''
if (data.pageRSS.length || data.pageRSSHub.length) {
text = (data.pageRSS.length + data.pageRSSHub.length) + ''
text = (data.pageRSS.filter(rss => !rss.uncertain).length + data.pageRSSHub.length) + ''
} else if (data.websiteRSSHub.length) {
text = ' '
}
setBadge(text, tabId)
}
export const setRSS = async (tabId, data: {
pageRSS: RSSData[],
pageRSSHub: RSSData[],
websiteRSSHub: RSSData[],
}) => {
applyRSS(tabId, data)
const res = await sendToContentScript({
name: "parseRSS",
tabId,
body: data.pageRSS.filter(rss => rss.uncertain).map(rss => rss.url)
})
data.pageRSS = data.pageRSS.filter(rss => {
if (rss.uncertain) {
const parsed = res.find(r => r.url === rss.url)
if (parsed && parsed.title !== null) {
if (parsed.title) {
rss.title = parsed.title
}
rss.uncertain = false
return true
} else {
return false
}
} else {
return true
}
})
applyRSS(tabId, data)
}
export const deleteCachedRSS = (tabId) => {
delete savedRSS[tabId];
}
+22
View File
@@ -1,12 +1,34 @@
import { sendToBackground } from "@plasmohq/messaging"
import { fetchRSSContent, parseRSS } from "~/lib/utils"
sendToBackground({
name: "contentReady",
})
async function getRSS(url) {
let title = null
try {
const content = await fetchRSSContent(url)
const result = parseRSS(content)
if (result) {
title = result.title || ""
}
} catch (error) {}
return {
url,
title,
}
}
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
if (msg.name === "requestHTML") {
sendResponse(document.documentElement.outerHTML)
} else if (msg.name === "parseRSS") {
Promise.all(msg.body?.map(getRSS)).then((data) => {
sendResponse(data)
})
return true
}
})
+11 -3
View File
@@ -157,12 +157,20 @@ export async function getPageRSS(data: {
feed.title = result.title;
}
pageRSS.push(feed);
unique.save(feed.url)
} else {
pageRSS.push({
...feed,
uncertain: true
});
}
resolve()
} catch (error) {
resolve()
pageRSS.push({
...feed,
uncertain: true
});
}
unique.save(feed.url)
resolve()
})
}))
+1
View File
@@ -18,4 +18,5 @@ export type RSSData = {
image?: string;
path?: string;
isDocs?: boolean;
uncertain?: boolean;
}
+1 -1
View File
@@ -16,7 +16,7 @@ export function removeFunctionFields(obj) {
}
}
export function parseRSS(content: string) {
if (content.includes("<rss ")) {
if (content.includes("<rss ") || content.includes("<feed ")) {
const titleContent = content.match(/<title>(.*)<\/title>/)?.[1]
const title = titleContent ? he.decode(titleContent)?.replace(/<!\[CDATA\[(.*)]]>/, (match, p1) => p1)?.trim() : ""
return {