javascript GAPI 未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29433744/
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
GAPI Is Not Defined
提问by Keith
I am having much trouble getting the Google javascript api to load in my chrome extension. Please note I am very new to javascript and even newer to chrome extensions.
我在让 Google javascript api 加载到我的 chrome 扩展程序中遇到了很多麻烦。请注意,我对 javascript 非常陌生,甚至对 chrome 扩展程序也很陌生。
I have a background.js file which executes a script
我有一个执行脚本的 background.js 文件
chrome.tabs.executeScript(null, { file: "ChromeExtension.js" });
This ChromeExtension.js file then look as follows
这个 ChromeExtension.js 文件如下所示
//Call Initialize Method
init();
//Function To Initial Chrome Extension
function init(){
var clientID = 'Client ID';
var apiKey = 'API Key';
var scopes = 'https://www.googleapis.com/auth/plus.me';
loadGAPIClient();
gapi.client.setApiKey(apiKey);
}
My problem is that at
我的问题是在
gapi.client.setApiKey(apiKey);
I get gapi is not definedThe thing is once my ChromeExtension.js has completed execution, gapi is fully defined and available.
我得到gapi未定义事情是一旦我的 ChromeExtension.js 完成执行,gapi 已完全定义且可用。
I have tried other suggestions in some stack overflow questions but to no avail. I believe this is due to lack of Javascript knowledge but I would be grateful if anyone would be able to provide some assistance.
我在一些堆栈溢出问题中尝试了其他建议,但无济于事。我相信这是由于缺乏 Javascript 知识,但如果有人能够提供一些帮助,我将不胜感激。
Thank you for your time.
感谢您的时间。
EDIT- Current GAPI Load
编辑- 当前 GAPI 负载
function () loadGAPIClient(){
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "https://apis.google.com/js/client.js";
$("head").append(s);
}
This function is called in my init(), which I have also updated to reflect this.
该函数在我的 init() 中调用,我也对其进行了更新以反映这一点。
I have also tried using jQuery.getScript among other ways.
我也尝试过使用 jQuery.getScript 等方法。
Please understand this is my issue, I cannot find a way to correctly load the GAPI Client
请理解这是我的问题,我找不到正确加载 GAPI 客户端的方法
采纳答案by Xan
Isolated worldproblem.
孤立的世界问题。
Specifically, your loadGAPIClient
adds a <script>
tag which then executes the script in the page's context, which is different from the content script context.
具体来说,您loadGAPIClient
添加一个<script>
标签,然后在页面上下文中执行脚本,这与内容脚本上下文不同。
The end result is that gapi
becomes defined in the page's code (possibly creating a conflict if the page loaded own copy), but is still undefined in yours.
最终结果是gapi
在页面代码中定义了(如果页面加载了自己的副本,可能会产生冲突),但在您的代码中仍然未定义。
I don't see an easy way out. You can only load things in the content script context by calling executeScript
or declaring them in the manifest; and if I recall correctly GAPI will try to load more libraries with the <script>
injection method.
我看不出有什么简单的出路。您只能通过executeScript
在清单中调用或声明它们来加载内容脚本上下文中的内容;如果我没记错的话,GAPI 将尝试使用<script>
注入方法加载更多库。
So I guess your best bet is to load the library in a background page and work with it from there, since loading external JS this way will be okay as long as you modify the CSP.
所以我想你最好的办法是在后台页面中加载库并从那里使用它,因为只要你修改 CSP ,以这种方式加载外部 JS 就可以了。
Or alternatively, you could try this library, which works around the issues you have with default CSP and uses chrome.identity
API. It may fit your needs, though again it won't work in a content script.
或者,您可以尝试使用这个库,它可以解决您在使用默认 CSP 时遇到的问题并使用chrome.identity
API。它可能符合您的需求,但它同样不适用于内容脚本。
回答by aqquadro
Doesn't threat my answer as offensive, in your code snippet there isn't evidence of that, you have load the Google APIs JavaScript library as show on the reference?
不会威胁我的回答令人反感,在您的代码片段中没有证据表明您已经加载了 Google APIs JavaScript 库,如参考资料所示?
<script src="https://apis.google.com/js/plus.js?onload=init"></script>
You need to use this method, doesn't call init by hand: let's gapi call for you :)
你需要使用这个方法,不要手动调用init:让我们gapi为你打电话:)