This commit is contained in:
王一之 2024-10-31 21:37:55 +08:00
parent 78b4b31d46
commit 29ce09a35d
5 changed files with 49 additions and 1 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`,
sandbox: `${src}/sandbox.ts`,
popup: `${src}/pages/popup/main.tsx`, popup: `${src}/pages/popup/main.tsx`,
}, },
output: { output: {
@ -104,6 +105,13 @@ export default defineConfig({
minify: true, minify: true,
chunks: ["popup"], chunks: ["popup"],
}), }),
new rspack.HtmlRspackPlugin({
filename: `${dist}/ext/src/sandbox.html`,
template: `${src}/pages/sandbox.html`,
inject: "head",
minify: true,
chunks: ["sandbox"],
}),
].filter(Boolean), ].filter(Boolean),
optimization: { optimization: {
minimizer: [ minimizer: [

View File

@ -17,5 +17,12 @@
"128": "assets/logo.png" "128": "assets/logo.png"
}, },
"default_locale": "zh_CN", "default_locale": "zh_CN",
"permissions": [] "permissions": [
"offscreen"
],
"sandbox": {
"pages": [
"sandbox.html"
]
}
} }

10
src/pages/sandbox.html Normal file
View File

@ -0,0 +1,10 @@
<!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>Sandbox</title>
</head>
<body></body>
</html>

3
src/sandbox.ts Normal file
View File

@ -0,0 +1,3 @@
chrome.runtime.onMessage.addListener((data) => {
console.log(data);
});

View File

@ -0,0 +1,20 @@
async function setupOffscreenDocument() {
// 创建运行后台脚本的沙盒环境
await chrome.offscreen.createDocument({
url: "src/sandbox.html",
reasons: [chrome.offscreen.Reason.DOM_SCRAPING],
justification: "background script",
});
// Send message to offscreen document
chrome.runtime.sendMessage({
type: "init",
target: "offscreen",
data: { init: "1" },
});
}
setupOffscreenDocument();