forked from jonathanKingston/new-container-tab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
117 lines (110 loc) · 2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/
*
* © 2017 Jonathan Kingston
* © 2021 Roy Orbison
* */
class WindowContainers {
set(tab) {
if (
tab.cookieStoreId
&& tab.windowId
&& tab.cookieStoreId !== containerNone
) {
this[tab.windowId] = tab.cookieStoreId;
}
}
}
const containers = new WindowContainers;
const containerNone = 'firefox-default';
browser.windows.onRemoved.addListener(windowId => delete containers[windowId]);
(async function() {
try {
const actives = await browser.tabs.query({
active: true
});
if (actives?.length) {
actives.forEach(async function(tab) {
if (tab.windowId && tab.cookieStoreId == containerNone) {
const others = await browser.tabs.query({
windowId: tab.windowId
});
if (others?.length) {
let counts = {},
stable = [];
others.forEach(tabOther => {
if (tabOther.cookieStoreId && tabOther.cookieStoreId != containerNone) {
stable[tabOther.index] = tabOther.cookieStoreId
if (counts[tabOther.cookieStoreId]) {
counts[tabOther.cookieStoreId].count++;
}
else {
counts[tabOther.cookieStoreId] = {
count: 1,
oneOf: tabOther
};
}
}
});
if (stable.length) {
stable = stable.reduce((containerLast, containerCurrent) =>
containerLast
&& (
!containerCurrent
|| counts[containerLast].count > counts[containerCurrent].count
) ?
containerLast :
containerCurrent);
tab = counts[stable].oneOf;
}
}
}
containers.set(tab);
});
}
}
catch (error) {
}
})();
browser.tabs.onActivated.addListener(async function(activeInfo) {
try {
const tab = await browser.tabs.get(activeInfo.tabId);
containers.set(tab);
}
catch (error) {
}
});
browser.commands.onCommand.addListener(async function(command) {
if (command === 'retainer-tab') {
try {
const tabs = await browser.tabs.query({
currentWindow: true,
active: true
});
if (tabs?.length && tabs[0].id != browser.tabs.TAB_ID_NONE) {
const exclKey = 'excludeDefault',
res = await browser.storage.sync.get(exclKey),
excl = +(res && res[exclKey] || 0),
mruCookieStoreId = containers[tabs[0].windowId];
if (excl < 2 || mruCookieStoreId) {
browser.tabs.create({
cookieStoreId: excl && mruCookieStoreId || tabs[0].cookieStoreId
});
}
else {
browser.notifications.create(
'no-container',
{
type: 'basic',
title: 'No container',
message: "You must open at least one contained tab to set the window's most recently used container."
}
);
}
}
}
catch (error) {
}
}
});