GAPI 未定义 [英] GAPI Is Not Defined
问题描述
我在将 Google javascript api 加载到我的 chrome 扩展程序中遇到了很多麻烦.请注意,我对 javascript 非常陌生,甚至对 chrome 扩展程序也很陌生.
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.
我有一个执行脚本的 background.js 文件
I have a background.js file which executes a script
chrome.tabs.executeScript(null, { file: "ChromeExtension.js" });
这个ChromeExtension.js文件如下
This ChromeExtension.js file then look as follows
//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);
}
我的问题是在
gapi.client.setApiKey(apiKey);
我得到 gapi 未定义 事情是一旦我的 ChromeExtension.js 完成执行,gapi 已完全定义并且可用.
I get gapi is not defined The thing is once my ChromeExtension.js has completed execution, gapi is fully defined and available.
我在一些堆栈溢出问题中尝试了其他建议,但无济于事.我相信这是由于缺乏 Javascript 知识,但如果有人能够提供一些帮助,我将不胜感激.
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.
感谢您的宝贵时间.
编辑 - 当前 GAPI 加载
EDIT - Current GAPI Load
function () loadGAPIClient(){
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "https://apis.google.com/js/client.js";
$("head").append(s);
}
此函数在我的 init() 中调用,我也对其进行了更新以反映这一点.
This function is called in my init(), which I have also updated to reflect this.
我也尝试过使用 jQuery.getScript 等方法.
I have also tried using jQuery.getScript among other ways.
请理解这是我的问题,我找不到正确加载 GAPI 客户端的方法
Please understand this is my issue, I cannot find a way to correctly load the GAPI Client
推荐答案
孤立世界 问题.
具体来说,您的 loadGAPIClient
添加一个 标签,然后在页面上下文中执行脚本,这与内容脚本上下文不同.
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.
最终结果是 gapi
在页面代码中定义(如果页面加载自己的副本,可能会产生冲突),但在您的代码中仍然未定义.
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.
我认为没有捷径可走.您只能通过调用 executeScript
或在清单中声明它们来加载内容脚本上下文中的内容;如果我没记错的话,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.
所以我猜你最好的办法是在后台页面中加载库并从那里使用它,因为只要你 修改 CSP.
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 中遇到的问题,并使用 chrome.identity
API.它可能符合您的需求,但同样不适用于内容脚本.
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.
这篇关于GAPI 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!