Skip to content

Commit

Permalink
feat: move getting page rss into content script
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Feb 4, 2024
1 parent 5b4e96c commit 2bd298c
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 122 deletions.
2 changes: 1 addition & 1 deletion src/background/messages/responseRSS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { PlasmoMessaging } from "@plasmohq/messaging"
import { setRSS } from "~/background/rss"

const handler: PlasmoMessaging.MessageHandler = async (req, res) => {
setRSS(req.body.tabId, req.body.rss)
setRSS(req.body.tabId || req.sender.tab.id, req.body.rss)
res.send("")
}

Expand Down
83 changes: 34 additions & 49 deletions src/background/rss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Storage } from "@plasmohq/storage"
import { setupOffscreenDocument } from "~/lib/offscreen"
import report from "~/lib/report"
import type { RSSData } from "~/lib/types"
import { getRSS as sandboxGetRSS } from "~/sandboxes"
import { getRSSHub as sandboxGetRSSHub } from "~/sandboxes"

import { setBadge } from "./badge"

Expand All @@ -16,9 +16,9 @@ const storage = new Storage({

const savedRSS: {
[tabId: number]: {
pageRSS: RSSData[]
pageRSSHub: RSSData[]
websiteRSSHub: RSSData[]
pageRSS: RSSData[]
}
} = {}

Expand All @@ -43,7 +43,7 @@ export const getRSS = async (tabId, url) => {
chrome.runtime.sendMessage({
target: "offscreen",
data: {
name: "requestRSS",
name: "requestRSSHub",
body: {
tabId,
html,
Expand All @@ -52,80 +52,65 @@ export const getRSS = async (tabId, url) => {
},
},
})

await new Promise((resolve) => setTimeout(resolve, 100))
} else {
sandboxGetRSS({
const rsshub = sandboxGetRSSHub({
html,
url,
rules: await storage.get("rules"),
callback: (rss) => setRSS(tabId, rss),
})
setRSS(tabId, rsshub)
}

const pageRSS = await sendToContentScript({
name: "requestPageRSS",
tabId,
})
setRSS(tabId, pageRSS)
})
}

export const getCachedRSS = (tabId) => {
return savedRSS[tabId]
}

function applyRSS(
export const setRSS = async (
tabId,
data: {
pageRSS: RSSData[]
} | {
pageRSSHub: RSSData[]
websiteRSSHub: RSSData[]
},
) {
savedRSS[tabId] = data
) => {
if (!data) {
return
}
if (!savedRSS[tabId]) {
savedRSS[tabId] = {
pageRSS: [],
pageRSSHub: [],
websiteRSSHub: [],
}
}
if ("pageRSS" in data) {
savedRSS[tabId].pageRSS = data.pageRSS
} else {
savedRSS[tabId].pageRSSHub = data.pageRSSHub
savedRSS[tabId].websiteRSSHub = data.websiteRSSHub
}

let text = ""
if (data.pageRSS.length || data.pageRSSHub.length) {
if (savedRSS[tabId].pageRSS.length || savedRSS[tabId].pageRSSHub.length) {
text =
data.pageRSS.filter((rss) => !rss.uncertain).length +
data.pageRSSHub.length +
savedRSS[tabId].pageRSS.length +
savedRSS[tabId].pageRSSHub.length +
""
} else if (data.websiteRSSHub.length) {
} else if (savedRSS[tabId].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 changes: 3 additions & 19 deletions src/contents/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
import { sendToBackground } from "@plasmohq/messaging"

import { fetchRSSContent, parseRSS } from "~/lib/utils"
import { getPageRSS } from "~/lib/rss"

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) => {
} else if (msg.name === "requestPageRSS") {
getPageRSS().then((data) => {
sendResponse(data)
})
return true
Expand Down
30 changes: 10 additions & 20 deletions src/lib/rss.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import type { RSSData } from "./types"
import { fetchRSSContent, parseRSS } from "./utils"

export async function getPageRSS(data: { html: string; url: string }) {
const parser = new DOMParser()
const document = parser.parseFromString(data.html, "text/html")
const location = new URL(data.url)

export async function getPageRSS() {
const defaultTitle =
document.querySelector("title") &&
document.querySelector("title").innerHTML &&
Expand All @@ -22,7 +18,7 @@ export async function getPageRSS(data: { html: string; url: string }) {

function handleUrl(url) {
return new URL(
url.replace(/^(feed:\/\/)/, "https://").replace(/^(feed:)/, ""),
url.replace(/^(feed:\/\/)/, "https://").replace(/^(feed:)/, "").replace(/^(http:\/\/)/, "https://"),
location.href,
).toString()
}
Expand Down Expand Up @@ -61,7 +57,9 @@ export async function getPageRSS(data: { html: string; url: string }) {
})

// skip the following check if this page is an RSS feed
return pageRSS
return {
pageRSS,
}
}
}

Expand Down Expand Up @@ -158,23 +156,15 @@ export async function getPageRSS(data: { html: string; url: string }) {
feed.title = result.title
}
pageRSS.push(feed)
} else {
pageRSS.push({
...feed,
uncertain: true,
})
unique.save(feed.url)
}
} catch (error) {
pageRSS.push({
...feed,
uncertain: true,
})
}
unique.save(feed.url)
} catch (error) { }
resolve()
})
}),
)

return pageRSS
return {
pageRSS,
}
}
1 change: 0 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ export type RSSData = {
image?: string
path?: string
isDocs?: boolean
uncertain?: boolean
}
49 changes: 17 additions & 32 deletions src/sandboxes/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { getPageRSS } from "~/lib/rss"
import { getPageRSSHub, getWebsiteRSSHub } from "~/lib/rsshub"
import { parseRules } from "~/lib/rules"
import { removeFunctionFields } from "~/lib/utils"

export {}

export const getRSS = async ({
export const getRSSHub = ({
html,
url,
rules,
callback,
}: {
html: string
url: string
rules: string
callback
}) => {
const pageRSSHub = getPageRSSHub({
html,
Expand All @@ -25,20 +22,10 @@ export const getRSS = async ({
url,
rules,
})
callback({
pageRSS: [],
return {
pageRSSHub,
websiteRSSHub,
})
const pageRSS = await getPageRSS({
html,
url,
})
callback({
pageRSS,
pageRSSHub,
websiteRSSHub,
})
}
}

export const getDisplayedRules = (rules: string) => {
Expand All @@ -53,7 +40,7 @@ if (typeof window !== "undefined") {
(
event: MessageEvent<
| {
name: "requestRSS"
name: "requestRSSHub"
body: {
html: string
url: string
Expand All @@ -70,25 +57,23 @@ if (typeof window !== "undefined") {
>,
) => {
switch (event.data?.name) {
case "requestRSS": {
getRSS({
case "requestRSSHub": {
const rsshub = getRSSHub({
html: event.data.body.html,
url: event.data.body.url,
rules: event.data.body.rules,
callback: (rss) => {
event.source.postMessage(
{
name: "responseRSS",
body: {
url: "url" in event.data.body && event.data.body.url,
tabId: "tabId" in event.data.body && event.data.body.tabId,
rss,
},
},
event.origin as any,
)
},
})
event.source.postMessage(
{
name: "responseRSS",
body: {
url: "url" in event.data.body && event.data.body.url,
tabId: "tabId" in event.data.body && event.data.body.tabId,
rss: rsshub,
},
},
event.origin as any,
)
break
}
case "requestDisplayedRules": {
Expand Down

0 comments on commit 2bd298c

Please sign in to comment.