0

I am making a chrome extension and I would like to make use of a third party API but I am running into some issues and I've been stuck on this problem for a while...

Problem:

I cannot make use of the functions in the API even though the chrome Dev tool says that the API was loaded successfully.

I've tried to load the API at different times to see if that would make a difference but it doesn't seem to.

I've used the API successfully in a basic web page. But I cannot seem to get it working in the content script of the extension.

Here's the code to load the API:

(function() {
    var scr = document.createElement('script');
    scr.type = 'text/javascript';
    scr.src = 'someurl';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(scr, s);
})();

Manifest:

 "content_security_policy": "script-src 'self' someurl; object-src 'self'"

I've also done the tutorial that Google offers on how to use Google Analytics within an extension because I thought it would be relevant. But no luck. Any guidance would be appreciated.

2
  • Where do you need to use the API? Background page, popup, content script? Commented Apr 11, 2014 at 23:04
  • Content Script. Because I want to Inject a Button into the page and when the button is clicked, I want a modal to open and I will be using the API in that Popup. I would make use of it in the popup portion of the extension but it is not possible to open browser action popups programatically. Commented Apr 13, 2014 at 21:08

1 Answer 1

1

You are injecting the API into the context of the webpage when you append a script tag; your content script remains isolated from it.

To circumvent that, one possible solution is to also append some of your code, that would then talk to your extension, either through DOM or through external messages

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.