gpt4 book ai didi

javascript - 如何修复 chrome-extension 内联 JavaScript 调用错误?

转载 作者:行者123 更新时间:2023-12-03 01:43:10 26 4
gpt4 key购买 nike

我正在制作一个 chrome 扩展,但是当我尝试启动 onclick() 事件时,我似乎收到以下错误。

Refused to load the script 'https://apis.google.com/js/client.js?onload=handleClientLoad' because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:"

Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self' blob: filesystem: chrome-extension-resource:". Either the 'unsafe-inline' keyword, a hash ('sha256-...'), or a nonce ('nonce-...') is required to enable inline execution.

这是我的manifest.json:

{
"manifest_version": 2,

"name": "SECURE",
"description": "this extension offers secure communication for GMAIL users",
"version": "1.0",

"browser_action": {
"default_icon": "resources/icon16.png",
"default_popup": "popup.html",
"default_title": "Click here!"


},

"background":{
"scripts":["background.js"]
},

"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js":["myscript.js"],
"run_at": "document_end"
}
],
"permissions": ["identity", "https://accounts.google.com/*", "https://www.googleapis.com/*"],

"oauth2": {
"client_id": "975410329966.apps.googleusercontent.com",
"scopes": [
"<all urls>",
"https://www.googleapis.com/auth/drive",
"https://mail.google.com/",
"https://www.googleapis.com/auth/gmail.login",
"https://www.googleapis.com/auth/gmail.compose",
"https://www.googleapis.com/auth/gmail.readonly",
"https://www.googleapis.com/auth/gmail.send"
],

"content_security_policy":"script-src 'self' 'unsafe-inline' 'unsafe eval' https://apis.google.com/js/client.js?; object-src 'self'"


}
}

任何有助于修复此错误的帮助将不胜感激。

最佳答案

默认Content Security Policy ,内联脚本不会被加载,只能加载本地脚本。您可以通过以下方式放宽默认策略:

  1. 内联脚本。看看Official Guide ,可以通过在策略中指定源代码的 Base64 编码哈希来将内联脚本列入白名单。请参阅Hash usage for elements举个例子。

    但我相信更好的方法是将这个逻辑提取到一个单独的脚本中,而不是使用内联脚本。

  2. 远程脚本。您可以通过 manifest.json

    中的以下部分将脚本资源列入白名单 https://apis.google.com/js/client.js?onload=handleClientLoad
    "content_security_policy":"script-src 'self' https://apis.google.com; object-src 'self'"

    此外,我相信更好的方法是下载远程 client.js 并将其作为本地脚本包含在内。

请注意Inline Script的描述,unsafe-inline 不再有效。

Up until Chrome 45, there was no mechanism for relaxing the restriction against executing inline JavaScript. In particular, setting a script policy that includes 'unsafe-inline' will have no effect.

关于javascript - 如何修复 chrome-extension 内联 JavaScript 调用错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36622181/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com