-
-
Save Roy-Orbison/19bebdbdc5e1d44fd45f3e8782c81320 to your computer and use it in GitHub Desktop.
An inferior solution to retaining current tab's context for new tabs
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
let windowContexts = {}; | |
browser.windows.onRemoved.addListener(function(windowId) { | |
delete windowContexts[windowId]; | |
}); | |
(async function() { | |
try { | |
let actives = await browser.tabs.query({ | |
active: true | |
}); | |
if (actives?.length) { | |
actives.forEach(tab => windowContexts[tab.windowId] = tab.cookieStoreId); | |
} | |
} | |
catch (error) { | |
} | |
})(); | |
browser.tabs.onActivated.addListener(async function(activeInfo) { | |
try { | |
let tabInfo = await browser.tabs.get(activeInfo.tabId); | |
windowContexts[activeInfo.windowId] = tabInfo.cookieStoreId; | |
} | |
catch (error) { | |
} | |
}); | |
browser.tabs.onCreated.addListener(function(tab) { | |
if ( | |
tab.cookieStoreId === 'firefox-default' | |
&& tab.url === 'about:newtab' | |
&& windowContexts[tab.windowId] | |
&& tab.cookieStoreId !== windowContexts[tab.windowId] | |
&& !tab.incognito | |
&& !tab.hidden | |
&& tab.active | |
&& tab.id !== browser.tabs.TAB_ID_NONE | |
) { | |
let tabRetained = { | |
cookieStoreId: windowContexts[tab.windowId], | |
active: tab.active, | |
index: tab.index, | |
windowId: tab.windowId, | |
openerTabId: tab.openerTabId | |
}; | |
browser.tabs.remove(tab.id); | |
browser.tabs.create(tabRetained); | |
return { | |
cancel: true | |
}; | |
} | |
}); |
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
{ | |
"browser_specific_settings": { | |
"gecko": { | |
"strict_min_version": "58.0a1" | |
} | |
}, | |
"background": { | |
"scripts": [ | |
"background.js" | |
] | |
}, | |
"description": "Retain current container when creating new tabs.", | |
"manifest_version": 2, | |
"name": "Retainer", | |
"permissions": [ | |
"tabs", | |
"cookies" | |
], | |
"version": "0.1.0" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is now a proper add-on: https://addons.mozilla.org/en-US/firefox/addon/retainer/
(@rickybrent)