我一直在尝试将 google api javascript 客户端库与 chrome 扩展结合起来有一段时间了,但 chrome 扩展似乎有一个可怕的临阵退缩的情况。脚本的链接是
https://apis.google.com/js/client.js
下载文件很困惑,因为该脚本实际上加载了其他脚本。我尝试将其包含在 list 中
manifest.json(摘录)
"background": {
"scripts": [
"background.js",
"https://apis.google.com/js/client.js?onload=callbackFunction"
]
},
但是扩展程序无法加载。我还尝试过将脚本注入(inject)后台 html
background.js(摘录)
var body = document.getElementsByTagName('body')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://apis.google.com/js/client.js?onload=callbackFunction";
body.appendChild(script);
但是 chrome 调试器给了我
拒绝加载脚本“https://apis.google.com/js/client.js”,因为它违反了以下内容安全策略指令:“script-src 'self' chrome-extension-resource :“。
有什么想法,或者它们注定要分开吗?
编辑:请注意,如果要使用回调函数,则必须将“?onload=myCallbackFunction”添加到脚本 URL。谢谢伊利亚。更多信息here
最佳答案
到目前为止,我找到的唯一解决方案是首先像我一样将脚本注入(inject)后台 html 页面:
background.js(摘录)
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = "https://apis.google.com/js/client.js?onload=callbackFunction";
head.appendChild(script);
然后要绕过安全警告,请编辑 list 文件 ( source ):
manifest.json(摘录)
"content_security_policy": "script-src 'self' https://apis.google.com; object-src 'self'"
但是,请注意,绕过安全性仅适用于 https
链接,而且我也发现它有点 hacky...欢迎任何其他解决方案
关于javascript - 将 Google API Javascript 客户端库加载到 Chrome 扩展中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18681803/