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!