Created
February 10, 2013 13:41
-
-
Save doitian/4749616 to your computer and use it in GitHub Desktop.
openAndReuseOneTabPerURL firefox and chrome version
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function openAndReuseOneTabPerURL(url) { | |
window.chrome.tabs.query({url: url + '*'}, function (tabs) { | |
console.log(tabs); | |
if (tabs.length > 0) { | |
window.chrome.tabs.update(tabs[0].id, {active: true, highlighted: true}); | |
} else { | |
window.chrome.tabs.create({url: url, active: true}); | |
} | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://developer.mozilla.org/en-US/docs/Code_snippets/Tabbed_browser | |
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.gBrowser; | |
// 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 (currentBrowser.currentURI.spec.index(url) === 0) { | |
// The URL is already opened. Select this tab. | |
tabbrowser.selectedTab = tabbrowser.tabContainer.childNodes[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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment