-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
223 lines (208 loc) · 7.26 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var config = Object();
/*global browser*/
const contextNameMap = new Object();
function updateContextNameMap() {
browser.contextualIdentities.query({}).then((contexts) => {
for (let context of contexts) {
contextNameMap[context.cookieStoreId] = context.name;
}
});
}
updateContextNameMap();
if (!browser.contextualIdentities.onUpdated.hasListener(updateContextNameMap))
browser.contextualIdentities.onUpdated.addListener(updateContextNameMap);
function restoreOptions() {
browser.storage.sync.get().then((res) => {
config = res;
});
}
function isPlaceholder(url) {
// proper check for published add-on
if (
url.startsWith(
`moz-extension://${browser.runtime.id.slice(1, -1)}/placeholder.html`
)
)
return true;
// hacky check when debugging
if (
url.startsWith("moz-extension://") &&
url.includes("/placeholder.html?title=")
)
return true;
return false;
}
document.addEventListener("DOMContentLoaded", restoreOptions);
browser.storage.onChanged.addListener(restoreOptions);
browser.tabs.onActivated.addListener((activeInfo) => {
browser.tabs
.get(activeInfo.tabId)
.then(function (tabInfo) {
console.log("----");
console.log(tabInfo);
if (isPlaceholder(tabInfo.url)) {
browser.tabs
.remove(tabInfo.id)
.catch((error) => {
console.log(error);
})
.then(() => console.log("Removing selected placeholder."));
return;
}
let selected = tabInfo.cookieStoreId; // id for the current group
let storesShown = {}; // tracks whether we have seen tabs in this group yet.
let tabInGroup = {}; // maps a store to a valid tab ID
// value is whether we have already shown the placeholder tab
let hidSomething = false;
let toHide = []; // tabs currently shown which we want to hide
let foreignTabs = []; // tabs to show despite not being in this group
let currentGroup = []; // tabs in the current group
browser.tabs
.query({ currentWindow: true })
.then((tabs) => {
if (config.sortTabs)
tabs = tabs.sort((a, b) => a.lastAccessed - b.lastAccessed);
for (let tab of tabs) {
let showMe = false;
let cxName = contextNameMap[tab.cookieStoreId] || "default";
let is_placeholder = tab.title === cxName;
console.log(tab.title + " [" + tab.id + "], " + is_placeholder);
// is this a placeholder page, but the title is not what it should be?
if (isPlaceholder(tab.url) && !is_placeholder) {
browser.tabs
.remove(tab.id)
.catch((error) => {
console.log(error);
})
.then(() => console.log("Removing broken placeholder."));
if (tabInfo.id === tab.id) {
console.log("current tab is broken, and is being removed.");
return;
}
continue;
}
if (tab.cookieStoreId === selected && !is_placeholder) {
showMe = true;
currentGroup.push(tab.id);
} else if (tab.cookieStoreId !== selected) {
if (!storesShown[tab.cookieStoreId]) {
storesShown[tab.cookieStoreId] = false;
tabInGroup[tab.cookieStoreId] = tab.id;
}
if (is_placeholder) {
storesShown[tab.cookieStoreId] = true;
showMe = true;
foreignTabs.push(tab.id);
}
}
if (tab.hidden && showMe)
browser.tabs
.show([tab.id])
.catch((error) => {
console.log(error);
})
.then(() =>
console.log(
"Showing " +
tab.title +
" (" +
cxName +
") [" +
tab.id +
"]"
)
);
if (!tab.hidden && !showMe) {
if (is_placeholder && tab.id === tabInfo.id) {
// we can't hide the currently-selected window, so
// we will have to destroy it instead
browser.tabs
.remove(tab.id)
.catch((error) => {
console.log(error);
})
.then(() =>
console.log(
"Removing " +
tab.title +
" (" +
cxName +
") [" +
tab.id +
"]"
)
);
console.log("current tab is selected, and is being removed.");
return;
} else {
hidSomething = true;
browser.tabs
.hide([tab.id])
.catch((error) => {
console.log(error);
})
.then(() =>
console.log(
"Hiding " +
tab.title +
" (" +
cxName +
") [" +
tab.id +
"]"
)
);
}
}
}
// are there some tab groups which don't have a placeholder?
// If so, make them
for (const cookieStoreId in storesShown) {
if (!storesShown[cookieStoreId]) {
let cxName = contextNameMap[cookieStoreId] || "default";
browser.tabs
.create({
index: 0,
openerTabId: tabInGroup[cookieStoreId],
title: cxName,
discarded: true,
url: `/placeholder.html?title=${encodeURIComponent(cxName)}`,
cookieStoreId: cookieStoreId,
})
.then((newTab) => {
console.log(
`Created new tab ${newTab.id} for ` +
cxName +
" with opener/succeeder " +
tabInGroup[cookieStoreId]
);
browser.tabs.moveInSuccession([
newTab.id,
tabInGroup[cookieStoreId],
]);
});
}
}
if (hidSomething && config.moveForeignTabs && foreignTabs.length > 0)
browser.tabs
.move(foreignTabs, { index: 0 })
.catch(() => {})
.then(() => console.log("Moved tabs to left: " + foreignTabs));
// if we have just switched groups, highlight those in the current group
if (config.highlightTabs && toHide.length > 0) {
browser.tabs
.highlight({
populate: false,
tabs: [...Array(tabs.length).keys()].slice(foreignTabs.length),
})
.catch(() => {});
}
})
.catch(() => {
// problem querying tabs. ignore it.
});
})
.catch((error) => {
console.error(error);
});
});