Skip to content

Commit

Permalink
feat: add open-popup and get-rss
Browse files Browse the repository at this point in the history
  • Loading branch information
DIYgod committed Feb 1, 2024
1 parent 2ab1d93 commit 53a6796
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 58 deletions.
135 changes: 77 additions & 58 deletions src/background/rss.ts
Original file line number Diff line number Diff line change
@@ -1,96 +1,115 @@
import AsyncLock from "async-lock"

import { sendToContentScript } from "@plasmohq/messaging"
import { setupOffscreenDocument } from "~/lib/offscreen"
import type { RSSData } from "~/lib/types";
import { Storage } from "@plasmohq/storage"
import AsyncLock from "async-lock"
import { setBadge } from "./badge"

import { setupOffscreenDocument } from "~/lib/offscreen"
import type { RSSData } from "~/lib/types"
import { getRSS as sandboxGetRSS } from "~/sandboxes"
import report from "~/lib/report"

import { setBadge } from "./badge"

const storage = new Storage({
area: "local"
})

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

const lock = new AsyncLock();
const lock = new AsyncLock()
export const getRSS = async (tabId, url) => {
if (savedRSS[tabId]) {
setRSS(tabId, savedRSS[tabId])
return
} else {
await lock.acquire(tabId, async () => {
const html = await sendToContentScript({
name: "requestHTML",
tabId,
})
await lock.acquire(tabId, async () => {
if (savedRSS[tabId]) {
setRSS(tabId, savedRSS[tabId])
return
}

if (chrome.offscreen) {
await setupOffscreenDocument("tabs/offscreen.html")
chrome.runtime.sendMessage({
target: "offscreen",
data: {
name: "requestRSS",
body: {
tabId,
html,
url,
rules: await storage.get("rules"),
}
}
})
} else {
sandboxGetRSS({
html,
url,
rules: await storage.get("rules"),
callback: (rss) => setRSS(tabId, rss)
})
}
report({
url,
name: "get-rss"
})
const html = await sendToContentScript({
name: "requestHTML",
tabId
})
}

if (chrome.offscreen) {
await setupOffscreenDocument("tabs/offscreen.html")
chrome.runtime.sendMessage({
target: "offscreen",
data: {
name: "requestRSS",
body: {
tabId,
html,
url,
rules: await storage.get("rules")
}
}
})

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

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

function applyRSS(tabId, data: {
pageRSS: RSSData[],
pageRSSHub: RSSData[],
websiteRSSHub: RSSData[],
}) {
function applyRSS(
tabId,
data: {
pageRSS: RSSData[]
pageRSSHub: RSSData[]
websiteRSSHub: RSSData[]
}
) {
savedRSS[tabId] = data

let text = ''
let text = ""
if (data.pageRSS.length || data.pageRSSHub.length) {
text = (data.pageRSS.filter(rss => !rss.uncertain).length + data.pageRSSHub.length) + ''
text =
data.pageRSS.filter((rss) => !rss.uncertain).length +
data.pageRSSHub.length +
""
} else if (data.websiteRSSHub.length) {
text = ' '
text = " "
}
setBadge(text, tabId)
}

export const setRSS = async (tabId, data: {
pageRSS: RSSData[],
pageRSSHub: RSSData[],
websiteRSSHub: RSSData[],
}) => {
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)
body: data.pageRSS.filter((rss) => rss.uncertain).map((rss) => rss.url)
})
data.pageRSS = data.pageRSS.filter(rss => {
data.pageRSS = data.pageRSS.filter((rss) => {
if (rss.uncertain) {
const parsed = res.find(r => r.url === rss.url)
const parsed = res.find((r) => r.url === rss.url)
if (parsed && parsed.title !== null) {
if (parsed.title) {
rss.title = parsed.title
Expand All @@ -104,10 +123,10 @@ export const setRSS = async (tabId, data: {
return true
}
})

applyRSS(tabId, data)
}

export const deleteCachedRSS = (tabId) => {
delete savedRSS[tabId];
delete savedRSS[tabId]
}
33 changes: 33 additions & 0 deletions src/lib/report.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
function report({
url,
name,
}) {
if (process.env.PLASMO_PUBLIC_UMAMI_ID && process.env.PLASMO_PUBLIC_UMAMI_URL) {
let hostname = ""
try {
hostname = new URL(url).hostname
} catch (error) {}

const umamiData = {
payload: {
hostname,
language: chrome?.i18n?.getUILanguage(),
referrer: hostname,
url: hostname,
website: process.env.PLASMO_PUBLIC_UMAMI_ID,
name: name
},
type: "event"
}

fetch(`${process.env.PLASMO_PUBLIC_UMAMI_URL}/api/send`, {
method: "POST",
headers: {
"content-type": "application/json",
},
body: JSON.stringify(umamiData)
})
}
}

export default report
10 changes: 10 additions & 0 deletions src/popup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useState } from "react"
import "~/lib/style.css"
import { sendToBackground } from "@plasmohq/messaging"
import RSSList from "./RSSList"
import report from "~/lib/report"

function IndexPopup() {
const [data, setData] = useState({
Expand All @@ -19,6 +20,15 @@ function IndexPopup() {
websiteRSSHub: [],
}, res))
})

chrome.tabs.query({
active: true,
}, ([tab]) => {
report({
url: tab.url,
name: "open-popup"
})
});
}, [])

return (
Expand Down

0 comments on commit 53a6796

Please sign in to comment.