From 6a03d29b86dc4fe8eae04eaf0f9fc661f1c3d1ea Mon Sep 17 00:00:00 2001 From: axi92 Date: Mon, 6 May 2024 14:18:49 +0200 Subject: [PATCH] fix(electron): detect electron context (#1856) * fix(electron): detect electron env * fix(electron): cleanup code * fix: fixed wrong operator * fix(electron): improved code and add some comments * Update src/lib/is-browser.ts Co-authored-by: Daniel Lando * fix: typo and lint --------- Co-authored-by: Daniel Lando --- src/lib/is-browser.ts | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/lib/is-browser.ts b/src/lib/is-browser.ts index 02134ab8f..7961aa20d 100644 --- a/src/lib/is-browser.ts +++ b/src/lib/is-browser.ts @@ -1,5 +1,28 @@ -const isStandardBrowserEnv = () => - typeof window !== 'undefined' && typeof window.document !== 'undefined' +const isStandardBrowserEnv = () => { + // window is only defined when it is a browser + if (typeof window !== 'undefined') { + // Is the process an electron application + // check if we are in electron `renderer` + const electronRenderCheck = + navigator?.userAgent?.toLowerCase().indexOf(' electron/') > -1 + 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, + // nodeIntegration: true + // contextIsolation: false + // } + return !electronMainCheck + } + return typeof window.document !== 'undefined' + } + // return false if nothing is detected + return false +} const isWebWorkerEnv = () => Boolean(