The reason this is happening is that the panel closes on blur – and opening any kind of (non-HTML) dialog takes the focus. This behavior is … unfortunate, but there is no way to prevent it (as an extension developer).
Anyway, after the panel closes and emits its unload
event, no other events or timers related to that window
will fire.
What could work is to have the background page “own” the input element. Maybe the event will then sill fire even if the panel is gone. But I don’t think that the file picker can be opened while the input element is owned by the background page. So what you might want to try is to transfer the ownership of the element to the background page after the picker is opened.
That could look something like this:
const input = document.querySelector('input[type=file]');
const background = await browser.runtime.getBackgroundPage();
if (!background) { console.warn(`This doesn't work in private windows ...`); return; }
window.onunload = () => {
background.document.adoptNode(input); // this may or may not help, but if it doesn't, I don't think there is a solution at all ...
};
input.onchange = () => {
background.doSomethingWith(input.files);
};
Just be aware that your window
is still dead, so doSomethingWith
should probably be a function from a background script, to avoid weird things.