-
Notifications
You must be signed in to change notification settings - Fork 1
/
background.js
218 lines (194 loc) · 6.64 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
/* temporary container for each tab
Principle: each time a new tab is open, the extension close it quickly
and then open it in a new temporary container. When a tab is close we check
if there are container that are not used by any tabs and remove them.
*/
const colors = ["blue", "turquoise", "green", "yellow", "orange", "red", "pink", "purple"];
const containerPrefix = "TC";
const containerIcon = "fingerprint";
// ideally put this code in a separate script to be accessible by context.js too
class TabContainer {
constructor() {
this.counter = 0; // number of container open during session
}
getColor(a) { // return color from global 'colors' variable
return colors[a % colors.length];
}
onError(e) {
console.error(e);
}
createNew(url, active = true, pinned = false, index = undefined) { // closure
const self = this;
if (url.slice(0, 5) === "about") {
url = null;
}
return new Promise(function (resolve, reject) {
self.counter++;
const color = self.getColor(self.counter);
browser.contextualIdentities.create({
name: containerPrefix + self.counter,
color: color,
icon: containerIcon
}).then(
function (context) {
console.log(`New TC: ${context.cookieStoreId}.`);
browser.tabs.create({
cookieStoreId: context.cookieStoreId,
url: url,
active: active,
pinned: pinned,
index: index
}).then(
(tab) => {
resolve(tab.id)
},
self.onError
);
}, self.onError);
})
}
cleaning() {
browser.contextualIdentities.query({}).then((contexts) => {
this.onGot(contexts); // delete all contexts
}, this.onError)
}
onGot(contexts) {
for (let context of contexts) {
if (context.name.slice(0, 2) === containerPrefix) {
browser.contextualIdentities.remove(context.cookieStoreId);
console.log("onGot: remaining container found and deleted");
}
}
}
checkUnusedContainer(tabId) {
const self = this;
async function check() {
console.log("checkUnusedContainer")
const contexts = await browser.contextualIdentities.query({})
for (let context of contexts) {
if (context.name.slice(0, 2) !== containerPrefix) {
continue;
}
const cookieStoreId = context.cookieStoreId
const tabs = await browser.tabs.query({
"cookieStoreId": cookieStoreId
})
if (tabs.length === 0) { // not tabs are using the context so we can remove it
browser.contextualIdentities.remove(cookieStoreId).then(
() => {
console.log(cookieStoreId, "deleted")
},
() => {
console.log(cookieStoreId, "already removed")
})
} else if (tabs.length === 1) {
if (tabs[0].id === tabId) {
browser.contextualIdentities.remove(cookieStoreId).then(
() => {
console.log(cookieStoreId, "deleted")
},
() => {
console.log(cookieStoreId, "already removed")
})
}
}
}
}
check()
}
}
var tc = new TabContainer()
tc.cleaning() // workaround to delete old container from previous session due to "close current tab" problem
// context-menus implementation
browser.contextMenus.create({
id: "contextNewTCtab",
title: "Open in New TC tab",
contexts: ["link"]
});
browser.contextMenus.onClicked.addListener(function (info, tab) {
if (info.menuItemId === "contextNewTCtab") {
console.log("open URL in new TC tab");
tc.createNew(info.linkUrl, active = false)
}
});
// check if newtab is in a context otherwise, close it and create a container tab to load the url
async function callback(details) {
if (details.url.slice(0, 5) === "about" || details.tabId < 0) {
return;
}
const tab = await browser.tabs.get(details.tabId);
// `firefox-default` is the ID of the tabs which are yet
// to be associated with a container
if (tab.cookieStoreId == "firefox-default") {
tc.createNew(details.url, active = tab.active, pinned = tab.pinned).then(
(_) => {
browser.tabs.remove(tab.id).then((_) => {
console.log("removed tab:", tab.id);
}, (error) => {
console.error("could not remove the tab:", tab.id, error);
});
},
(err) => {
console.log("could not create new tc tab:", err)
}
);
return {
cancel: true
};
} else {
// if the tab already associated with a container,
// we should check if the container still exists
const contexts = await browser.contextualIdentities.query({});
const tabCtx = contexts.find((c) => c.cookieStoreId === tab.cookieStoreId);
// could not find associated container for the tab
// re-open the tab in a new container
if (tabCtx === undefined) {
tc.createNew(details.url, active = tab.active, pinned = tab.pinned, index = tab.index).then(
(a) => {
console.log("updated container for the tab:", a);
browser.tabs.remove(tab.id)
},
(err) => {
console.error('could not update container for the tab:', err);
}
);
return {
cancel: true
};
}
}
}
// we need the <all_urls> permission to automatically
// open a new tab in a TC when it is not open via a
// "user action" like the context menu. Because we do
// this automatically and do not require user interaction
// we need "<all_urls>" permission and end up with the
// warning "This extension can access data on all websites"
// using the "activeTab" permission works only if we open
// a new temporary container tab from the context menu or
// keyboard shortcut (= with a user action)
filter = {
urls: ["<all_urls>"],
types: ["main_frame"]
}
browser.webRequest.onBeforeRequest.addListener(callback, filter, ['blocking'])
// focus not on address bar = bug in API
browser.commands.onCommand.addListener(function (command) {
if (command === "openNewTCtab") {
tc.createNew()
console.log("received openNewContainer event")
} else {
console.log("not good command received")
}
})
// delete temporary container when tab is closing
function rmCallback(tabId, removeInfo) {
function cleanClosedTabs() {
tc.checkUnusedContainer()
}
setTimeout(cleanClosedTabs, 5000)
// ISSUE: no time to get tabId, by the time browser.tabs.get(tabId), the
// tab is already closed... so instead each time a tab is closed, we trigger
// a checkUnusedContainer() after 5 s (in case the user want to restore the tab quickly)
}
browser.tabs.onRemoved.addListener(rmCallback)