Fix #2747: open/reopen container tabs in tab groups when appropriate

Firefox 137 introduced tab groups. Tab group web extension support is rolling out in Firefox 138 and later. Creating a new tab after the last tab in a tab group can inadvertently create the new tab outside of the tab group.

When a user opens a new tab that should be in a container, this patch will make sure that the new tab resides in the same tab group as the original tab.
This commit is contained in:
Stephen Thompson
2025-04-21 23:35:28 -04:00
parent a60f5bb1be
commit b6a1bff9e8
5 changed files with 122 additions and 26 deletions
+22 -7
View File
@@ -1,3 +1,11 @@
/**
* Firefox does not yet have the `tabGroups` API, which exposes this constant
* to indicate that a tab is not in a tab group. But the Firefox `tabs` API
* currently returns this constant value for `Tab.groupId`.
* @see https://searchfox.org/mozilla-central/rev/3b95c8dbe724b10390c96c1b9dd0f12c873e2f2e/browser/components/extensions/schemas/tabs.json#235
*/
const TAB_GROUP_ID_NONE = -1;
async function load() {
const searchParams = new URL(window.location).searchParams;
const redirectUrl = searchParams.get("url");
@@ -69,11 +77,15 @@ function confirmSubmit(redirectUrl, cookieStoreId) {
openInContainer(redirectUrl, cookieStoreId);
}
function getCurrentTab() {
return browser.tabs.query({
/**
* @returns {Promise<Tab>}
*/
async function getCurrentTab() {
const tabs = await browser.tabs.query({
active: true,
windowId: browser.windows.WINDOW_ID_CURRENT
});
return tabs[0];
}
async function denySubmit(redirectUrl, currentCookieStoreId) {
@@ -93,7 +105,7 @@ async function denySubmit(redirectUrl, currentCookieStoreId) {
await browser.runtime.sendMessage({
method: "exemptContainerAssignment",
tabId: tab[0].id,
tabId: tab.id,
pageUrl: redirectUrl
});
document.location.replace(redirectUrl);
@@ -103,12 +115,15 @@ load();
async function openInContainer(redirectUrl, cookieStoreId) {
const tab = await getCurrentTab();
await browser.tabs.create({
index: tab[0].index + 1,
const reopenedTab = await browser.tabs.create({
index: tab.index + 1,
cookieStoreId,
url: redirectUrl
});
if (tab.length > 0) {
browser.tabs.remove(tab[0].id);
if (tab.groupId && tab.groupId !== TAB_GROUP_ID_NONE && browser.tabs.group) {
// If the original tab was in a tab group, make sure that the reopened tab
// stays in the same tab group.
browser.tabs.group({ groupId: tab.groupId, tabIds: reopenedTab.id });
}
browser.tabs.remove(tab.id);
}