简体   繁体   English

GAPI未定义

[英]GAPI Is Not Defined

I am having much trouble getting the Google javascript api to load in my chrome extension. 我在使用我的Chrome扩展程序加载Google javascript api时遇到了很多麻烦。 Please note I am very new to javascript and even newer to chrome extensions. 请注意我是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 defined The 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客户端的方法

Isolated world problem. 孤立的世界问题。

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; 您只能通过调用executeScript或在清单中声明它们来加载内容脚本上下文中的内容; and if I recall correctly GAPI will try to load more libraries with the <script> injection method. 如果我没记错的话,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. 或者,您可以尝试使用此库该库可解决您使用默认CSP时chrome.identity的问题并使用chrome.identity API。 It may fit your needs, though again it won't work in a content script. 它可能符合您的需求,但同样在内容脚本中也不起作用。

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 API 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 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM