开发者

Content script communicate with bg page [Synchronous]

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.


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
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜