Javascript 抛出:拒绝执行内联事件处理程序,因为它违反了以下内容安全策略指令:“script-src 'self'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15657644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:34:05  来源:igfitidea点击:

Javascript throwing : Refused to execute inline event handler because it violates the following Content Security Policy directive: "script-src 'self'

javascriptgoogle-chrome-extension

提问by Nir

I'm newbie in google chrome extention delveopment. I'm trying to develop a simple extension and i keep getting the error above.

我是谷歌浏览器扩展开发的新手。我正在尝试开发一个简单的扩展,但我不断收到上述错误。

my manifest:

我的清单:

{
  "name": "set my favourties",
  "description" : "just another super awesome plugin",
  "version" : "0.1", 
    "background": {
    "page": "backround.html"
  },

   "manifest_version": 2,
    "content_security_policy": "script-src 'self' https://www.google.com; object-src 'self'",

   "browser_action" :{
     "popup" : "popup.html",
     "default_icon" : "icon.gif"
     },

     "permissions" : ["notifications"]
 }

the html code:

html代码:

<html>
<head>
<script src = "backround.js">

</script>
</head>
<body onload = "loadHandler()">

</body>
</html>

and the js:

和js:

  function loadHandler(){
  window.webkitNotifications.createNotification("icon.gif","Plugin Loaded","it was loaded").show();

   }

thanks in advance

提前致谢

Nir

近红外

采纳答案by Quentin

If this wasn't a Chrome extension, you could add 'unsafe-inline'to the list of acceptable places to load scripts from,but you should avoid using inline event handlers at all.

如果这不是 Chrome 扩展程序,您可以添加'unsafe-inline'到可接受的加载脚本位置列表中,但是您应该完全避免使用内联事件处理程序。

Replace (in the HTML):

替换(在 HTML 中):

onload = "loadHandler()"

with (in the script):

与(在脚本中):

window.addEventListener('load', loadHandler);