【问题标题】:How to fix chrome-extension inline JavaScript invocation error?如何修复 chrome-extension 内联 JavaScript 调用错误?
【发布时间】:2016-04-14 11:49:03
【问题描述】:

我正在制作一个 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'"


}
}

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

【问题讨论】:

标签: javascript google-chrome google-chrome-extension content-security-policy


【解决方案1】:

默认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不再有效。

在 Chrome 45 之前,没有机制可以放宽对执行内联 JavaScript 的限制。特别是,设置包含“unsafe-inline”的脚本策略将不起作用。

【讨论】:

    【解决方案2】:

    我通过将所有内容外包到 JavaScript 文件中解决了这个问题。 因此,我在 JS 文件中拥有的不是 html 中的 onclick 方法:

    window.onload = function () {
        document.getElementById("button").onclick = <function>;
    }
    

    【讨论】:

      【解决方案3】:

      您可以在外部文件中使用它来代替 onclick:

      document.getElementById("#divId").addEventListener("click", myFunction);
      

      【讨论】:

        猜你喜欢
        • 2021-08-16
        • 1970-01-01
        • 2020-09-23
        • 1970-01-01
        • 2019-11-10
        • 2020-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多