I work on an addon that observes all the http requests and populate some info corresponding to each tab. I want to clear the data for the tab which I close. However, I can't get to identiy tab close event in a cleaner/saner way.
So I have the contentWindow (LoadContext -> associatedWindow) for which the request was made, and the browser for that content window (which is for the tab).
- I realized that to do a
browser.addEventListener, I will have to listen forDOMNodeRemovedevent - which is deprecated as per MDN (and should rather use mutation observers). Moreover, I won't get the events when a popup window is closed. - If I do a
contentWindow.addEventListenerand look forunloadevent, the results are quite unpredictable. Some times, I don't get the events at all. - If I get this
tabelement fromgBrowser.tabsand listen forTabCloseevent, I can get all close events for the tab. But then again, it won't inform of any popup-window closes (although I do get atabelement for the popup-window)
So what is the easiest/cleanest way to know any tab/popup-window close ? I don't use the addon SDK, btw.
EDIT : Looking for a simpler, valid-for-all-cases solution. ie. I get notified of close event be it a tab or a popup-window (or any other types of windows if they exist!). I believe that might be possible given that I hold a browser element which contains the DOM window, and the DOM get removed (unloaded) upon a close.
EDIT-2 : Realized that all the problem was due to one thing : there is no TabClose event from Firefox when the last tab closes (which is a window close). Life would have been much easier if we were notified of the tabs that get closed when a window closes. Any ways, I will go with a modified version of @Noitidart's solution - to have a separate window listener, in addition to the TabClose listener. And find the tab elements in it and do my clean up. Sigh!
Will do a bit more search in this, and see if this qualifies for a bug/enhancement.
Thanks to @Noitidart and @Blargh for their suggestions.
TabOpenevent fired when a new browser window with tabs opens. Question though, what happens when a window closes with multiple tabs, I would think noTabCloseevent for any of those tabs huh? - NoitidartTabClosewhen a window closes. What I do is to look for all tabs in it and do my cleanup. In fact, I have an expando property for thebrowserelement per tab, and I use that to index my cached array. I can update my answer if you really want that. - user3337744