MV3 massage passing: service worker consistently returns undefined response the first time

889 views
Skip to first unread message

Giacomo Tommaso Petrucci

unread,
Jul 11, 2022, 5:30:22 AM7/11/22
to Chromium Extensions
Short version: the first time I send a message from a script injected inside a web page to a service worker using chrome.runtime.sendMessage, I *always* get a response inside the callback function that is undefined, even if according to Chromium the service worker is active. Subsequent attempts seem to work correctly. If the method that I invoke to generate the data to be put inside the message fails, I still send a message containing an object, but in this cast the object contains an error. So the problem isn't that method failing.

More detailed version: I'm developing an extension to encrypt and decrypt Gmail's emails. For this, I let the user compose the email as usual, and then there's an added button in the compose view that encrypts and sends the email (for interacting with Gmail's UI, I'm using the Gmail.js library. This is why the script that send the message to the service worker is injected into Gmail's web page. So form Chromium's perspective is the web page that sends the message). The first time that I click on that button, I get the following two errors in the console:

Error handling response: TypeError: Cannot read properties of undefined (reading 'ciphertext')
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.

This happens even if according to Chromium the service worker is active (and indeed I can inspect the service worker's output prior to this to confirm it) and even if the service worker is supposed to send an object containing an error if the encryption fails. Subsequent attempts work.
As a workaround, I could check if the response is undefined and send the message again, but I would like to find the root cause of the issue, any ideas about what's wrong here?

You can find my code here: https://github.com/GTP95/IRMA-Gmail-webextension/tree/chrome
The script that tries to send the message is src/js/gmailInteraction.js while the service worker is in src/js/sw.js
I can also try to write a minimal reproducible example, if needed.
Thank you for your help,

Giacomo


wOxxOm

unread,
Jul 11, 2022, 12:03:21 PM7/11/22
to Chromium Extensions, giacomo.to...@gmail.com
To use sendResponse asynchronously your onMessageExternal listener must have `return true` at the end.

Giacomo Tommaso Petrucci

unread,
Jul 11, 2022, 12:21:35 PM7/11/22
to Chromium Extensions, wOxxOm, Giacomo Tommaso Petrucci
You're right! I actually already run into the same problem before, but than my code changed and I forgot about it. Thank you very much!

Giacomo Tommaso Petrucci

unread,
Sep 8, 2022, 9:15:48 AM9/8/22
to Chromium Extensions, Giacomo Tommaso Petrucci, wOxxOm
Greetings,

Sorry if I resurface an old thread, but the problem come back: the very fist time message passing happens after starting Chromium, it fails by returning an undefined response to the ContentScript even if I have `return true` at the end of my message handler in the service worker. This happens either when encrypting an email that has to be sent or decrypting an email that has been received, whatever happens first. Any idea about why this happens?

As said in the first message, you can find my code here: https://github.com/GTP95/IRMA-Gmail-webextension/tree/chrome and the script that tries to send the message is src/js/gmailInteraction.js while the service worker that responds to it is in src/js/sw.js. I can also try to write a minimal reproducible example, if needed.
Thank you for your help,

Giacomo


Mike-O

unread,
Sep 20, 2022, 9:08:17 AM9/20/22
to Chromium Extensions, giacomo.to...@gmail.com, wOxxOm
Not that this might help, but I've found that after I reload my plugin in the Extensions panel by using the circular arrow icon button, I need to restart all previous tabs in order for it to be able to reach the service worker again. What I do in my content script is detect this very specific failed communication status error code and then I reload the current tab. The service worker can then be reached again.
Reply all
Reply to author
Forward
0 new messages