Injecting content script while tab is unloaded

I see that tabs can be in an unloaded state (depending on some options in about:config), for example, when your session has just restored or the minimum time limit for unloading has passed.

I’m trying to load a content script into all tabs in my current window but some of the tabs are unloaded and this halts my background.js script, effectively breaking it until I either re-trigger my browser action or reload the extension.

How can I load the content script in unloaded tabs without manually reloading every tab in the browser window.

I’ve tried using browser.tabs.reload() before injecting but it gives me a host permissions error, I think because the tab isn’t yet fully reloaded when my injection code runs. What is the best and most efficient way to do this. The troubling portion of the code is below.

async function handleClick() {
	/* Empty fileArray just incase it's not empty upon alarm */
	fileArray.length = 0;
	/* After user triggers browser action add file header to array once */
	fileArray.push(getHeader());
	/* Get all tabs in the active window */
	const tabs = await browser.tabs.query({ currentWindow: true });
	/* Begin tab reload and content script injection loop */
	for (const tab of tabs) {
		try {
			/* Reload tab */
			/* Debug: **What to do here**
			await browser.tabs.reload(tab.id); 
			}*/
			await browser.tabs.executeScript(
				tab.id,
			{
				file: "/content-script.js",
			});
			console.log(
				`Successfully injected in tab ${tab.id}`);
		}
		catch (error) {
			console.log(error);
		}
	}
...

I’d be happy to supply any more needed information. Thanks.

Sorry about above, code is below.

async function handleClick() {
    /* Empty fileArray just incase it's not empty upon alarm */
    fileArray.length = 0;
    /* After user triggers browser action add file header to array once  */
    fileArray.push(getHeader());
    /* Get all tabs in the active window */
    const tabs = await browser.tabs.query({ currentWindow: true });
    /* Begin tab reload and content script injection loop */
    for (const tab of tabs) {
        try {
            /* Reload tab */
            /* Debug: **What to do here**
            await browser.tabs.reload(tab.id); 
            */
            await browser.tabs.executeScript(
                tab.id,
            {
                file: "/content-script.js",
            });
            console.log(`Successfully injected in tab ${tab.id}`);
        }
        catch (error) {
            console.log(error);
        }
}

I remember dealing with this bug long ago.
But I had a different use case so my solution was to simply “skip” discarded tabs.

In any case, maybe you could “not await” the injection into discarded tabs.
But I can say if it’s gonna inject it once it’s loaded…