Using google api client library with addon SDK

<<

DavideBuldrini

Posts: 7

Joined: Mon Apr 22, 2013 6:25 am

Post Mon Apr 22, 2013 6:42 am

Using google api client library with addon SDK

I need to use google api javascript client library http://code.google.com/p/google-api-javascript-client/ in my addon.
I have a content script where I dynamically added the library script to the web page:

  Code:
var head = document.getElementsByTagName('HEAD').item(0);
var script= document.createElement("script");
script.type = "text/javascript";
script.src="https://apis.google.com/js/client.js?onload=gApiLoaded";
head.appendChild(script);


I also added the callback function "gApiLoaded" in the web page that send a message to the content script to let know when the library is loaded:

  Code:
script = document.createElement('script');
script.type = 'text/javascript';
var code = 'function gApiLoaded() { window.postMessage("gApiLoaded", window.location.protocol + "//" + window.location.host); }';
script.appendChild (document.createTextNode(code));
document.body.appendChild(script);


All this works fine but I have the problem that I cannot directly access the gapi object within the content script because this library was loaded in the page. I could use a series of messages between the content script and the page but I think will be very complicated.

Any suggestion on the right way to do it?
<<

DavideBuldrini

Posts: 7

Joined: Mon Apr 22, 2013 6:25 am

Post Wed Apr 24, 2013 9:31 am

Re: Using google api client library with addon SDK

Anybody?
<<

wbamberg

Posts: 236

Joined: Thu Jun 23, 2011 4:08 pm

Post Wed Apr 24, 2013 7:08 pm

Re: Using google api client library with addon SDK

Does it help to use "unsafeWindow" rather than "window"? Of course, that would be unsafe. (https://addons.mozilla.org/en-US/develo ... safeWindow)
<<

DavideBuldrini

Posts: 7

Joined: Mon Apr 22, 2013 6:25 am

Post Sat Apr 27, 2013 1:00 am

Re: Using google api client library with addon SDK

Thank you for the answer. Yes, using unsafeWindow it works but I would avoid to use that.
<<

DavideBuldrini

Posts: 7

Joined: Mon Apr 22, 2013 6:25 am

Post Fri May 03, 2013 2:12 pm

Re: Using google api client library with addon SDK

does nobody know easier solutions?
<<

kwierso

Posts: 116

Joined: Mon Jul 04, 2011 12:26 pm

Post Fri May 03, 2013 2:18 pm

Re: Using google api client library with addon SDK

DavideBuldrini wrote:does nobody know easier solutions?

Not really. The window object you get in the content script is protected from changes that the page's scripts make to it. The only way around it is to use the unsafeWindow to access the unprotected, original window object.
<<

DavideBuldrini

Posts: 7

Joined: Mon Apr 22, 2013 6:25 am

Post Fri May 03, 2013 2:31 pm

Re: Using google api client library with addon SDK

So the only way to respect security guidelines and get this work done is to use a series of messages between the content script and the page.
There is a way to include the google api library in the content script instead of the page? It should simplify many things.
<<

kwierso

Posts: 116

Joined: Mon Jul 04, 2011 12:26 pm

Post Fri May 03, 2013 2:34 pm

Re: Using google api client library with addon SDK

DavideBuldrini wrote:So the only way to respect security guidelines and get this work done is to use a series of messages between the content script and the page.
There is a way to include the google api library in the content script instead of the page? It should simplify many things.

You can set up the contentScriptFile property on your pageMod's constructor to be an array of files. You can include the google library as one of the items in that array, along with your normal contentScript file, and it should load it up in the page and be usable by your contentscript.
<<

DavideBuldrini

Posts: 7

Joined: Mon Apr 22, 2013 6:25 am

Post Fri May 03, 2013 2:44 pm

Re: Using google api client library with addon SDK

I already tried it, these are the step I did:

- I added a new content script file and copied the contents into from https://apis.google.com/js/client.js
- I setup the content script file property as array as you suggest including the newly created content script file

but for some reason the library won't load and I cannot access the gapi object. I think I cannot use this kind of library offline. I mean I can only include it in a page with script src tag. Correct me if I'm wrong.
<<

kwierso

Posts: 116

Joined: Mon Jul 04, 2011 12:26 pm

Post Fri May 03, 2013 2:58 pm

Re: Using google api client library with addon SDK

DavideBuldrini wrote:I already tried it, these are the step I did:

- I added a new content script file and copied the contents into from https://apis.google.com/js/client.js
- I setup the content script file property as array as you suggest including the newly created content script file

but for some reason the library won't load and I cannot access the gapi object. I think I cannot use this kind of library offline. I mean I can only include it in a page with script src tag. Correct me if I'm wrong.

Hrm. I'm seeing the following error when I include it as a contentScriptFile:

Timestamp: 5/3/2013 2:56:33 PM
Error: ReferenceError: gapi is not defined
Source File: https://apis.google.com/_/scs/apps-stat ... i.loaded_0
Line: 1

I've never used that library before, so I can't tell for sure, but I think you might be stuck using unsafeWindow...
<<

DavideBuldrini

Posts: 7

Joined: Mon Apr 22, 2013 6:25 am

Post Fri May 03, 2013 3:07 pm

Re: Using google api client library with addon SDK

yes, I think I have no choice. Thank you for your help!
<<

kwierso

Posts: 116

Joined: Mon Jul 04, 2011 12:26 pm

Post Fri May 03, 2013 3:13 pm

Re: Using google api client library with addon SDK

DavideBuldrini wrote:yes, I think I have no choice. Thank you for your help!

I'd add a comment to your code near wherever you use unsafeWindow explaining why you need to use it instead of the normal window, so that you don't get as many complaints from addon reviewers on AMO that'd review your code.

Return to Add-on SDK & Add-on Builder

Who is online

Users browsing this forum: No registered users

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
CA Gen2 style designed by Vjacheslav Trushkin.