views:

67

answers:

2

Right now I'm in the process of creating a Chrome extension. For it, I need to use Google's Calendar Data API. Here is my manifest.json file:

{
"name": "Test",
"version": "1.0",
"background_page": "background.html",
"content_scripts": [
    {
    "matches": ["<all_urls>"],
    "js": ["jquery.js", "content_script.js"]
    }
],
"permissions": [
    "tabs", "http://*/*"
]

}

I've tried adding the following to the js part of the manifest file but that throws an error when loading the extension.

http://www.google.com/jsapi?key=keyhere

I've also tried adding

document.write('<script type="text/javascript" src="http://www.google.com/jsapi?key=keyhere"&gt;&lt;/script&gt;');

to my background.html file. However, whenever I call

google.load("gdata", "1");

I get an error that says Uncaught ReferenceError: google is not defined. Why is my extension not loading this api when it loads the other ones fine?

A: 

You can't include external script into content_scripts.

If you want to inject <script> tag using document.write then you need to mask slash in a closing tag:

document.write('...<\/script>');

You can include your external api js into background page just as usual though:

<script type="text/javascript" src="http://www.google.com/jsapi?key=keyhere"&gt;&lt;/script&gt;

If you need this api in content scripts then you can send a request to your background page and ask it to do API-dependent stuff there, and then send a result back to your content script.

serg
I tried just adding it as normal in the background page, and that doesn't work, either. I need it in content_scripts, but I thought loading it in background.html would automatically allow me to use it in content_scripts.
joshholat
@joshholat Background page is pretty much just a normal page without any restrictions. If it doesn't work there then the problem is in the code. Put your background page into webserver root and open it in a browser - I bet it wouldn't work there either.
serg
Also putting api js into background page doesn't automatically load it into content scripts, they are completely isolated. In order to communicate between them you need to send requests back and forth.
serg
What I meant is that I added the script to background.html the normal way, but that it still won't recognize Google in content_scripts after doing that. It will recognize google.load("gdata", "1"); in background.html, but I need it in content_scriptsAfter seeing your addition, I suppose my above comments make sense as to why it isn't working, now.
joshholat
@joshholat If you absolutely need it in content scripts then try `document.write` trick.
serg
@joshhola Also I am not very familiar with google's jsapi, maybe they have offline version of their library? Then you can just download it and inject using `content_scripts`.
serg
It's not absolutely necessary, I'm just not sure how I'd send information between the two.
joshholat
@joshholat You can read about requests here: http://code.google.com/chrome/extensions/dev/messaging.html
serg
A: 

Thanks for that link, it helped a lot. However, now I've run into another interesting problem.

    for (var i = rangeArray.length - 1; i >= 0; i--) {
    var myLink = document.createElement('a'); 
    myLink.setAttribute('onclick','helloThere();'); 
    myLink.innerText ="GO";
    rangeArray[i].insertNode(myLink);
}

Now I get the error, "helloThere is not defined" even though I placed that function about ten lines above the current function that has the above loop in the same file. Why might this be happening? And if I do:

    for (var i = rangeArray.length - 1; i >= 0; i--) {
    var myLink = document.createElement('a'); 
    myLink.setAttribute('onclick','chrome.extension.sendRequest({greeting: "hello"}, function(response) { });'); 
    myLink.innerText ="GO";
    rangeArray[i].insertNode(myLink);
}

I get Uncaught TypeError: Cannot call method 'sendRequest' of undefined

joshholat
It probably would be better to put it as another question as it is not related. Content script and actual page don't share variable space, they only share DOM.
serg
Don't use strings, use function references. Instead of `myLink.setAttribute('onclick','helloThere();');` say `myLink.onclick = helloThere`
Dominic Mitchell