指令“script-src”中不安全的 CSP 值“'unsafe-eval'”

Syn*_*ker 11 javascript firefox google-chrome google-chrome-extension

当尝试将第三方js文件加载到chrome扩展中的内容脚本中时。我遇到一个unsafe-eval错误

我的manifest.json看起来像这样

{
    "manifest_version": 3,
    "name": "Test",
    "version": "1.0",
    "host_permissions": ["https://mail.google.com/"],
    "content_scripts": [
        {
            "matches": [
                "https://mail.google.com/*"
            ],
            "js": ["3rdparty.js", "code.js"],
            "run_at": "document_end"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

加载后我收到此错误

记录错误:EvalError:拒绝将字符串评估为 JavaScript,因为“unsafe-eval”不是以下内容安全策略指令中允许的脚本源:“script-src 'self'”。

所以,我尝试将 csp 添加到清单文件中

{
...,
"content_security_policy": {
    "extension_pages": "script-src 'self' 'unsafe-eval'; object-src 'self'"
  }
}
Run Code Online (Sandbox Code Playgroud)

然后我在 chrome 中收到此错误,告知其无法加载扩展程序

'content_security_policy.extension_pages':指令 'script-src' 中不安全的 CSP 值“'unsafe-eval'”。

Ahm*_*eri 6

恐怕你不能在清单版本 3 中使用'unsafe-eval'

您正在执行远程代码或任意字符串吗?您无法再使用 chrome.scripting.executeScript({code: '...'})、eval() 和 new Function() 执行外部逻辑。

您必须将所有脚本移至远程或本地文件中。您可以在远程文件(例如 php)上生成脚本并使用chrome.scripting.executeScript而不是使用执行它eval(),或者考虑迁移回 MV2。