detectincognitojs
Version:
detectIncognito.js can be used to detect incognito mode & other private browsing modes on most modern browsers.
31 lines • 7.36 kB
JavaScript
/*!
*
* detectIncognito v1.7.0
*
* https://github.com/Joe12387/detectIncognito
*
* MIT License
*
* Copyright (c) 2021 - 2026 Joe Rutkowski <Joe@dreggle.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Please keep this comment intact in order to properly abide by the MIT License.
*
**/var e={d:(t,r)=>{for(var n in r)e.o(r,n)&&!e.o(t,n)&&Object.defineProperty(t,n,{enumerable:!0,get:r[n]})},o:(e,t)=>Object.prototype.hasOwnProperty.call(e,t)},t={};e.d(t,{A:()=>a,k:()=>o});var r=function(e,t,r,n){return new(r||(r=Promise))(function(o,a){function i(e){try{s(n.next(e))}catch(e){a(e)}}function c(e){try{s(n.throw(e))}catch(e){a(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r(function(e){e(t)})).then(i,c)}s((n=n.apply(e,t||[])).next())})},n=function(e,t){var r,n,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(c){return function(s){return function(c){if(r)throw new TypeError("Generator is already executing.");for(;a&&(a=0,c[0]&&(i=0)),i;)try{if(r=1,n&&(o=2&c[0]?n.return:c[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,c[1])).done)return o;switch(n=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,n=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!(o=i.trys,(o=o.length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){i.label=c[1];break}if(6===c[0]&&i.label<o[1]){i.label=o[1],o=c;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(c);break}o[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(e,i)}catch(e){c=[6,e],n=0}finally{r=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,s])}}};function o(){return r(this,void 0,Promise,function(){return n(this,function(e){switch(e.label){case 0:return[4,new Promise(function(e,t){var o="Unknown",a=!1;function i(t){a||(a=!0,e({isPrivate:t,browserName:o}))}function c(){var e=0,t=parseInt("-1");try{t.toFixed(t)}catch(t){e=t.message.length}return e}function s(){return r(this,void 0,void 0,function(){var e,t;return n(this,function(r){switch(r.label){case 0:return r.trys.push([0,2,,3]),[4,navigator.storage.getDirectory()];case 1:return r.sent(),i(!1),[3,3];case 2:return e=r.sent(),t=e instanceof Error&&"string"==typeof e.message?e.message:String(e),i(t.includes("unknown transient reason")),[3,3];case 3:return[2]}})})}function u(){var e;return r(this,void 0,Promise,function(){return n(this,function(t){switch(t.label){case 0:return"function"!=typeof(null===(e=navigator.storage)||void 0===e?void 0:e.getDirectory)?[3,2]:[4,s()];case 1:return t.sent(),[3,3];case 2:void 0!==navigator.maxTouchPoints?function(){var e=String(Math.random());try{var t=indexedDB.open(e,1);t.onupgradeneeded=function(t){var r=t.target.result,n=function(e){i(e)};try{r.createObjectStore("t",{autoIncrement:!0}).put(new Blob),n(!1)}catch(e){(e instanceof Error&&"string"==typeof e.message?e.message:String(e)).includes("are not yet supported")?n(!0):n(!1)}finally{r.close(),indexedDB.deleteDatabase(e)}},t.onerror=function(){return i(!1)}}catch(e){i(!1)}}():function(){var e=window.openDatabase,t=window.localStorage;try{e(null,null,null,null)}catch(e){return void i(!0)}try{t.setItem("test","1"),t.removeItem("test")}catch(e){return void i(!0)}i(!1)}(),t.label=3;case 3:return[2]}})})}function l(){var e;return r(this,void 0,Promise,function(){var t,r,o;return n(this,function(n){switch(n.label){case 0:if("function"!=typeof(null===(e=navigator.storage)||void 0===e?void 0:e.getDirectory))return[3,5];n.label=1;case 1:return n.trys.push([1,3,,4]),[4,navigator.storage.getDirectory()];case 2:return n.sent(),i(!1),[3,4];case 3:return t=n.sent(),r=t instanceof Error&&"string"==typeof t.message?t.message:String(t),i(r.includes("Security error")),[2];case 4:return[3,6];case 5:(o=indexedDB.open("inPrivate")).onerror=function(e){o.error&&"InvalidStateError"===o.error.name&&e.preventDefault(),i(!0)},o.onsuccess=function(){indexedDB.deleteDatabase("inPrivate"),i(!1)},n.label=6;case 6:return[2]}})})}(function(){return r(this,void 0,Promise,function(){return n(this,function(e){switch(e.label){case 0:return 44!==c()&&43!==c()?[3,2]:(o="Safari",[4,u()]);case 1:return e.sent(),[3,6];case 2:return 51!==c()?[3,3]:(a=navigator.userAgent,o=a.match(/Chrome/)?void 0!==navigator.brave?"Brave":a.match(/Edg/)?"Edge":a.match(/OPR/)?"Opera":"Chrome":"Chromium",function(){var e=this,t="__di_"+Math.random().toString(36).slice(2),o=new Uint8Array(16384),a=indexedDB.open(t,1);a.onupgradeneeded=function(){a.result.createObjectStore("s")},a.onerror=function(){indexedDB.deleteDatabase(t),i(!1)},a.onsuccess=function(){var c=a.result,s=!1;try{var u=c.transaction("s","readwrite",{durability:"strict"});s="strict"===u.durability,u.abort()}catch(e){}if(!s)return c.close(),indexedDB.deleteDatabase(t),void i(!1);var l=function(e){return new Promise(function(t,r){var n=performance.now(),a=0,i=function(){if(15!==a){var s=c.transaction("s","readwrite",{durability:e});s.objectStore("s").put(o,a),a++,s.oncomplete=i,s.onerror=s.onabort=function(){return r(s.error)}}else t(performance.now()-n)};i()})};r(e,void 0,void 0,function(){var e,r,o,a,s;return n(this,function(n){switch(n.label){case 0:return e=performance.now(),[4,l("relaxed")];case 1:return n.sent(),[4,l("strict")];case 2:n.sent(),r=[],o=0,n.label=3;case 3:return o<15?[4,l("relaxed")]:[3,7];case 4:return a=n.sent(),[4,l("strict")];case 5:if(s=n.sent(),r.push(a>0?s/a:1/0),r.length>=7&&performance.now()-e>=1e3)return[3,7];n.label=6;case 6:return o++,[3,3];case 7:return r.sort(function(e,t){return e-t}),c.close(),indexedDB.deleteDatabase(t),i(r[r.length>>1]<1.3),[2]}})}).catch(function(){c.close(),indexedDB.deleteDatabase(t),i(!1)})}}(),[3,6]);case 3:return 25!==c()?[3,5]:(o="Firefox",[4,l()]);case 4:return e.sent(),[3,6];case 5:void 0!==navigator.msSaveBlob?(o="Internet Explorer",i(void 0===window.indexedDB)):t(new Error("detectIncognito cannot determine the browser")),e.label=6;case 6:return[2]}var a})})})().catch(t)})];case 1:return[2,e.sent()]}})})}"undefined"!=typeof window&&(window.detectIncognito=o);const a=o,i=t.A,c=t.k;export{i as default,c as detectIncognito};