Skip to content

Commit

Permalink
Merge pull request #11 from Joe12387/port-1.1.0b-to-typescript
Browse files Browse the repository at this point in the history
Ports 1.1.0 changes to detectIncognito.ts
  • Loading branch information
Joe12387 authored Jun 27, 2022
2 parents 89167e1 + 6b58773 commit 211474c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 44 deletions.
3 changes: 3 additions & 0 deletions src/@types/lib.dom.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
interface IDBVersionChangeEvent {
target: IDBRequest;
}
74 changes: 30 additions & 44 deletions src/detectIncognito.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
*
* detectIncognito v1.0.0 - (c) 2022 Joe Rutkowski <Joe@dreggle.com> (https://github.com/Joe12387/detectIncognito)
* detectIncognito v1.1.0 - (c) 2022 Joe Rutkowski <Joe@dreggle.com> (https://github.com/Joe12387/detectIncognito)
*
**/
export const detectIncognito = function (): Promise<{
Expand Down Expand Up @@ -69,39 +69,38 @@ export const detectIncognito = function (): Promise<{
* Safari (Safari for iOS & macOS)
**/

function macOS_safari14() {
function newSafariTest() {
try {
(window as any).safari.pushNotification.requestPermission(
"https://example.com",
"private",
{},
function () {}
);
} catch (e: any) {
return __callback(!new RegExp("gesture").test(e));
}
return __callback(false);
}
var db = window.indexedDB.open("test", 1);

function iOS_safari14() {
var tripped = false;
var iframe = document.createElement("iframe");
iframe.style.display = "none";
document.body.appendChild(iframe);
db.onupgradeneeded = function (i) {
var res = i.target?.result;

(iframe as any).contentWindow.applicationCache.addEventListener(
"error",
function () {
tripped = true;
return __callback(true);
}
);
try {
res.createObjectStore("test", {
autoIncrement: true,
}).put(new Blob);

__callback(false);
} catch (e) {
let message = e;

if (e instanceof Error) {
message = e.message ?? e;
}

if (typeof message !== 'string') {
return __callback(false);
}

let matchesExpectedError = /BlobURLs are not yet supported/.test(message);

setTimeout(function () {
if (!tripped) {
__callback(false);
return __callback(matchesExpectedError);
}
}
}, 100);
} catch (e) {
return __callback(false);
}
}

function oldSafariTest() {
Expand All @@ -122,23 +121,9 @@ export const detectIncognito = function (): Promise<{
}

function safariPrivateTest() {
var w = window as any;
if (navigator.maxTouchPoints !== undefined) {
if (w.safari !== undefined && w.DeviceMotionEvent === undefined) {
browserName = "Safari for macOS";
macOS_safari14();
} else if (w.DeviceMotionEvent !== undefined) {
browserName = "Safari for iOS";
iOS_safari14();
} else {
reject(
new Error(
"detectIncognito Could not identify this version of Safari"
)
);
}
newSafariTest();
} else {
browserName = "Safari";
oldSafariTest();
}
}
Expand Down Expand Up @@ -214,6 +199,7 @@ export const detectIncognito = function (): Promise<{

function main() {
if (isSafari()) {
browserName = 'Safari';
safariPrivateTest();
} else if (isChrome()) {
browserName = identifyChromium();
Expand Down

0 comments on commit 211474c

Please sign in to comment.