Last active
August 29, 2015 14:10
-
-
Save Noitidart/9b335a460f53b0390336 to your computer and use it in GitHub Desktop.
_ff-addon-snippet-SwapBrowsersAndDONTCloseOther -
This file contains 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
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
README
Rev1
Rev2