首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何修复chrome-extension内联JavaScript调用错误?

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

Stack Overflow用户
提问于 2016-04-14 19:49:03
回答 2查看 46.4K关注 0票数 36

我正在做一个chrome扩展,但是当我试图触发onclick()事件时,我似乎得到了以下错误。

代码语言:javascript
复制
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:"

代码语言:javascript
复制
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:

代码语言:javascript
复制
{
  "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'"


}
}

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

EN

回答 2

Stack Overflow用户

发布于 2021-02-15 00:16:37

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

代码语言:javascript
复制
window.onload = function () {
    document.getElementById("button").onclick = <function>;
}
票数 3
EN

Stack Overflow用户

发布于 2021-04-27 16:34:18

您可以在外部文件中使用此命令,而不是onclick:

代码语言:javascript
复制
document.getElementById("#divId").addEventListener("click", myFunction);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36622181

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档