Search code examples
javascriptgoogle-chrome-extensionsynchronousmessage-passing

Communicating synchronously with a chrome extension


I'm looking for a way to send and receive message synchronously from a chrome extension. The idea would be to call a function defined in my extension from a traditional web page (web app) in a synchronous way so that i don't have to wait before receiving the result.

Unfortunately, i read Synchronous message passing in chrome extensions? and https://code.google.com/p/chromium/issues/detail?id=135095. Both say that it's not possible.

But i thought that the XMLHttpRequest can make synchronous call.

I know this is certainly not possible but i'm asking anyway.. Would there be a way to use the XMLHttpRequest object in such a way that we could communicate synchronously with an extension?


Solution

  • I'm sorry for you, but the answer is still no. I don't see why using XMLHttpRequests would help to make any kind of operation that is done (asynchronously) with the Chrome API, but let's assume you've got some content_script.js that runs inside some page, and you need to send a message to background.js. How would you accomplish this by using an XMLHttpRequest? Making a request to your background.js will not give you the chance to run its code, but only to read it. That's because most of the API methods used in background.js aren't available in a content script. That's the same for any other asynchronous operation that happens inside a Chrome extension.

    So the problem here is that Chrome Extensions cannot use XMLHttpRequests to communicate within their environment, thus, you just can't use them in that way.