When is the extension loaded?

I have a page that needs to communicate with the extension if it is installed, and do something else if not.
Normally, I did the check in the window.onload handler.
It worked fine prior to Firefox v.55.
Now at the moment when window.onload fires, the plugin is not yet loaded.
Additional 200ms timeout helps in most cases, but not always.
Is there a better way to catch the moment when the extension is loaded and ready to use?

If by “the extension” you mean a WebExtensions content script, then it should usually load long before the load event (it you specify "run_at": "document_start").

Thank you Niklas!
I’m trying to dispatch event to the background script, but it seems to be not ready at the load event. Any suggestions?

Sorry for the delayed reply.

Were do you want to dispatch the event from, and whose window.onload do you mean?

Here is the add-on I’m talking about: https://addons.mozilla.org/en-US/firefox/addon/jazz-midi/
It is supposed to work on the third-party pages.

The script on the page first tries to find out if the visitor have the add-on is installed by sending the event that should be responded by the add-on:
document.dispatchEvent(new Event(‘jazz-midi’));

If this is done to early while the add-on is not yet loaded, the event just gets lost.
The question is - when it is a right time to send the event to the add-on?

Before Firefox v.55 I could be sure that at the moment of the page’s onload handle the add-on was ready and listening, but it is not the case anymore.

As I mentioned. Please read https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/content_scripts#run_at and be aware that Document.readyState complete correlates with the load event. You should specify "document_start" of "document_end".

Thanks a lot! That seems to be exactly what I was looking for!

Oh, actually that applies to CONTENT_script. Will it work with the BACKGROUND_script ?

What way are you trying to do the event? When the page loads tell the background script to load something or tell the page something from the background script?

I feel in either case you should have a content script that when loaded, emits a message event to the background script to do something.

I guess you are right. It’s a content script that first should get message from the page.
Let me experiment with the add-on a bit…

Works perfect! Thanks to everyone for help!