Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noitidart/9b335a460f53b0390336 to your computer and use it in GitHub Desktop.
Save Noitidart/9b335a460f53b0390336 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-SwapBrowsersAndDONTCloseOther -
var tabIdToReplace = 'panel-3-248';
var tabIdToReplaceWith = 'panel-3-249';
var aTabToReplace = null;
var aTabToReplaceWith = null;
function swapBrowsersAndDONTCloseOther(aOurTab, aOtherTab) {
console.log('here, this:', this);
// Do not allow transfering a private tab to a non-private window
// and vice versa.
var window = aOurTab.ownerDocument.defaultView;
if (window.PrivateBrowsingUtils.isWindowPrivate(window) !=
window.PrivateBrowsingUtils.isWindowPrivate(aOtherTab.ownerDocument.defaultView))
return;
console.log('h1');
// That's gBrowser for the other window, not the tab's browser!
var remoteBrowser = aOtherTab.ownerDocument.defaultView.gBrowser;
var isPending = aOtherTab.hasAttribute("pending");
/*
// First, start teardown of the other browser. Make sure to not
// fire the beforeunload event in the process. Close the other
// window if this was its last tab.
if (!remoteBrowser._beginRemoveTab(aOtherTab, true, true))
return;
*/
let ourBrowser = this.getBrowserForTab(aOurTab);
let otherBrowser = aOtherTab.linkedBrowser;
// If the other tab is pending (i.e. has not been restored, yet)
// then do not switch docShells but retrieve the other tab's state
// and apply it to our tab.
if (isPending) {
window.SessionStore.setTabState(aOurTab, SessionStore.getTabState(aOtherTab));
// Make sure to unregister any open URIs.
this._swapRegisteredOpenURIs(ourBrowser, otherBrowser);
} else {
// Workarounds for bug 458697
// Icon might have been set on DOMLinkAdded, don't override that.
if (!ourBrowser.mIconURL && otherBrowser.mIconURL)
this.setIcon(aOurTab, otherBrowser.mIconURL);
var isBusy = aOtherTab.hasAttribute("busy");
if (isBusy) {
aOurTab.setAttribute("busy", "true");
this._tabAttrModified(aOurTab);
if (aOurTab.selected)
this.mIsBusy = true;
}
this._swapBrowserDocShells(aOurTab, otherBrowser);
}
console.log('h2');
// Handle findbar data (if any)
let otherFindBar = aOtherTab._findBar;
if (otherFindBar &&
otherFindBar.findMode == otherFindBar.FIND_NORMAL) {
let ourFindBar = this.getFindBar(aOurTab);
ourFindBar._findField.value = otherFindBar._findField.value;
if (!otherFindBar.hidden)
ourFindBar.onFindCommand();
}
/*
// Finish tearing down the tab that's going away.
remoteBrowser._endRemoveTab(aOtherTab);
*/
console.log('h3');
if (isBusy)
this.setTabTitleLoading(aOurTab);
else
this.setTabTitle(aOurTab);
// If the tab was already selected (this happpens in the scenario
// of replaceTabWithWindow), notify onLocationChange, etc.
if (aOurTab.selected)
this.updateCurrentBrowser(true);
console.log('succ done');
}
Cu.import('resource://gre/modules/Services.jsm');
var DOMWindows = Services.wm.getEnumerator('navigator:browser');
while (DOMWindows.hasMoreElements()) {
var aDOMWindow = DOMWindows.getNext();
if (aDOMWindow.gBrowser && aDOMWindow.gBrowser.tabContainer) {
var tabs = aDOMWindow.gBrowser.tabContainer.childNodes;
for (var i = 0; i < tabs.length; i++) {
var aTab = tabs[i];
var aTabId = aTab.getAttribute('linkedpanel');
console.log('tab id:', aTabId);
if (aTabId == tabIdToReplace) {
aTabToReplace = aTab;
} else if (aTabId == tabIdToReplaceWith) {
aTabToReplaceWith = aTab;
}
if (aTabToReplace && aTabToReplaceWith) {
console.log('both found');
console.log('swap them now');
swapBrowsersAndDONTCloseOther.bind(aTabToReplace.ownerDocument.defaultView.gBrowser, aTabToReplace, aTabToReplaceWith)();
break;
}
}
} else {
//this window does not have any tabs
}
}
@Noitidart
Copy link
Author

README

Rev1

  • I was going about overriding the _beginRemove and _endRemove but realized that's not the way to go as we have to do the swaps to the other tab

Rev2

  • Works but I need to swap to the tab that gets closed (aTabToReplaceWith)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment