'How to detect if a tab is unloaded after you restart the Chrome browser?

Let's say I have a bunch of tabs opened in Chrome, I close the browser and then reopen it. Those tabs will remain unloaded, until I click on them, then they will load automatically. This is a native feature of the browser to save memory.

I am looking for a way to check when tabs are in this specific state, before I click on them. I have tried the two properties .status and .discarded mentioned in the chrome.tabs api, which provide information if the tabs are unloaded or discarded:

https://developer.chrome.com/docs/extensions/reference/tabs/

but the values they give those properties are always the same, regardless if those tabs are completely unloaded after a restart or fully loaded after I click on them:

.status = "complete" 

.discarded = "false"

And now I am stuck. I dont know how to solve this. Any help will be appreciated. Thank you.

ps. I am not using any tab suspension addons.



Solution 1:[1]

You can check the discarded or status property of a tab, a discarded tab will have its status as “unloaded”:

chrome.runtime.onStartup.addListener(()=>{
    chrome.tabs.query({currentWindow: true}, (tabs)=>{
      tabs.forEach(tab =>{
        if(tab.discarded === true || tab.status === “unloaded” ){
           //Do something      
        }
      })   
    })
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1