diff --git a/src/background/index.ts b/src/background/index.ts index 4d06a11..189f1b2 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -1,8 +1,82 @@ -export { } +import { sendToContentScript } from "@plasmohq/messaging" +import { setupOffscreenDocument } from "./offscreen" + +export {} console.log("HELLO WORLD FROM BGSCRIPTS") -chrome.offscreen.createDocument({ - url: chrome.runtime.getURL("tabs/offscreen.html"), - reasons: [chrome.offscreen.Reason.IFRAME_SCRIPTING], - justification: 'Get RSS in the sandbox for enhanced security.', -}); +const getRSS = async (tabId, url) => { + console.debug("Get RSS", tabId, url) + await setupOffscreenDocument("tabs/offscreen.html") + + console.debug("Send to content script requestHTML") + const html = await sendToContentScript({ + name: "requestHTML" + }) + + console.debug("Get html", html) + console.debug("Send to offscreen") + chrome.runtime.sendMessage({ + target: "offscreen", + data: { + name: "requestRSS", + html, + url, + } + }) +} + +chrome.tabs.onActivated.addListener((tab) => { + console.debug("Tab activated", tab) + chrome.tabs.get(tab.tabId, (info) => { + if (info.url) { + getRSS(tab.tabId, info.url); + } + }); +}) + +chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { + if (changeInfo.url && tab.active) { + getRSS(tabId, changeInfo.url); + } +}) + +// chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { +// if (sender.tab && sender.tab.active) { +// if (msg.text === 'setPageRSS') { +// handleRSS(msg.feeds, sender.tab.id); +// } else if (msg.text === 'addPageRSS') { +// addPageRSS(msg.feed, sender.tab.id); +// } +// } +// if (msg.text === 'getAllRSS') { +// sendResponse(getAllRSS(msg.tabId)); +// } +// }); + +// chrome.tabs.onRemoved.addListener((tabId) => { +// removeRSS(tabId); +// }); + +// getConfig((config) => { +// if (!config.version) { +// config.version = VERSION; +// saveConfig(config); +// } else if (config.version !== VERSION) { +// chrome.notifications.create('RSSHubRadarUpdate', { +// type: 'basic', +// iconUrl: './rsshub.png', +// title: '🎉 RSSHub Radar 更新', +// message: `v${VERSION},点击查看更新日志`, +// }); +// chrome.notifications.onClicked.addListener((id) => { +// if (id === 'RSSHubRadarUpdate') { +// chrome.tabs.create({ +// url: 'https://github.com/DIYgod/RSSHub-Radar/releases', +// }); +// chrome.notifications.clear('RSSHubRadarUpdate'); +// } +// }); +// config.version = VERSION; +// saveConfig(config); +// } +// }); diff --git a/src/background/messages/responseRSS.ts b/src/background/messages/responseRSS.ts new file mode 100644 index 0000000..fc5d458 --- /dev/null +++ b/src/background/messages/responseRSS.ts @@ -0,0 +1,7 @@ +import type { PlasmoMessaging } from "@plasmohq/messaging" + +const handler: PlasmoMessaging.MessageHandler = async (req, res) => { + console.log("response RSS", req) +} + +export default handler \ No newline at end of file diff --git a/src/background/offscreen.ts b/src/background/offscreen.ts new file mode 100644 index 0000000..820689d --- /dev/null +++ b/src/background/offscreen.ts @@ -0,0 +1,30 @@ +// https://developer.chrome.com/docs/extensions/reference/api/offscreen +let creating +async function setupOffscreenDocument(path) { + const offscreenUrl = chrome.runtime.getURL(path) + // @ts-ignore + const existingContexts = await chrome.runtime.getContexts({ + contextTypes: ["OFFSCREEN_DOCUMENT"], + documentUrls: [offscreenUrl] + }) + + if (existingContexts.length > 0) { + return + } + + if (creating) { + await creating + } else { + creating = chrome.offscreen.createDocument({ + url: chrome.runtime.getURL("tabs/offscreen.html"), + reasons: [chrome.offscreen.Reason.IFRAME_SCRIPTING], + justification: "Get RSS in the sandbox for enhanced security." + }) + await creating + creating = null + } +} + +export { + setupOffscreenDocument +} \ No newline at end of file diff --git a/src/contents/index.tsx b/src/contents/index.tsx deleted file mode 100644 index ed9302f..0000000 --- a/src/contents/index.tsx +++ /dev/null @@ -1,11 +0,0 @@ -import type { PlasmoCSConfig } from "plasmo" - -export const config: PlasmoCSConfig = { - matches: ["https://docs.plasmo.com/*"] -} - -window.addEventListener("load", () => { - console.log("content script loaded") - - document.body.style.background = "pink" -}) diff --git a/src/contents/requestHTML.tsx b/src/contents/requestHTML.tsx new file mode 100644 index 0000000..f301df6 --- /dev/null +++ b/src/contents/requestHTML.tsx @@ -0,0 +1,12 @@ +import { useMessage } from "@plasmohq/messaging/hook" + +const RequestHTML = () => { + useMessage(async (req, res) => { + if (req.name === "requestHTML") { + res.send(document.documentElement.outerHTML) + } + }) + return <> +} + +export default RequestHTML \ No newline at end of file diff --git a/src/newtab.tsx b/src/newtab.tsx deleted file mode 100644 index ab15a48..0000000 --- a/src/newtab.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import { useState } from "react" - -import "./style.css" - -function IndexNewtab() { - const [data, setData] = useState("") - - return ( -
-

- Welcome to your Plasmo Extension! -

- setData(e.target.value)} value={data} /> -
Crafted by @PlamoHQ
-
- ) -} - -export default IndexNewtab diff --git a/src/popup.tsx b/src/popup.tsx index 846c382..e549bb0 100644 --- a/src/popup.tsx +++ b/src/popup.tsx @@ -6,7 +6,7 @@ function IndexPopup() { return (
- +
diff --git a/src/sandboxes/index.ts b/src/sandboxes/index.ts index 4c61490..03df320 100644 --- a/src/sandboxes/index.ts +++ b/src/sandboxes/index.ts @@ -1,3 +1,13 @@ -export { } +export {} -console.log("HELLO WORLD FROM SANDBOXES") \ No newline at end of file +console.log("HELLO WORLD FROM SANDBOXES") + +window.addEventListener("message", (event) => { + if (event.data?.name === "requestRSS") { + event.source.postMessage({ + name: "responseRSS", + url: event.data.url, + result: ["1"], + }, event.origin as any) + } +}) diff --git a/src/tabs/offscreen.tsx b/src/tabs/offscreen.tsx index 4d05de6..134fdfc 100644 --- a/src/tabs/offscreen.tsx +++ b/src/tabs/offscreen.tsx @@ -1,8 +1,29 @@ +import { useRef } from "react"; +import { sendToBackground } from "@plasmohq/messaging" + +console.log("HELLO WORLD FROM OFFSCREEN") + +window.addEventListener('message', (event) => { + if (event.data?.name === "responseRSS") { + chrome.runtime.sendMessage(event.data) + sendToBackground({ + name: "responseRSS", + body: { + data: 1 + } + }) + } +}); + function OffscreenPage() { - console.log("HELLO WORLD FROM OFFSCREEN") + const iframeRef = useRef(null); + + chrome.runtime.onMessage.addListener((msg) => { + iframeRef.current?.contentWindow?.postMessage(msg.data, "*"); + }) return ( - + ); }