Ole Albers
Ole Albers

Reputation: 9295

Call Chrome-Extension Method from within options page?

I wrote a chrome extension for a specific website. If the extension is installed and I navigate to that site "example.com", my extension calls the following method:

 var search="john";
 $.get("https://www.example.com/complete/search?q="+search, function (data) {
        console.log(data);
    });

That works just fine.

Now I have to call the same method from within the options-page of that extension. When I do that, I receive the following error:

XMLHttpRequest cannot load https://www.example.com/complete/search?q=John. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

I understand why this happens but have to find a way to get around this. My idea is to trigger the Extension-Script to call the method and then return the results to the options page. I expect, chrome.runtime.sendMessage() isn't help here.

Needless to say: That domain is not hosted by me, so I can't just change the header.

Upvotes: 0

Views: 208

Answers (1)

Xan
Xan

Reputation: 77523

The error indicates it's not an actual options page: you're opening it (possibly accidentally) through a local webserver (origin: http://localhost/) instead of opening a page packaged with the extension.

  1. You need to add your page (say, options.html) to your extension's folder
  2. You need to add it to the manifest like "options_page": "options.html" or better yet with options_ui
  3. You need to reload the extension to apply the manifest change
  4. You need to open it through the extension, e.g. through chrome://extensions or the context menu of your extension's button (if any)

Upvotes: 1

Related Questions