我一直在使用 Vite 和 React 开发 Chrome 扩展,直到最近一切都运行良好。我没有进行任何更改,但现在我收到内容安全策略 (CSP) 错误,导致脚本无法加载。
这是我在控制台中收到的错误消息:
Refused to load the script 'chrome-extension://5216f022-cd78-49fc-abf9-9e362fc1640f/assets/content-script-preamble.js.js'
because it violates the following Content Security Policy directive:
"script-src 'self' 'wasm-unsafe-eval' 'inline-speculation-rules' http://localhost:* http://127.0.0.1:*".
Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
这是我的manifest.json:
{
"manifest_version": 3,
"name": "CRXJS React Vite Example",
"version": "1.0.0",
"action": { "default_title": "Tags New", "default_popup": "index.html" },
"permissions": ["storage", "webNavigation", "tabs", "scripting", "cookies", "activeTab"],
"background": {
"service_worker": "src/background.js"
},
"content_security_policy": {
"extension_pages": "script-src 'self' 'wasm-unsafe-eval'; object-src 'self'"
},
"content_scripts": [
{
"js": ["src/content.jsx"],
"matches": ["<all_urls>"],
"run_at": "document_idle"
}
],
"host_permissions": [
"http://localhost:3001/*",
"https://tagdots.vercel.app/*"
],
"web_accessible_resources": [
{
"resources": ["images/cursor-custom.png", "images/icon32.png"],
"matches": ["<all_urls>"]
}
],
"icons": {
"16": "images/icon32.png",
"32": "images/icon32.png",
"48": "images/icon64.png",
"128": "images/icon64.png"
}
}
错误消息似乎表明脚本 chrome-extension://5216f022-cd78-49fc-abf9-9e362fc1640f/assets/content-script-preamble.js.js 由于违反 CSP 而被阻止。但是,我最近没有对清单或代码进行任何更改。
一些额外的背景:
我的问题:
下面讨论的问题似乎是Chrome 130以来出现的问题。可以通过将@crxjs/vite-plugin升级到版本^2.0.0-beta.26来解决。
https://github.com/crxjs/chrome-extension-tools/issues/918#issuecomment-2425242487