如何修复chrome-extension内联JavaScript调用错误?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何修复chrome-extension内联JavaScript调用错误?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2475字,纯文字阅读大概需要4分钟。
内容图文
![如何修复chrome-extension内联JavaScript调用错误?](/upload/InfoBanner/zyjiaocheng/709/655bcc3842a44c5d8b35eab5bdaf801e.jpg)
我正在进行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,不会加载内联脚本,只能加载本地脚本.您可以通过以下方式放宽默认策略:
>内联脚本.看一下Official Guide,可以通过在策略中指定源代码的base64编码哈希来将内联脚本列入白名单.有关示例,请参见Hash usage for elements.
但我相信更好的方法是将此逻辑提取到单独的脚本而不使用内联脚本.
>远程脚本.您可以通过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.
内容总结
以上是互联网集市为您收集整理的如何修复chrome-extension内联JavaScript调用错误?全部内容,希望文章能够帮你解决如何修复chrome-extension内联JavaScript调用错误?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。