【问题标题】:Why am I getting "Failed to load extension. Invalid value for 'content_security_policy'"?为什么我会收到“无法加载扩展程序。'content_security_policy' 的值无效”?
【发布时间】:2021-04-16 19:01:37
【问题描述】:

我正在尝试创建一个 chrome 扩展。我的清单文件是

{
    "name": "Alert-Beep",
    "action": {},
    "manifest_version": 3,
    "version": "0.1",
    "description": "Beeps if alert() is called",
    "content_security_policy": "script-src 'self'; object-src 'self'",
    "permissions": [
    "activeTab",
    "scripting"
    ],
    "content_scripts": [
    {
        "matches": ["https://*.com/*"],
        "js": ["alert-beep.js"],
        "run_at": "document_start"
    }
    ]
}

加载扩展失败并显示消息

Failed to load extension
File
~\alert-beep
Error
Invalid value for 'content_security_policy'.
Could not load manifest.

我做错了什么?

【问题讨论】:

标签: javascript google-chrome-extension manifest.json


【解决方案1】:

另一个帮助我的例子:

清单 v3

"content_security_policy": {
    "extension_pages": "script-src 'self'; object-src 'self'; script-src-elem 'self' 'unsafe-inline' https://music.yandex.ru/;"
}

【讨论】:

    【解决方案2】:

    我已经研究了 chrome 扩展的 CSP(转至 here 以了解有关 manifest v3 的更多信息)。

    使用

    "content_security_policy": {
      "extension_pages": "script-src 'self'; object-src 'self'"
    }
    

    【讨论】:

      【解决方案3】:

      由于清单现已更新到 V3,因此 content_security_policy 是一个字典。在这种情况下,您应该更改此行

      "content_security_policy": "script-src 'self'; object-src 'self'",
      

      到这里

      "content_security_policy": {
          "script-src": 'self',
          "object-src": 'self'
       }
      

      在此处阅读更多信息:https://developer.chrome.com/docs/extensions/mv3/intro/mv3-migration/#content-security-policy

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-30
        • 2017-01-16
        • 2020-09-22
        • 2011-11-17
        • 1970-01-01
        • 1970-01-01
        • 2016-08-08
        • 1970-01-01
        相关资源
        最近更新 更多