Listening tabs open / close and deal with existing tabs

I try to do something every time a new tab opens, starting with firefox starting and when a new tab is added after starting Firefox. I follow an example:

https://developer.mozilla.org/en/Code_snippets/Tabbed_browser

So, I have a

var container = gBrowser.tabContainer; container.addEventListener("TabOpen", tabAdded, false); container.addEventListener("TabClose", tabClosed, false); function tabAdded(event) { alert("tabAdded!"); var browser = gBrowser.getBrowserForTab(event.target); browser.pollingService = new PollingService(createGuid()); browser.pollingService.start(); } 

And I have a similar function to close. This works great when the tabs are actually open / close, but I ran into a few problems.

First, when Firefox opens, it has already opened the initial tab, but the tabAdded event never fires. Similarly, when I close firefox, it never launches TabClose for these tabs.

It seems that the right thing in this case is to go through all the tabs that are in gBrowser.tabContainer and add my service to them, and also do something similar when Firefox closes. Unfortunately, I'm not quite sure how to connect to find out when Firefox closes (it is also possible there is a much better way to handle this, but I can’t think about it).

Secondly, gBrowser.tabContainer can be uninitialized sometimes when my script initialization is executed; Is there a specific event that I should listen to know when I can safely add listeners to the tabContainer?

+4
source share
1 answer

Use the boot event listener to give window time for you so you can add Tab event listeners and create a polling service for an existing tab. Then use the unload event listener to perform the cleanup.

+4
source

Source: https://habr.com/ru/post/1341877/


All Articles