views:

580

answers:

3

Hi guys. I followed a tutorial on reusing tabs inside Firefox, and right now my extension does it well. However, I also need to reuse tabs that are opened from outside (from some application, start menu etc.). How do I do this?

I tried adding an event listener for TabOpen event, but when I log the

event.target.linkedBrowser.currentURI.spec

it's value is "about:blank". I expected the actual address that I typed into the address bar (automatically opens in a new tab), or the address that I open from some other application, so I can close that tab immediately, and the focus the right one. What am I doing wrong?

Thanks in advance.


Just in case, here's the code that reuses the tab when a new tab is requested from an extension

function openAndReuseOneTabPerURL(url) {
  var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
                     .getService(Components.interfaces.nsIWindowMediator);
  var browserEnumerator = wm.getEnumerator("navigator:browser");

  // Check each browser instance for our URL
  var found = false;
  while (!found && browserEnumerator.hasMoreElements()) {
    var browserWin = browserEnumerator.getNext();
    var tabbrowser = browserWin.getBrowser();

    // Check each tab of this browser instance
    var numTabs = tabbrowser.browsers.length;
    for(var index=0; index<numTabs; index++) {
      var currentBrowser = tabbrowser.getBrowserAtIndex(index);
      if (url == currentBrowser.currentURI.spec) {

        // The URL is already opened. Select this tab.
        tabbrowser.selectedTab = tabbrowser.mTabs[index];

        // Focus *this* browser-window
        browserWin.focus();

        found = true;
        break;
      }
    }
  }

  // Our URL isn't open. Open it now.
  if (!found) {
    var recentWindow = wm.getMostRecentWindow("navigator:browser");
    if (recentWindow) {
      // Use an existing browser window
      recentWindow.delayedOpenTab(url, null, null, null, null);
    }
    else {
      // No browser windows are open, so open a new one.
      window.open(url);
    }
  }
}
A: 

I guess you could extract the wanted behaviour from the implementation in the Tab Mix Plus extension

jitter
+2  A: 

When you receive the TabOpen event, it's too early for the page content to be loaded still. When you receive the TabOpen event, however, you should register for load or DOMContentLoaded. When you receive that event, you should be able to access the URI.

sdwilsh
Thanks, I figured out it might be a possible solution. It's not what I was hoping for, since the tab is open for a short time (right before I close it if another tab with the same address exists).
ms
It should be pretty quick. about:blank will go away very very fast. You'll want to use DOMContentLoaded if you are concerned about speed.
sdwilsh
Exactly what I've done, but still, I can see that tab for a short amount of time. What I really need is some kind of "PreTabOpen" event (: I got another advice on how it could be done, so I'll try that before asking for more help here.
ms
A: 

How is the tab closing event? I tried pagehide and unload but it's just for the window, not the tab...

The tab close event is called "TabClose", and you add an event listener on a tab container. More details here : https://developer.mozilla.org/en/Code_snippets/Tabbed_browser
ms