-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
55 lines (42 loc) · 1.42 KB
/
index.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
import { browsers, detect } from 'get-browser';
const UNUSUAL_KEY = 'cd1394e6-3fd1-4a2d-ae60-c9ae01f7ee89';
const CANNOT_IDENTIFY_ERROR = new Error('Cannot identify whether incognito mode is active');
export default function isIncognito() {
const browser = detect();
return new Promise((resolve, reject) => {
const yes = () => resolve(true);
const no = () => resolve(false);
const unknown = () => reject(CANNOT_IDENTIFY_ERROR);
if (browser === browsers.CHROME || browser === browsers.OPERA) {
const fs = window.RequestFileSystem || window.webkitRequestFileSystem;
if (!fs) return unknown();
return fs(0, 0, no, yes);
}
if (browser === browsers.FIREFOX) {
if (!window.indexedDB) return yes();
const db = window.indexedDB.open(UNUSUAL_KEY);
db.onerror = yes;
db.onsuccess = no;
return;
}
if (browser === browsers.IE || browser === browsers.EDGE) {
const isPrivate = !window.indexedDB && (window.PointerEvent || window.MSPointerEvent);
return isPrivate ? yes() : no();
}
if (browser === browsers.SAFARI) {
try {
localStorage[UNUSUAL_KEY] = UNUSUAL_KEY;
localStorage.removeItem(UNUSUAL_KEY);
} catch (e) {
return yes();
}
try {
window.openDatabase(null, null, null, null);
} catch (e) {
return yes();
};
return no();
}
return unknown();
});
};