Reusing tabs for the same URL: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
m (added note that technique doesn't work with Mozilla Suite)
No edit summary
Line 1: Line 1:
{{extdev}}
A common feature found in many extensions is to point the user to <code>chrome://</code> URIs in a browser window (e.g., help or about information) or external (on-line <code>http(s)://</code>) HTML documents when he clicks an extension's button or link.
A common feature found in many extensions is to point the user to <code>chrome://</code> URIs in a browser window (e.g., help or about information) or external (on-line <code>http(s)://</code>) HTML documents when he clicks an extension's button or link.


Line 43: Line 45:
   }
   }
}
}
</pre>
Sometimes re-using an open tab is not a question of the tabs URI, or additional data is assigned to the tab for later use. In this case, the approach presented above can be slightly modified.
Instead of opening the new tab via <code>delayedOpenTab</code> which doesn't return the new tab, the new tab can be created and manipulate manually:
<pre>
...
if (!found) {
  // get fresh iterator
  var browserEnumerator = wm.getEnumerator('navigator:browser');
  var browserInstance = browserEnumerator.getNext().getBrowser();
 
  // create tab
  var newTab = browserInstance.addTab(url);
  newTab.setAttribute("some-data", "xyz");
 
  // focus tab
  browserInstance.selectedTab = newTab;
}
</pre>
Now, searching for the tab is just as easy- but requires <code>mTabContainter</code> instead of <code>mPanelContainer</code> for accessing the browser tab:
<pre>
...
  while (index < browserInstance.mTabContainer.childNodes.length && !found)
  {
    // get the tab
    var currentTab = browserInstance.mTabContainer.childNodes[index];
 
    // does the tab contain the assigned data?
    if (currentTab.getAttribute("some-data") == "xyz") {
      // tab found- select and focus
      ...
      found = true;
  }
...
</pre>
</pre>



Revision as of 13:32, 9 December 2005

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

A common feature found in many extensions is to point the user to chrome:// URIs in a browser window (e.g., help or about information) or external (on-line http(s)://) HTML documents when he clicks an extension's button or link.

Rather than open a new browser or new tab each and every time the same URI is needed, it is better practice to re-use an existing tab which already displays the desired URI, if one exists. Following this practice minimizes the proliferation of tabs and browsers created by your extension. The following code demonstrates how to do this.

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 (browserEnumerator.hasMoreElements() && !found) {
    var browserInstance = browserEnumerator.getNext();
    browserInstance = browserInstance.getBrowser();

    // Check each tab of this browser instance
    var index = 0, numTabs = browserInstance.mPanelContainer.childNodes.length;
    while (index < numTabs && !found) {
      var currentTab = browserInstance.getBrowserAtIndex(index);
      if (url == currentTab.currentURI.spec) {
        // The URL is already opened. Select its tab.
        browserInstance.selectedTab = currentTab;
        // Focus *this* browser
        browserInstance.focus();
        found = true;
      }
      index++;
    }
  }

  // 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);
    }
    else {
      // No browser windows are open, so open a new one.
      window.open(url);
    }
  }
}

Sometimes re-using an open tab is not a question of the tabs URI, or additional data is assigned to the tab for later use. In this case, the approach presented above can be slightly modified.

Instead of opening the new tab via delayedOpenTab which doesn't return the new tab, the new tab can be created and manipulate manually:

...
if (!found) {
  // get fresh iterator
  var browserEnumerator = wm.getEnumerator('navigator:browser');
  var browserInstance = browserEnumerator.getNext().getBrowser(); 
  
  // create tab
  var newTab = browserInstance.addTab(url);
  newTab.setAttribute("some-data", "xyz");
  
  // focus tab
  browserInstance.selectedTab = newTab;
}

Now, searching for the tab is just as easy- but requires mTabContainter instead of mPanelContainer for accessing the browser tab:

...
  while (index < browserInstance.mTabContainer.childNodes.length && !found)
  {
    // get the tab
    var currentTab = browserInstance.mTabContainer.childNodes[index];
  
    // does the tab contain the assigned data?
    if (currentTab.getAttribute("some-data") == "xyz") {
      // tab found- select and focus
      ...
      found = true;
  }
...

This code uses techniques discussed in Enumerating tabbrowser tabs and Opening a new browser window. It does not work with the Mozilla Suite; only Firefox and Thunderbird.