0

So I have been trying for hours, but I could not make it work. This is happening only on a published chrome extension not on my unpacked build

this is the pop-up script:

_handleMessageResponse = (response) => {
    console.log(response) // undefined
  };

chrome.runtime.sendMessage(
  "message",
  this._handleMessageResponse
);

and on my background script

chrome.runtime.onMessage.addListener(
  async (
    message,
    _sender: any,
    sendResponse: any
  ) => {
    try {
      const response = await someAsyncApiCall();
      console.log(response) // {some: 'prop'}
      sendResponse({
        response
      });
      return true;
    } catch (e) {
      sendResponse({ errorMessage: e.message });
      return true;
    }
  }
);


In my local environment it returns an object, while in a published extension returns undefined. In the published extension the response in the background script is of course defined.

Probably is because is an async function, but I did not find anything rather then returning true from the handler, but even that (like in my code is showing) it did not working.

Matteo
  • 2,256
  • 26
  • 42
  • A typical cause is to put the onMessage listener inside onInstalled, [more info](https://stackoverflow.com/a/57472453). – wOxxOm May 29 '20 at 14:05
  • 1
    Also, you can't use an `async` function directly as a listener for onMessage **and** `return true` in Chrome, [more info](https://stackoverflow.com/a/53024910). – wOxxOm May 29 '20 at 14:07
  • yea, I have figured out, thanks. And that is why it was working in my local (still weird) – Matteo May 30 '20 at 09:06

0 Answers0