-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
382 lines (339 loc) · 10.7 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Param values from https://developer.mozilla.org/Add-ons/WebExtensions/API/contextualIdentities/create
const NETFLIX_CONTAINER_NAME = "Netflix";
const NETFLIX_CONTAINER_COLOR = "red";
const NETFLIX_CONTAINER_ICON = "briefcase";
let NETFLIX_DOMAINS = [
"netflix.com", "nflxso.net", "nflxvideo.net", "nflximg.net", "nflxext.com"
];
const MAC_ADDON_ID = "@contain-netflix";
let macAddonEnabled = false;
let extensionSettings = {};
const canceledRequests = {};
const tabsWaitingToLoad = {};
const netflixHostREs = [];
async function isMACAddonEnabled () {
try {
const macAddonInfo = await browser.management.get(MAC_ADDON_ID);
if (macAddonInfo.enabled) {
return true;
}
} catch (e) {
return false;
}
return false;
}
async function setupMACAddonManagementListeners () {
browser.management.onInstalled.addListener(info => {
if (info.id === MAC_ADDON_ID) {
macAddonEnabled = true;
}
});
browser.management.onUninstalled.addListener(info => {
if (info.id === MAC_ADDON_ID) {
macAddonEnabled = false;
}
});
browser.management.onEnabled.addListener(info => {
if (info.id === MAC_ADDON_ID) {
macAddonEnabled = true;
}
});
browser.management.onDisabled.addListener(info => {
if (info.id === MAC_ADDON_ID) {
macAddonEnabled = false;
}
});
}
async function getMACAssignment (url) {
if (!macAddonEnabled) {
return false;
}
try {
const assignment = await browser.runtime.sendMessage(MAC_ADDON_ID, {
method: "getAssignment",
url
});
return assignment;
} catch (e) {
return false;
}
}
function cancelRequest (tab, options) {
// we decided to cancel the request at this point, register canceled request
canceledRequests[tab.id] = {
requestIds: {
[options.requestId]: true
},
urls: {
[options.url]: true
}
};
// since webRequest onCompleted and onErrorOccurred are not 100% reliable
// we register a timer here to cleanup canceled requests, just to make sure we don't
// end up in a situation where certain urls in a tab.id stay canceled
setTimeout(() => {
if (canceledRequests[tab.id]) {
delete canceledRequests[tab.id];
}
}, 2000);
}
function shouldCancelEarly (tab, options) {
// we decided to cancel the request at this point
if (!canceledRequests[tab.id]) {
cancelRequest(tab, options);
} else {
let cancelEarly = false;
if (canceledRequests[tab.id].requestIds[options.requestId] ||
canceledRequests[tab.id].urls[options.url]) {
// same requestId or url from the same tab
// this is a redirect that we have to cancel early to prevent opening two tabs
cancelEarly = true;
}
// register this requestId and url as canceled too
canceledRequests[tab.id].requestIds[options.requestId] = true;
canceledRequests[tab.id].urls[options.url] = true;
if (cancelEarly) {
return true;
}
}
return false;
}
function generateNetflixHostREs () {
const matchOperatorsRegex = /[|\\{}()[\]^$+*?.-]/g;
for (let netflixDomain of NETFLIX_DOMAINS) {
netflixDomain = netflixDomain.replace(matchOperatorsRegex, '\\$&');
netflixHostREs.push(new RegExp(`(^|\\.)${netflixDomain}$`));
}
}
async function loadExtensionSettings () {
extensionSettings = await browser.storage.sync.get();
}
async function clearNetflixCookies () {
// Clear all netflix cookies
const containers = await browser.contextualIdentities.query({});
containers.push({
cookieStoreId: "firefox-default"
});
let macAssignments = [];
if (macAddonEnabled) {
const promises = NETFLIX_DOMAINS.map(async netflixDomain => {
const assigned = await getMACAssignment(`https://${netflixDomain}/`);
return assigned ? netflixDomain : null;
});
macAssignments = await Promise.all(promises);
}
NETFLIX_DOMAINS.map(async netflixDomain => {
const netflixCookieUrl = `https://${netflixDomain}/`;
// dont clear cookies for netflixDomain if mac assigned (with or without www.)
if (macAddonEnabled &&
(macAssignments.includes(netflixDomain) ||
macAssignments.includes(`www.${netflixDomain}`))) {
return;
}
containers.map(async container => {
const storeId = container.cookieStoreId;
if (storeId === netflixCookieStoreId) {
// Don't clear cookies in the Netflix Container
return;
}
const cookies = await browser.cookies.getAll({
domain: netflixDomain,
storeId
});
cookies.map(cookie => {
browser.cookies.remove({
name: cookie.name,
url: netflixCookieUrl,
storeId
});
});
});
});
}
async function setupContainer () {
// Use existing Netflix container, or create one
const contexts = await browser.contextualIdentities.query({name: NETFLIX_CONTAINER_NAME});
if (contexts.length > 0) {
netflixCookieStoreId = contexts[0].cookieStoreId;
} else {
const context = await browser.contextualIdentities.create({
name: NETFLIX_CONTAINER_NAME,
color: NETFLIX_CONTAINER_COLOR,
icon: NETFLIX_CONTAINER_ICON
});
netflixCookieStoreId = context.cookieStoreId;
}
}
function reopenTab ({url, tab, cookieStoreId}) {
browser.tabs.create({
url,
cookieStoreId,
active: tab.active,
index: tab.index + 1,
windowId: tab.windowId
});
browser.tabs.remove(tab.id);
}
function isNetflixURL (url) {
const parsedUrl = new URL(url);
for (let netflixHostRE of netflixHostREs) {
if (netflixHostRE.test(parsedUrl.host)) {
return true;
}
}
return false;
}
function shouldContainInto (url, tab) {
if (!url.startsWith("http")) {
// we only handle URLs starting with http(s)
return false;
}
let handleUrl = isNetflixURL(url);
if (handleUrl) {
if (tab.cookieStoreId !== netflixCookieStoreId) {
if (tab.cookieStoreId !== "firefox-default" && extensionSettings.dont_override_containers) {
// Tab is already in a container, the user doesn't want us to override containers
return false;
}
// Netflix-URL outside of Netflix Container Tab
// Should contain into Netflix Container
return netflixCookieStoreId;
}
} else if (tab.cookieStoreId === netflixCookieStoreId) {
// Non-Netflix-URL inside Netflix Container Tab
// Should contain into Default Container
return "firefox-default";
}
return false;
}
async function maybeReopenAlreadyOpenTabs () {
const maybeReopenTab = async tab => {
const macAssigned = await getMACAssignment(tab.url);
if (macAssigned) {
// We don't reopen MAC assigned urls
return;
}
const cookieStoreId = shouldContainInto(tab.url, tab);
if (!cookieStoreId) {
// Tab doesn't need to be contained
return;
}
reopenTab({
url: tab.url,
tab,
cookieStoreId
});
};
const tabsOnUpdated = (tabId, changeInfo, tab) => {
if (changeInfo.url && tabsWaitingToLoad[tabId]) {
// Tab we're waiting for switched it's url, maybe we reopen
delete tabsWaitingToLoad[tabId];
maybeReopenTab(tab);
}
if (tab.status === "complete" && tabsWaitingToLoad[tabId]) {
// Tab we're waiting for completed loading
delete tabsWaitingToLoad[tabId];
}
if (!Object.keys(tabsWaitingToLoad).length) {
// We're done waiting for tabs to load, remove event listener
browser.tabs.onUpdated.removeListener(tabsOnUpdated);
}
};
// Query for already open Tabs
const tabs = await browser.tabs.query({});
tabs.map(async tab => {
if (tab.incognito) {
return;
}
if (tab.url === "about:blank") {
if (tab.status !== "loading") {
return;
}
// about:blank Tab is still loading, so we indicate that we wait for it to load
// and register the event listener if we haven't yet.
//
// This is a workaround until platform support is implemented:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1447551
// https://github.com/mozilla/multi-account-containers/issues/474
tabsWaitingToLoad[tab.id] = true;
if (!browser.tabs.onUpdated.hasListener(tabsOnUpdated)) {
browser.tabs.onUpdated.addListener(tabsOnUpdated);
}
} else {
// Tab already has an url, maybe we reopen
maybeReopenTab(tab);
}
});
}
async function containNetflix (options) {
// Listen to requests and open Netflix into its Container,
// open other sites into the default tab context
if (options.tabId === -1) {
// Request doesn't belong to a tab
return;
}
if (tabsWaitingToLoad[options.tabId]) {
// Cleanup just to make sure we don't get a race-condition with startup reopening
delete tabsWaitingToLoad[options.tabId];
}
// We have to check with every request if the requested URL is assigned with MAC
// because the user can assign URLs at any given time (needs MAC Events)
const macAssigned = await getMACAssignment(options.url);
if (macAssigned) {
// This URL is assigned with MAC, so we don't handle this request
return;
}
const tab = await browser.tabs.get(options.tabId);
if (tab.incognito) {
// We don't handle incognito tabs
return;
}
// Check whether we should contain this request into another container
const cookieStoreId = shouldContainInto(options.url, tab);
if (!cookieStoreId) {
// Request doesn't need to be contained
return;
}
if (shouldCancelEarly(tab, options)) {
// We need to cancel early to prevent multiple reopenings
return {cancel: true};
}
// Decided to contain
reopenTab({
url: options.url,
tab,
cookieStoreId
});
return {cancel: true};
}
(async function init() {
await setupMACAddonManagementListeners();
macAddonEnabled = await isMACAddonEnabled();
try {
await setupContainer();
} catch (error) {
// TODO: Needs backup strategy
// See https://github.com/mozilla/contain-facebook/issues/23
// Sometimes this add-on is installed but doesn't get a netflixCookieStoreId ?
// eslint-disable-next-line no-console
console.log(error);
return;
}
loadExtensionSettings();
clearNetflixCookies();
generateNetflixHostREs();
// Clean up canceled requests
browser.webRequest.onCompleted.addListener((options) => {
if (canceledRequests[options.tabId]) {
delete canceledRequests[options.tabId];
}
},{urls: ["<all_urls>"], types: ["main_frame"]});
browser.webRequest.onErrorOccurred.addListener((options) => {
if (canceledRequests[options.tabId]) {
delete canceledRequests[options.tabId];
}
},{urls: ["<all_urls>"], types: ["main_frame"]});
// Add the request listener
browser.webRequest.onBeforeRequest.addListener(containNetflix, {urls: ["<all_urls>"], types: ["main_frame"]}, ["blocking"]);
maybeReopenAlreadyOpenTabs();
})();