将 Google API Javascript Client Library 加载到 Chrome 扩展中
Loading Google API Javascript Client Library into Chrome Extension
我一直在尝试将Google api javascript客户端库与chrome扩展相结合一段时间了,但似乎chrome扩展有一个可怕的冷脚情况。脚本的链接是
https://apis.google.com/js/client.js
下载文件很混乱,因为脚本实际上加载了其他脚本。我试过将其包含在清单中
manifest.json(摘录)
"background": {
"scripts": [
"background.js",
"https://apis.google.com/js/client.js?onload=callbackFunction"
]
},
但随后扩展不会加载。我还尝试将脚本注入后台 html
背景.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调试器给了我
Refused to load the script 'https://apis.google.com/js/client.js' because it violates the following Content Security Policy directive: "script-src 'self' chrome-extension-resource:".
任何想法,还是它们注定要分开?
编辑:请注意,如果要使用回调函数,则必须在脚本 url 中添加"?onload=myCallbackFunction"。谢谢伊利亚。更多信息在这里
到目前为止,我找到的唯一解决方案是首先将脚本注入后台 html 页面,就像我所做的那样:
背景.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);
然后,要绕过安全警告,请编辑清单文件(源):
manifest.json(摘录)
"content_security_policy": "script-src 'self' https://apis.google.com; object-src 'self'"
但是,请注意,绕过安全性仅适用于https
链接,而且我也发现它有点黑客......欢迎任何其他解决方案
我在https://apis.google.com/js/client.js
的源代码中发现了一些有趣的东西。上面写着:
gapi.load("client",{callback:window["gapi_onload"], ......
在网页中加载client.js
后,将立即调用gapi.load
。似乎加载window.gapi_onload
后gapi.client
将被调用为回调。
作为概念验证,我构建了这个 plunker:http://plnkr.co/edit/TGvzI9SMKwGM6KnFSs7U
gapi.auth
和gapi.client
都已成功打印到控制台。
回到 Chrome 扩展程序。
我把它放在我mainfest.json
的背景部分:
"background": {
"scripts": [
"background.js",
"gapi-client.js"
]
}
其中background.js
是我的扩展中的主要背景脚本。gapi-client.js
的所有内容都是直接从https://apis.google.com/js/client.js
复制粘贴的。
background.js
里面写着:
window.gapi_onload = function(){
console.log('gapi loaded.', gapi.auth, gapi.client);
// Do things you want with gapi.auth and gapi.client.
}
请注意,background.js
在gapi-client.js
之前加载。由于gapi-client.js
在加载后立即读取window["gapi_onload"]
,因此必须在此之前指定window.gapi_onload
。
因此,window.gapi_onload
将按预期调用,同时填充gapi.auth
和gapi.client
。
在我的解决方案中,我没有自己创建background.html
。我也没有修改内容安全策略。但是,请注意,该解决方案相当未记录,因此将来可能会发生变化。
您可以通过背景加载它们.html这会加载您的背景.js。
<html>
<head>
<title></title>
<script src="background.js"></script>
</head>
<body>
</body>
<script src="https://apis.google.com/js/client.js"></script>
</html>
使用 manifest.json:
"background":
{
"page": "background.html"
},
"content_security_policy": "script-src 'self' https://apis.google.com; object-src 'self'",
你只需要为这个库设置 onload 方法
https://apis.google.com/js/client.js?onload=handleClientLoad
和 handleClientLoad - 默认您的注册方法。
js oauth 的示例
我试图将清单文件添加为 woojoo666 的建议,但仍然失败,看起来我们需要再添加一个标志"不安全的":
"content_security_policy":"脚本-src'自我''不安全-eval'https://apis.google.com;对象-src 'self'",
- chrome扩展中的navigator.geolocation.getCurrentPosition
- chrome扩展:尽管运行了at:documentidle,js脚本还是过早启动
- chrome扩展更改主机/域警告
- 如何在chrome扩展中重定向到html页面
- Chrome扩展没有't在重新加载之前考虑期权价值
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 如何在chrome扩展中存储数据/结果,以及如何使用setTimeout使其只被调用一次
- 从选项页面更新chrome扩展清单权限
- 如何将chrome扩展功能移植到移动设备(特别是jquery和trello)
- Chrome扩展:遍历不同的页面并收集数据
- 未捕获的类型错误:无法读取属性'addEventListener'的null chrome扩展名
- 可以从Chrome扩展修改窗口对象吗
- 从popup.js|Chrome扩展访问DOM
- 从Chrome扩展名中的popup.html文件在background.js文件中运行一个函数
- 在Chrome扩展内部输出Google API调用
- 检查是否存在使用chrome扩展的javascript库
- 从Chrome扩展访问Google Cloud SQL数据库
- Chrome扩展,Chrome.tabs.query的结果未定义
- 为tweet构建chrome扩展
- 在chrome扩展中使用AJAX获取目录中的文件数