mirror of
https://github.com/DIYgod/RSSHub-Radar.git
synced 2026-11-29 03:17:02 +08:00
fix: restore RSS detection after background suspension
This commit is contained in:
1 parent
5e7b5748e7
commit
4ad953e186
4 files changed
+93
-15
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { getCachedRSS } from "~/background/rss"
|
||||
import { getCachedRSS, getRSS } from "~/background/rss"
|
||||
|
||||
const handler = (
|
||||
_message?: unknown,
|
||||
@@ -10,8 +10,23 @@ const handler = (
|
||||
active: true,
|
||||
lastFocusedWindow: true,
|
||||
},
|
||||
([tab]) => {
|
||||
resolve(getCachedRSS(tab.id))
|
||||
async ([tab]) => {
|
||||
if (!tab?.id || !tab.url) {
|
||||
resolve(undefined)
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
let cachedRSS = getCachedRSS(tab.id)
|
||||
if (!cachedRSS) {
|
||||
await getRSS(tab.id, tab.url)
|
||||
cachedRSS = getCachedRSS(tab.id)
|
||||
}
|
||||
resolve(cachedRSS)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
resolve(undefined)
|
||||
}
|
||||
},
|
||||
)
|
||||
})
|
||||
|
||||
+68
-9
@@ -5,6 +5,7 @@ import { setupOffscreenDocument } from "~/lib/offscreen"
|
||||
import report from "~/lib/report"
|
||||
import { getLocalStorage } from "~/lib/storage"
|
||||
import type { RSSData } from "~/lib/types"
|
||||
import { parseRSS } from "~/lib/utils"
|
||||
import { getRSSHub as sandboxGetRSSHub } from "~/tabs/sandboxes"
|
||||
|
||||
import { setBadge } from "./badge"
|
||||
@@ -20,6 +21,49 @@ const savedRSS: {
|
||||
const lock = new AsyncLock({
|
||||
maxExecutionTime: 3000,
|
||||
})
|
||||
|
||||
const requestContentScript = async <T>(name: string, tabId: number) => {
|
||||
try {
|
||||
return await sendToContentScript<T>({
|
||||
name,
|
||||
tabId,
|
||||
})
|
||||
} catch (error) {
|
||||
// Content scripts are not injected into every tab (for example, existing
|
||||
// tabs after an extension reload). contentReady retries once they are.
|
||||
if (
|
||||
error instanceof Error &&
|
||||
error.message.includes("Receiving end does not exist")
|
||||
) {
|
||||
return
|
||||
}
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
||||
const getDirectFeed = async (url: string) => {
|
||||
try {
|
||||
const content = await (await fetch(url)).text()
|
||||
const prefix = content.slice(0, 4096)
|
||||
if (!/^\s*(?:<\?xml[^>]*>\s*)?<(?:rss|feed)\b/i.test(prefix)) {
|
||||
return
|
||||
}
|
||||
|
||||
const parsed = parseRSS(content)
|
||||
if (!parsed) {
|
||||
return
|
||||
}
|
||||
|
||||
return {
|
||||
url,
|
||||
title: parsed.title,
|
||||
image: `https://icons.duckduckgo.com/ip3/${new URL(url).hostname}.ico`,
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to inspect direct feed", error)
|
||||
}
|
||||
}
|
||||
|
||||
export const getRSS = async (tabId, url) => {
|
||||
if (!tabId || !url || !url.startsWith("http")) {
|
||||
return
|
||||
@@ -34,14 +78,20 @@ export const getRSS = async (tabId, url) => {
|
||||
report({
|
||||
url,
|
||||
})
|
||||
const html = await sendToContentScript({
|
||||
name: "requestHTML",
|
||||
tabId,
|
||||
})
|
||||
const html = await requestContentScript<string>("requestHTML", tabId)
|
||||
if (typeof html !== "string") {
|
||||
const directFeed = await getDirectFeed(url)
|
||||
if (directFeed) {
|
||||
setRSS(tabId, {
|
||||
pageRSS: [directFeed],
|
||||
})
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (chrome.offscreen && (chrome.runtime as any).getContexts) {
|
||||
await setupOffscreenDocument("offscreen.html")
|
||||
chrome.runtime.sendMessage({
|
||||
await chrome.runtime.sendMessage({
|
||||
target: "offscreen",
|
||||
data: {
|
||||
name: "requestRSSHub",
|
||||
@@ -62,11 +112,20 @@ export const getRSS = async (tabId, url) => {
|
||||
setRSS(tabId, rsshub)
|
||||
}
|
||||
|
||||
const pageRSS = await sendToContentScript({
|
||||
name: "requestPageRSS",
|
||||
const pageRSS = await requestContentScript<{ pageRSS: RSSData[] }>(
|
||||
"requestPageRSS",
|
||||
tabId,
|
||||
})
|
||||
setRSS(tabId, pageRSS)
|
||||
)
|
||||
if (pageRSS?.pageRSS.length) {
|
||||
setRSS(tabId, pageRSS)
|
||||
} else {
|
||||
const directFeed = await getDirectFeed(url)
|
||||
if (directFeed) {
|
||||
setRSS(tabId, {
|
||||
pageRSS: [directFeed],
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
|
||||
@@ -3,6 +3,8 @@ import { getPageRSS } from "~/lib/rss"
|
||||
|
||||
sendToBackground({
|
||||
name: "contentReady",
|
||||
}).catch((error) => {
|
||||
console.error("Failed to notify background that content is ready", error)
|
||||
})
|
||||
|
||||
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
|
||||
|
||||
+5
-3
@@ -17,11 +17,13 @@ export function removeFunctionFields(obj) {
|
||||
}
|
||||
export function parseRSS(content: string) {
|
||||
content = content.replaceAll("<", "<").replaceAll(">", ">")
|
||||
if (content.includes("<rss ") || content.includes("<feed ")) {
|
||||
const titleContent = content.match(/<title>(.*)<\/title>/)?.[1]
|
||||
if (/<rss(?:\s|>)/i.test(content) || /<feed(?:\s|>)/i.test(content)) {
|
||||
const titleContent = content.match(
|
||||
/<title(?:\s[^>]*)?>([\s\S]*?)<\/title>/i,
|
||||
)?.[1]
|
||||
const title = titleContent
|
||||
? decode(titleContent)
|
||||
?.replace(/<!\[CDATA\[(.*)]]>/, (match, p1) => p1)
|
||||
?.replace(/<!\[CDATA\[([\s\S]*?)]]>/g, (match, p1) => p1)
|
||||
?.trim()
|
||||
: ""
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user