Chrome 扩展程序:由于内容安全策略 (CSP) 在最近没有更改后拒绝加载脚本

问题描述 投票:0回答:1

我一直在使用 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.

enter image description here

这是我的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 而被阻止。但是,我最近没有对清单或代码进行任何更改。

一些额外的背景:

  • 该扩展使用了Vite和React。
  • 错误开始突然出现,而扩展之前工作正常。
  • 除了扩展中的脚本之外,我没有显式加载任何外部脚本。
  • 我的 CSP 目前允许在 script-src 下使用“self”和“wasm-unsafe-eval”。
  • 我尝试对此进行调试,但似乎无法查明可能导致此问题的原因。

我的问题:

  • 如果我的扩展中没有任何更改,可能会导致此错误的原因是什么?
  • 如何解决 CSP 问题以允许脚本正确加载?
  • 任何帮助或指示将不胜感激。谢谢!
google-chrome google-chrome-extension manifest chrome-extension-manifest-v3
1个回答
0
投票

下面讨论的问题似乎是Chrome 130以来出现的问题。可以通过将@crxjs/vite-plugin升级到版本^2.0.0-beta.26来解决。

https://github.com/crxjs/chrome-extension-tools/issues/918#issuecomment-2425242487

© www.soinside.com 2019 - 2024. All rights reserved.