沙盒页

This commit is contained in:
王一之 2024-11-01 17:01:53 +08:00
parent 29ce09a35d
commit c8a5430594
6 changed files with 28 additions and 9 deletions

View File

@ -24,6 +24,7 @@ export default defineConfig({
context: __dirname, context: __dirname,
entry: { entry: {
service_worker: `${src}/service_worker.ts`, service_worker: `${src}/service_worker.ts`,
offscreen: `${src}/offscreen.ts`,
sandbox: `${src}/sandbox.ts`, sandbox: `${src}/sandbox.ts`,
popup: `${src}/pages/popup/main.tsx`, popup: `${src}/pages/popup/main.tsx`,
}, },
@ -105,6 +106,13 @@ export default defineConfig({
minify: true, minify: true,
chunks: ["popup"], chunks: ["popup"],
}), }),
new rspack.HtmlRspackPlugin({
filename: `${dist}/ext/src/offscreen.html`,
template: `${src}/pages/offscreen.html`,
inject: "head",
minify: true,
chunks: ["offscreen"],
}),
new rspack.HtmlRspackPlugin({ new rspack.HtmlRspackPlugin({
filename: `${dist}/ext/src/sandbox.html`, filename: `${dist}/ext/src/sandbox.html`,
template: `${src}/pages/sandbox.html`, template: `${src}/pages/sandbox.html`,

View File

@ -22,7 +22,7 @@
], ],
"sandbox": { "sandbox": {
"pages": [ "pages": [
"sandbox.html" "src/sandbox.html"
] ]
} }
} }

0
src/offscreen.ts Normal file
View File

12
src/pages/offscreen.html Normal file
View File

@ -0,0 +1,12 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Offscreen</title>
</head>
<body>
<iframe src="/src/sandbox.html" name="sandbox" style="display: none"></iframe>
</body>
</html>

View File

@ -1,3 +1 @@
chrome.runtime.onMessage.addListener((data) => { eval('console.log("Hello from sandbox!");');
console.log(data);
});

View File

@ -1,12 +1,13 @@
chrome.runtime.onConnect.addListener((port) => {
console.log("service worker connected", port);
});
async function setupOffscreenDocument() { async function setupOffscreenDocument() {
// 创建运行后台脚本的沙盒环境 // 创建运行后台脚本的沙盒环境
await chrome.offscreen.createDocument({ await chrome.offscreen.createDocument({
url: "src/sandbox.html", url: "src/offscreen.html",
reasons: [chrome.offscreen.Reason.DOM_SCRAPING], reasons: [chrome.offscreen.Reason.CLIPBOARD],
justification: "background script", justification: "offscreen page",
}); });
// Send message to offscreen document // Send message to offscreen document