From 5e142b2070e2e5fa698b63e47c25f6633e2f4193 Mon Sep 17 00:00:00 2001 From: joey Date: Sun, 29 Mar 2020 17:55:45 -0400 Subject: [PATCH 1/4] draft implementation of reorganizing tabs for background logic --- src/js/background/backgroundLogic.js | 45 ++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/src/js/background/backgroundLogic.js b/src/js/background/backgroundLogic.js index d68e51e..253ea3e 100644 --- a/src/js/background/backgroundLogic.js +++ b/src/js/background/backgroundLogic.js @@ -237,6 +237,51 @@ const backgroundLogic = { return identitiesOutput; }, + async reorganize() { + let windows = await browser.windows.getAll(); + let containers = new Set(); + for (const windowObj of windows) { + const tabs = await browser.tabs.query({windowId: windowObj.id}); + for (const tab of tabs) { + containers.add(tab.cookieStoreId); + } + } + containers = Array.from(containers); + + for (let i = 0; i < containers.length; i++) { + const windowId = (i < windows.length) ? windows[i].id : -1; + await this._reorganizeInternal(windowId, containers[i]); + } + windows = await browser.windows.getAll(); + for (let i = containers.length; i < windows.length; i++) { + await browser.windows.remove(windows[i].id); + } + }, + + async _reorganizeInternal(windowId, cookieStoreId) { + let createNew = false; + let tabs = await browser.tabs.query({ + "cookieStoreId": cookieStoreId + }); + if (windowId === -1) { + const newWindowObj = await browser.windows.create(); + windowId = newWindowObj.id; + createNew = true; + } + browser.tabs.move(tabs.map((tab) => tab.id), { + windowId: windowId, + index: -1 + }); + if (createNew) { + tabs = await browser.tabs.query({windowId: windowId}); + for (let tab of tabs) { // eslint-disable-line prefer-const + if (tab.cookieStoreId !== cookieStoreId) { + browser.tabs.remove(tab.id); + } + } + } + }, + async sortTabs() { const windows = await browser.windows.getAll(); for (let windowObj of windows) { // eslint-disable-line prefer-const From 84bfe683c5d73a72dca06479970a39f5c7d8445d Mon Sep 17 00:00:00 2001 From: joey Date: Sun, 29 Mar 2020 23:38:20 -0400 Subject: [PATCH 2/4] renamed the background --- src/js/background/backgroundLogic.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/js/background/backgroundLogic.js b/src/js/background/backgroundLogic.js index 253ea3e..93be0fc 100644 --- a/src/js/background/backgroundLogic.js +++ b/src/js/background/backgroundLogic.js @@ -237,7 +237,7 @@ const backgroundLogic = { return identitiesOutput; }, - async reorganize() { + async sortTabsByWindow() { let windows = await browser.windows.getAll(); let containers = new Set(); for (const windowObj of windows) { @@ -250,7 +250,7 @@ const backgroundLogic = { for (let i = 0; i < containers.length; i++) { const windowId = (i < windows.length) ? windows[i].id : -1; - await this._reorganizeInternal(windowId, containers[i]); + await this._sortTabsByWindowInternal(windowId, containers[i]); } windows = await browser.windows.getAll(); for (let i = containers.length; i < windows.length; i++) { @@ -258,7 +258,7 @@ const backgroundLogic = { } }, - async _reorganizeInternal(windowId, cookieStoreId) { + async _sortTabsByWindowInternal(windowId, cookieStoreId) { let createNew = false; let tabs = await browser.tabs.query({ "cookieStoreId": cookieStoreId @@ -274,7 +274,7 @@ const backgroundLogic = { }); if (createNew) { tabs = await browser.tabs.query({windowId: windowId}); - for (let tab of tabs) { // eslint-disable-line prefer-const + for (const tab of tabs) { if (tab.cookieStoreId !== cookieStoreId) { browser.tabs.remove(tab.id); } From fd793b719593df19a11d523cc04dd31d08a95391 Mon Sep 17 00:00:00 2001 From: joey Date: Tue, 31 Mar 2020 23:00:41 -0400 Subject: [PATCH 3/4] record the newly open tab id if new window is craeted, which makes delete easier --- src/js/background/backgroundLogic.js | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/src/js/background/backgroundLogic.js b/src/js/background/backgroundLogic.js index 93be0fc..67b8350 100644 --- a/src/js/background/backgroundLogic.js +++ b/src/js/background/backgroundLogic.js @@ -259,27 +259,29 @@ const backgroundLogic = { }, async _sortTabsByWindowInternal(windowId, cookieStoreId) { - let createNew = false; - let tabs = await browser.tabs.query({ + let newlyOpenedTabId = null; + const tabs = await browser.tabs.query({ "cookieStoreId": cookieStoreId }); + + // create a new window so that move tabs to the new window if (windowId === -1) { const newWindowObj = await browser.windows.create(); windowId = newWindowObj.id; - createNew = true; + newlyOpenedTabId = newWindowObj.tabs[0].id; } + + // move all tabs browser.tabs.move(tabs.map((tab) => tab.id), { windowId: windowId, index: -1 }); - if (createNew) { - tabs = await browser.tabs.query({windowId: windowId}); - for (const tab of tabs) { - if (tab.cookieStoreId !== cookieStoreId) { - browser.tabs.remove(tab.id); - } - } + + // if new window is created, then close newly opened tab by its id + if (newlyOpenedTabId !== null) { + browser.tabs.remove(newlyOpenedTabId); } + }, async sortTabs() { From 4df51a9790b6b5545f9ce43e4c2c0209758cec7f Mon Sep 17 00:00:00 2001 From: joey Date: Wed, 1 Apr 2020 14:34:01 -0400 Subject: [PATCH 4/4] added comments --- src/js/background/backgroundLogic.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/js/background/backgroundLogic.js b/src/js/background/backgroundLogic.js index 67b8350..de9f402 100644 --- a/src/js/background/backgroundLogic.js +++ b/src/js/background/backgroundLogic.js @@ -240,6 +240,8 @@ const backgroundLogic = { async sortTabsByWindow() { let windows = await browser.windows.getAll(); let containers = new Set(); + + // loop through each tab to find all active containers for (const windowObj of windows) { const tabs = await browser.tabs.query({windowId: windowObj.id}); for (const tab of tabs) { @@ -248,10 +250,16 @@ const backgroundLogic = { } containers = Array.from(containers); + // for each container, move all related tabs to its window for (let i = 0; i < containers.length; i++) { + // if no window is available, then pass -1 const windowId = (i < windows.length) ? windows[i].id : -1; await this._sortTabsByWindowInternal(windowId, containers[i]); } + + // Logically, there shouldn't be any redundant windows + // but, maybe other addons/features conflicts, so double check to + // delete redundant windows here. windows = await browser.windows.getAll(); for (let i = containers.length; i < windows.length; i++) { await browser.windows.remove(windows[i].id);