Error: Could not establish connection. Receiving end does not exist.

930 views
Skip to first unread message

Almog Idisis

unread,
Jul 17, 2023, 12:01:32 PM7/17/23
to Chromium Extensions
So, a moment before you are saying "Oh no, not again this bug has 1234 answers to it already", give me a chance here :>

We are moving from the traditional, built-in bundler of the extension to a Vite & Rollup-powered engine (https://github.com/crxjs), this, in order to improve the development experience and more.

Now, when, for example, I refresh a tab, say google.com, `chrome.tabs.onUpdated` dispatches, and after some logic, we try to send a message to that tab so it injects some UI into the tab. 

It seems that ever since we moved, the content script within the tab is not ready to receive messages, yielding the `Error: Could not establish connection. Receiving end does not exist.` error.
Out of 10 refreshes, it would happen 5 times.

My assumption is that due to the extra bundling work, it takes longer for the content script to load, therefore the onMessage listener is not ready.

We are able to go past that by either setting a 1000ms "wait" before sending that initial message, or just sending messages to the tab every 1000ms until we get an "I'm ready" response (example code attached).

It confirms my guess that we have a race condition between chrome.tabs.onUpdated and the initiating process of the content scripts.

My question is, is there a better way to wait for the content script to fully load?

Thanks all!
Screenshot 2023-07-17 at 18.58.12.png

Jackie Han

unread,
Jul 17, 2023, 1:43:01 PM7/17/23
to Almog Idisis, Chromium Extensions
I don't know why you use tabs.onUpdated to communicate with content scripts. Injecting content script needs some time when document initial loads. Also it depends on the 'runAt' parameter.
One solution is removing content scripts and using chrome.scripting.executeScript() in tabs.onUpdated.

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/6c1b3e94-4ca7-45e8-af13-673eeb21884bn%40chromium.org.

Almog Idisis

unread,
Jul 18, 2023, 3:04:13 AM7/18/23
to Chromium Extensions, Jackie Han, Chromium Extensions

Hey Jackie, thanks for replying!
The reason we use `onUpdated` is because some tabs are relevant for injection, while others are not.
So, when a new tab opens, we check the URL, and, if needed, send an "INJECT" message.

The runAt parameter is `document_idle`, the default. I tried `document_start` but found out that a given tab is still not ready 10\10 times.

wOxxOm

unread,
Jul 18, 2023, 10:30:55 AM7/18/23
to Chromium Extensions, Almog Idisis, Jackie Han, Chromium Extensions
To ensure reliable communication you can let the content script be an initiator by using chrome.runtime.sendMessage or chrome.runtime.connect inside the content script.
Reply all
Reply to author
Forward
0 new messages