MozillaZine

Reusing tabs for the same URL

From MozillaZine Knowledge Base

(Difference between revisions)
Revision as of 01:11, 29 March 2005
Grimholtz (Talk | contribs)
(missed a sentence)
<-- Previous diff
Revision as of 05:47, 29 March 2005
Asqueella (Talk | contribs)
(minor stylistic changes (reduce lines length))
Next diff -->
Line 5: Line 5:
<pre> <pre>
function openAndReuseOneTabPerURL(url) { function openAndReuseOneTabPerURL(url) {
- var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);+ var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
 + .getService(Components.interfaces.nsIWindowMediator);
var browserEnumerator = wm.getEnumerator("navigator:browser"); var browserEnumerator = wm.getEnumerator("navigator:browser");
Line 11: Line 12:
var found = false; var found = false;
while (browserEnumerator.hasMoreElements() && !found) { while (browserEnumerator.hasMoreElements() && !found) {
- var browserInstance = browserEnumerator.getNext().document.getElementById("content"); + var browserInstance = browserEnumerator.getNext();
 + browserInstance = browserInstance.getBrowser();
 + 
// Check each tab of this browser instance // Check each tab of this browser instance
- var currentTab, index = 0, numTabs = browserInstance.mPanelContainer.childNodes.length;+ var index = 0, numTabs = browserInstance.mPanelContainer.childNodes.length;
while (index < numTabs && !found) { while (index < numTabs && !found) {
- currentTab = browserInstance.getBrowserAtIndex(index);+ var currentTab = browserInstance.getBrowserAtIndex(index);
if (url == currentTab.currentURI.spec) { if (url == currentTab.currentURI.spec) {
// The URL is already opened. Select its tab. // The URL is already opened. Select its tab.

Revision as of 05:47, 29 March 2005

A common feature found in many extensions is to point the user to chrome:// URIs (e.g., help or about information) or external (on-line) HTML documents when he clicks an extension 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);
    }
  }
}