diff --git a/rspack.config.ts b/rspack.config.ts index f25bb7c..57d416b 100644 --- a/rspack.config.ts +++ b/rspack.config.ts @@ -24,6 +24,7 @@ export default defineConfig({ context: __dirname, entry: { service_worker: `${src}/service_worker.ts`, + sandbox: `${src}/sandbox.ts`, popup: `${src}/pages/popup/main.tsx`, }, output: { @@ -104,6 +105,13 @@ export default defineConfig({ minify: true, chunks: ["popup"], }), + new rspack.HtmlRspackPlugin({ + filename: `${dist}/ext/src/sandbox.html`, + template: `${src}/pages/sandbox.html`, + inject: "head", + minify: true, + chunks: ["sandbox"], + }), ].filter(Boolean), optimization: { minimizer: [ diff --git a/src/manifest.json b/src/manifest.json index 916f5e9..f4e6860 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -17,5 +17,12 @@ "128": "assets/logo.png" }, "default_locale": "zh_CN", - "permissions": [] + "permissions": [ + "offscreen" + ], + "sandbox": { + "pages": [ + "sandbox.html" + ] + } } \ No newline at end of file diff --git a/src/pages/sandbox.html b/src/pages/sandbox.html new file mode 100644 index 0000000..91d493c --- /dev/null +++ b/src/pages/sandbox.html @@ -0,0 +1,10 @@ + + + + + + + Sandbox + + + diff --git a/src/sandbox.ts b/src/sandbox.ts new file mode 100644 index 0000000..d2ca7c1 --- /dev/null +++ b/src/sandbox.ts @@ -0,0 +1,3 @@ +chrome.runtime.onMessage.addListener((data) => { + console.log(data); +}); diff --git a/src/service_worker.ts b/src/service_worker.ts index e69de29..2c35d32 100644 --- a/src/service_worker.ts +++ b/src/service_worker.ts @@ -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();