0

I want to check in how many tabs my application is currently opened using chrome.tabs api whenever onbeforeunload event occurs in application.

So I wanted to know if it is possible to do it using chrome.tabs if yes then how can it be done.

Sagar Shirke
  • 648
  • 9
  • 32
  • Are you trying to capture some state before a window closes or when chrome closes? Since chrome exit won't guarantee events callbacks firing before closing. – holmberd May 22 '18 at 13:53
  • onbeforeunload is for synchronous code only so none of chrome.* API with callbacks would work there because the callbacks are invoked asynchronously. You'll have to maintain a list of URLs constantly in a [global] variable, for example by using chrome.tabs.onUpdated and onRemoved events. – wOxxOm May 22 '18 at 13:54
  • @holmberd basically my Idea was to clear asp.net websites session as soon as it gets closed from all the tabs of chrome. SO by using Chrome.tabs I will find in how many tabs application is opened and if it is 0 then clear the session by callback. – Sagar Shirke May 22 '18 at 14:02
  • @SagarShirke but is this for when a specific window closes, or chrome itself? – holmberd May 22 '18 at 14:07
  • @holmberd.. it is for both, Chrome close and Tab close – Sagar Shirke May 22 '18 at 14:08
  • @SagarShirke ok, then as I mentioned in the beginning, chrome doesn't guarantee that your callbacks will have time to fire when chrome is closing. Once chrome start to close, then some of your events, `chrome.windows.onRemoved` or windows unload, might be called; but since there is no guarantee your "clean-up" will take place, it might leave your extension in a non-consistent state. – holmberd May 22 '18 at 14:17
  • There is a similar question asked here: https://stackoverflow.com/questions/42109296/is-there-any-way-to-count-number-of-tabs-are-opened-in-chrome Check this and if it didnt help, ask for more help. – nima May 22 '18 at 13:50

1 Answers1

1
chrome.tabs.query({windowType:'normal'}, function(tabs) {
    console.log('Number of open tabs in all normal browser windows:',tabs.length);
}); 
аlex
  • 5,426
  • 1
  • 29
  • 38