From b28f92fe4240e2136e70fb8b18ca70961d16aded Mon Sep 17 00:00:00 2001 From: axi92 Date: Tue, 30 Apr 2024 17:31:30 +0200 Subject: [PATCH 1/6] fix(electron): detect electron env --- src/lib/is-browser.ts | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib/is-browser.ts b/src/lib/is-browser.ts index 02134ab8f..356ec3d1d 100644 --- a/src/lib/is-browser.ts +++ b/src/lib/is-browser.ts @@ -1,5 +1,22 @@ -const isStandardBrowserEnv = () => - typeof window !== 'undefined' && typeof window.document !== 'undefined' +const isStandardBrowserEnv = () => { + if (typeof window !== 'undefined') { + return ( + window.navigator.userAgent.toLowerCase().indexOf(' electron/') > + -1 && + Object.prototype.hasOwnProperty.call(process.versions, 'electron') + ) + // const userAgent = window.navigator.userAgent.toLowerCase() + // if ( + // userAgent.indexOf(' electron/') > -1 || + // Object.prototype.hasOwnProperty.call(process.versions, 'electron') + // ) { + // return false + // } + } + return ( + typeof window !== 'undefined' && typeof window.document !== 'undefined' + ) +} const isWebWorkerEnv = () => Boolean( From 9224f081448c5f1b038969259151fd6a878064b1 Mon Sep 17 00:00:00 2001 From: axi92 Date: Tue, 30 Apr 2024 18:01:04 +0200 Subject: [PATCH 2/6] fix(electron): cleanup code --- src/lib/is-browser.ts | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/lib/is-browser.ts b/src/lib/is-browser.ts index 356ec3d1d..14224cf9a 100644 --- a/src/lib/is-browser.ts +++ b/src/lib/is-browser.ts @@ -1,21 +1,16 @@ const isStandardBrowserEnv = () => { if (typeof window !== 'undefined') { - return ( - window.navigator.userAgent.toLowerCase().indexOf(' electron/') > - -1 && + if ( + navigator.userAgent.toLowerCase().indexOf(' electron/') > -1 && Object.prototype.hasOwnProperty.call(process.versions, 'electron') + ) { + return false + } + return ( + typeof window !== 'undefined' && + typeof window.document !== 'undefined' ) - // const userAgent = window.navigator.userAgent.toLowerCase() - // if ( - // userAgent.indexOf(' electron/') > -1 || - // Object.prototype.hasOwnProperty.call(process.versions, 'electron') - // ) { - // return false - // } } - return ( - typeof window !== 'undefined' && typeof window.document !== 'undefined' - ) } const isWebWorkerEnv = () => From 8372c2843aab106c834a4eab9bd86a6d532324ec Mon Sep 17 00:00:00 2001 From: axi92 Date: Thu, 2 May 2024 08:35:56 +0200 Subject: [PATCH 3/6] fix: fixed wrong operator --- src/lib/is-browser.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/is-browser.ts b/src/lib/is-browser.ts index 14224cf9a..60e4c7c01 100644 --- a/src/lib/is-browser.ts +++ b/src/lib/is-browser.ts @@ -1,7 +1,7 @@ const isStandardBrowserEnv = () => { if (typeof window !== 'undefined') { if ( - navigator.userAgent.toLowerCase().indexOf(' electron/') > -1 && + navigator.userAgent.toLowerCase().indexOf(' electron/') > -1 || Object.prototype.hasOwnProperty.call(process.versions, 'electron') ) { return false From 3620992e90dcae0232dcf92e9892f9e602bb8225 Mon Sep 17 00:00:00 2001 From: axi92 Date: Thu, 2 May 2024 10:03:39 +0200 Subject: [PATCH 4/6] fix(electron): improved code and add some comments --- src/lib/is-browser.ts | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/lib/is-browser.ts b/src/lib/is-browser.ts index 60e4c7c01..6453a685d 100644 --- a/src/lib/is-browser.ts +++ b/src/lib/is-browser.ts @@ -1,16 +1,28 @@ const isStandardBrowserEnv = () => { + // window is only defined when it is a browser if (typeof window !== 'undefined') { - if ( - navigator.userAgent.toLowerCase().indexOf(' electron/') > -1 || - Object.prototype.hasOwnProperty.call(process.versions, 'electron') - ) { + // Is the process an electron application + const electronMainCheck = Object.prototype.hasOwnProperty.call( + process.versions, + 'electron', + ) + // In case of electron the userAgent contains a string formated like: 'Electron/' + // we can search for that to detect if it is an electron application + const electronRenderCheck = + navigator.userAgent.toLowerCase().indexOf(' electron/') > -1 + if (electronMainCheck && electronRenderCheck) { + // Both electron checks are only true if the following webPreferences are set in the main electron BrowserWindow() + // webPreferences: { + // sandbox: false, + // nodeIntegration: true + // contextIsolation: false + // } return false } - return ( - typeof window !== 'undefined' && - typeof window.document !== 'undefined' - ) + return typeof window.document !== 'undefined' } + // return false if nothing is detected + return false } const isWebWorkerEnv = () => From bb6c89a5ccad5044974608299711a66b50abbb9d Mon Sep 17 00:00:00 2001 From: axi92 Date: Thu, 2 May 2024 10:54:56 +0200 Subject: [PATCH 5/6] Update src/lib/is-browser.ts Co-authored-by: Daniel Lando --- src/lib/is-browser.ts | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/lib/is-browser.ts b/src/lib/is-browser.ts index 6453a685d..6347d2a9d 100644 --- a/src/lib/is-browser.ts +++ b/src/lib/is-browser.ts @@ -2,22 +2,21 @@ const isStandardBrowserEnv = () => { // window is only defined when it is a browser if (typeof window !== 'undefined') { // Is the process an electron application - const electronMainCheck = Object.prototype.hasOwnProperty.call( - process.versions, - 'electron', - ) - // In case of electron the userAgent contains a string formated like: 'Electron/' - // we can search for that to detect if it is an electron application + // check if we are in electron `renderer` const electronRenderCheck = - navigator.userAgent.toLowerCase().indexOf(' electron/') > -1 - if (electronMainCheck && electronRenderCheck) { + navigator?.userAgent?.toLowerCase().indexOf(' electron/') > -1 + if (electronRendererCheck && process?.versions) { + const electronMainCheck = Object.prototype.hasOwnProperty.call( + process.versions, + 'electron', + ) // Both electron checks are only true if the following webPreferences are set in the main electron BrowserWindow() // webPreferences: { // sandbox: false, // nodeIntegration: true // contextIsolation: false // } - return false + return !electronMainCheck } return typeof window.document !== 'undefined' } From d653bca59e0655d0f21350a741b5975f7b6350f1 Mon Sep 17 00:00:00 2001 From: axi92 Date: Thu, 2 May 2024 11:15:00 +0200 Subject: [PATCH 6/6] fix: typo and lint --- src/lib/is-browser.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/lib/is-browser.ts b/src/lib/is-browser.ts index 6347d2a9d..7961aa20d 100644 --- a/src/lib/is-browser.ts +++ b/src/lib/is-browser.ts @@ -5,11 +5,11 @@ const isStandardBrowserEnv = () => { // check if we are in electron `renderer` const electronRenderCheck = navigator?.userAgent?.toLowerCase().indexOf(' electron/') > -1 - if (electronRendererCheck && process?.versions) { - const electronMainCheck = Object.prototype.hasOwnProperty.call( - process.versions, - 'electron', - ) + if (electronRenderCheck && process?.versions) { + const electronMainCheck = Object.prototype.hasOwnProperty.call( + process.versions, + 'electron', + ) // Both electron checks are only true if the following webPreferences are set in the main electron BrowserWindow() // webPreferences: { // sandbox: false,