Tab Detection closed (after closing) of the Firefox extension


I'm trying to get my firefox extension to create a url list from all tabs in the browser. To keep the list updated I need to know when a tab has been closed.

I've tried using

window.addEventListener("TabClose", tabRemoved, false);

However, this gets called BEFORE the tab is actually closed, which results in my updated tablist still containing the closed tabs url.

I update the tab list by iterating all browsers, like so:

function ()
    gBrowser = window.getBrowser();
    tabs = gBrowser.browsers;
    urls = [];

    for (var i = 0; i < tabs.length; i++)
        if (typeof(tabs[i]) != "undefined") {

    return urls;

So what I'm looking for is an event that gets called AFTER a tab has been closed, or a way to get the index of the tab that was closed so that I can skip it while iterating browsers.

Anyone know of any such event or other solutions to my problem?

Thanks in advance!

There is no special event for that - you simply catch the regular event and update the tab list delayed. Something along these lines:

var tabRemoveTimeout = null;
window.addEventListener("TabClose", tabRemoved, false);

function tabRemoved(event)
  // If we already scheduled an async action - cancel it
  if (tabRemoveTimeout)

  tabRemoveTimeout = window.setTimeout(function()
    tabRemoveTimeout = null;
  }, 0);

A less generic approach would be updating the list immediately but excluding the tab that is being closed.