-
Notifications
You must be signed in to change notification settings - Fork 5
/
background.js
67 lines (63 loc) · 1.84 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
class windowManager {
constructor() {
browser.contextMenus.onClicked.addListener(() => {
this.merge();
});
browser.windows.onFocusChanged.addListener(() => {
this.calculateContextMenu();
});
this.calculateContextMenu();
}
async getCurrentWindows() {
const currentWindow = await browser.windows.getCurrent();
const windows = await browser.windows.getAll({});
return windows.filter((windowObj) => {
return windowObj.incognito === currentWindow.incognito;
});
}
async calculateContextMenu() {
const windows = await this.getCurrentWindows();
const id = "merge-windows";
browser.contextMenus.remove(id);
if (windows.length > 1) {
browser.contextMenus.create({
id,
title: "Merge all windows",
contexts: ["all", "tab"]
});
}
}
async merge() {
const windowMap = new Map();
const windows = await this.getCurrentWindows();
let biggestCount = 0;
let biggest = null;
let repin = [];
const promises = windows.map(async function (windowObj) {
const tabs = await browser.tabs.query({windowId: windowObj.id});
windowMap.set(windowObj, tabs.map((tab) => {
if (tab.pinned) {
repin.push(browser.tabs.update(tab.id, {pinned: false}));
}
return tab.id;
}));
if (tabs.length > biggestCount) {
biggest = windowObj;
biggestCount = tabs.length;
}
});
await Promise.all(promises);
const repinTabs = await Promise.all(repin);
windows.forEach((windowObj) => {
if (windowObj === biggest) {
return;
}
browser.tabs.move(windowMap.get(windowObj), {index: -1, windowId: biggest.id});
});
repinTabs.forEach((tab) => {
browser.tabs.update(tab.id, {pinned: true});
});
this.calculateContextMenu();
}
};
new windowManager();