将 Google API Javascript 客户端库加载到 Chrome 扩展程序中 [英] Loading Google API Javascript Client Library into Chrome Extension

查看:25
本文介绍了将 Google API Javascript 客户端库加载到 Chrome 扩展程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一段时间以来,我一直试图将 google api javascript 客户端库与 chrome 扩展程序结合起来,但似乎 chrome 扩展程序有一个可怕的冷脚案例.脚本的链接是

I've been trying to wed the google api javascript client library with a chrome extension for a while now, but it seems the chrome extension has a terrible case of cold feet. The link to the script is

https://apis.google.com/js/client.js

下载文件很麻烦,因为脚本实际上加载了其他脚本.我试过将它包含在清单中

Downloading the files is messy because the script actually loads other scripts. I've tried including it in the manifest

ma​​nifest.json(摘录)

"background": {
  "scripts": [
    "background.js",
    "https://apis.google.com/js/client.js?onload=callbackFunction"
  ]
},

但是扩展没有加载.我也试过将脚本注入后台 html

but then the extension doesn't load. I've also tried injecting the script into the background 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 调试器给了我

but the chrome debugger gives me

拒绝加载脚本https://apis.google.com/js/client.js",因为它违反了以下内容安全策略指令:script-src 'self' chrome-extension-resource:".

有什么想法,还是注定要分开?

Any ideas, or are they fated to be kept apart?

请注意,如果您想使用回调函数,则必须将?onload=myCallbackFunction"添加到脚本 url.谢谢伊利亚.更多信息此处

note that you must add "?onload=myCallbackFunction" to the script url if you want to utilize a callback function. Thanks Ilya. More info here

推荐答案

到目前为止,我找到的唯一解决方案是像我一样首先将脚本注入后台 html 页面:

So far the only solution I've found is to first inject the script into the background html page like I did:

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);

然后要绕过安全警告,请编辑清单文件 (来源):

And then to bypass the security warning, edit the manifest file (source):

ma​​nifest.json(摘录)

"content_security_policy": "script-src 'self' https://apis.google.com; object-src 'self'"

但是,请注意绕过安全性仅适用于 https 链接,而且我也觉得它有点麻烦......欢迎任何其他解决方案

However, note that bypassing the security only works for https links, and I also find it kind of hacky...any other solutions are welcome

这篇关于将 Google API Javascript 客户端库加载到 Chrome 扩展程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆