Merge pull request #14 from CSCD01/issue-1607-background-logic

Issue 1607 background logic
This commit is contained in:
JoeyHuang 2020-04-02 15:45:28 -04:00 committed by GitHub
commit 8ffc33c636
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -237,6 +237,61 @@ const backgroundLogic = {
return identitiesOutput;
},
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) {
containers.add(tab.cookieStoreId);
}
}
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);
}
},
async _sortTabsByWindowInternal(windowId, cookieStoreId) {
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;
newlyOpenedTabId = newWindowObj.tabs[0].id;
}
// move all tabs
browser.tabs.move(tabs.map((tab) => tab.id), {
windowId: windowId,
index: -1
});
// if new window is created, then close newly opened tab by its id
if (newlyOpenedTabId !== null) {
browser.tabs.remove(newlyOpenedTabId);
}
},
async sortTabs() {
const windows = await browser.windows.getAll();
for (let windowObj of windows) { // eslint-disable-line prefer-const