This repository has been archived by the owner on Nov 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathincognito.js
73 lines (62 loc) · 1.91 KB
/
incognito.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
"use strict";
(function(exports) {
// This file keeps track of incognito sessions, and clears any caches after
// an entire incognito session is closed (i.e. all incognito windows are closed).
let state = {
incognito_session_exists: false,
};
function Incognito(onIncognitoDestruction) {
Object.assign(this, {onIncognitoDestruction});
// Listen to window creation, so we can detect if an incognito window is created
if (chrome.windows) {
chrome.windows.onCreated.addListener(this.detect_incognito_creation);
}
// Listen to window destruction, so we can clear caches if all incognito windows are destroyed
if (chrome.windows) {
chrome.windows.onRemoved.addListener(this.detect_incognito_destruction);
}
}
Incognito.prototype = {
/**
* Detect if an incognito session is created, so we can clear caches when it's destroyed.
*
* @param window: A standard Window object.
*/
detect_incognito_creation: function(window_) {
if (window_.incognito === true) {
state.incognito_session_exists = true;
}
},
// If a window is destroyed, and an incognito session existed, see if it still does.
detect_incognito_destruction: async function() {
if (state.incognito_session_exists) {
if (!(await any_incognito_windows())) {
state.incognito_session_exists = false;
this.onIncognitoDestruction();
}
}
},
}
/**
* Check if any incognito window still exists
*/
function any_incognito_windows() {
return new Promise(resolve => {
chrome.windows.getAll(arrayOfWindows => {
for (let window_ of arrayOfWindows) {
if (window_.incognito === true) {
return resolve(true);
}
}
resolve(false);
});
});
}
function onIncognitoDestruction(callback) {
return new Incognito(callback);
};
Object.assign(exports, {
onIncognitoDestruction,
state,
});
})(typeof exports == 'undefined' ? require.scopes.incognito = {} : exports);