Skip to content

Instantly share code, notes, and snippets.

@Roy-Orbison
Created May 4, 2021 04:22
Show Gist options
  • Save Roy-Orbison/19bebdbdc5e1d44fd45f3e8782c81320 to your computer and use it in GitHub Desktop.
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
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
};
}
});
{
"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"
}
@Roy-Orbison
Copy link
Author

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