views:

97

answers:

1

Howdy, ok I'm aware that a content script can communicate with the background page using:

chrome.extension.sendRequest({action:'test'}, function(response) {
  //code here...
});

someFunction();

But is it possible to communicate synchronously? Basically wait until the response comes back to the content script before executing someFunction()?

If not, is it possible to communicate with the bg page using a normal xmlhttprequest?

Why you ask?

I am loading my content script into the web page on "document_start" (required) and one of my variables in the CS depends on a localStorage variable set in the options page. So I need this localStorage variable from the background page before someFunction() is called.

Thanks in advance.

+1  A: 

You can chain the callbacks to call the next request.

or

You can explicitly specify the XHR to be synchronous.

chrome.extension.sendRequest({action:'test'}, function(response) {
    someFunction(response);  // Calling the function
    // or
    chrome.extension.sendRequest(...);
});
function someFunction (resp) {
    // Execute code
};
CrazyEnigma
Can you provide an example of "chaining callbacks" such that the response from the background page comes back to the content script before someFunction() is called?
UICodes
Ok, there is a misunderstanding. In my example, "someFunction()" represents all the other logic included in the page and cannot be moved inside of the callback.So technically you did answer the question (thanks) but I'm looking for a way to retrieve data from background page immediately, or if that's not possible, synchronously (while the page waits for the response).
UICodes