WebExtension content script not seeing the DOMContentLoaded event

Hi all,

I’m running into some interesting behaviour in content scripts. It seems like I can’t get my content script to detect DOMContentLoaded or “load” events. E.g.:

document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOM fully loaded and parsed");
});

window.addEventListener("load", function(event) {
    console.log("All resources finished loading!");
});

Neither of the above actually fire. Is this expected behaviour? I can try to whip up a minimal webex add-on to make it easier to share.

How are you injecting the content script?

Is it from manifest.json?

If it is, then the default injection point (or run_at) is "document_idle".
That means it is injected AFTER those events have been fired so there is nothing to listen to anymore.

If you want to run those listeners, you would need to set run_at: "document_start"

Ahh thanks, that was exactly it. My situation is actually slightly trickier because the website I’m injecting into creates elements dynamically, so I’ll need to figure out some way to wait for those to show up, or just poll. But at least I know now the content script is injected at the right time.