Skip to content

Instantly share code, notes, and snippets.

@rpl
Last active January 18, 2016 21:03
Show Gist options
  • Save rpl/d72099ffddf53ae22265 to your computer and use it in GitHub Desktop.
Save rpl/d72099ffddf53ae22265 to your computer and use it in GitHub Desktop.
tabs.duplicate
duplicate: function(tabId, callback) {
let tab = TabManager.getTab(tabId);
// Ignore invalid tab ids.
if (!tab) {
if (callback) {
runSafe(context, callback, undefined);
}
return;
}
let gBrowser = tab.ownerDocument.defaultView.gBrowser;
let newTab = gBrowser.duplicateTab(tab);
gBrowser.moveTabTo(newTab, gBrowser.visibleTabs.indexOf(tab) + 1);
gBrowser.selectedTab = newTab;
gBrowser.updateCurrentBrowser(true);
// to be called if the addon has been disabled before
// the callback has been called
let cleanupOnClose = {
close: () => {
if (newTab) {
newTab.removeEventListener("SSTabRestored", onTabRestored);
}
},
};
// to be called once the duplicated tab is fully restored
let onTabRestored = () => {
// no need to cleanup on close anymore
context.forgetOnClose(cleanupOnClose);
//remove the one shot listener
newTab.removeEventListener("SSTabRestored", onTabRestored);
// these should both log the duplicated URI
dump(newTab.linkedBrowser.currentURI.spec)
dump('\n')
dump(tab.linkedBrowser.currentURI.spec)
dump('\n')
if (callback) {
runSafe(context, callback, TabManager.convert(extension, newTab));
}
};
// wait the SSTabRestored event
newTab.addEventListener("SSTabRestored", onTabRestored);
// prepare to cleanup the registered listener if the addon has been disabled
context.callOnClose(cleanupOnClose);
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment