feat: messages

This commit is contained in:
DIYgod 2023-12-10 22:56:28 +08:00
parent a715f0d465
commit 64918e370b
No known key found for this signature in database
9 changed files with 165 additions and 47 deletions

View File

@ -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);
// }
// });

View File

@ -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

View File

@ -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
}

View File

@ -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"
})

View File

@ -0,0 +1,12 @@
import { useMessage } from "@plasmohq/messaging/hook"
const RequestHTML = () => {
useMessage<string, string>(async (req, res) => {
if (req.name === "requestHTML") {
res.send(document.documentElement.outerHTML)
}
})
return <></>
}
export default RequestHTML

View File

@ -1,25 +0,0 @@
import { useState } from "react"
import "./style.css"
function IndexNewtab() {
const [data, setData] = useState("")
return (
<div
className="new-tab"
style={{
padding: 16,
display: "flex",
flexDirection: "column"
}}>
<h1>
Welcome to your <a href="https://www.plasmo.com">Plasmo</a> Extension!
</h1>
<input onChange={(e) => setData(e.target.value)} value={data} />
<footer>Crafted by @PlamoHQ</footer>
</div>
)
}
export default IndexNewtab

View File

@ -6,7 +6,7 @@ function IndexPopup() {
return (
<div className="min-w-[350px] p-5">
<a className="absolute right-4" href="/options.html">
<a className="absolute right-4" href="/options.html" target="_blank">
<i className="icon-[mingcute--settings-3-fill] w-5 h-5 text-slate-600 hover:text-black transition-colors"></i>
</a>
<div className="space-y-4">

View File

@ -1,3 +1,13 @@
export { }
export {}
console.log("HELLO WORLD FROM SANDBOXES")
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)
}
})

View File

@ -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<HTMLIFrameElement>(null);
chrome.runtime.onMessage.addListener((msg) => {
iframeRef.current?.contentWindow?.postMessage(msg.data, "*");
})
return (
<iframe id="sandbox" src="/sandboxes/index.html"></iframe>
<iframe id="sandbox" src="/sandboxes/index.html" ref={iframeRef}></iframe>
);
}