Skip to content

Commit 6a03d29

Browse files
axi92robertsLando
andauthored
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 <[email protected]> * fix: typo and lint --------- Co-authored-by: Daniel Lando <[email protected]>
1 parent c6580a6 commit 6a03d29

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/lib/is-browser.ts

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
1-
const isStandardBrowserEnv = () =>
2-
typeof window !== 'undefined' && typeof window.document !== 'undefined'
1+
const isStandardBrowserEnv = () => {
2+
// window is only defined when it is a browser
3+
if (typeof window !== 'undefined') {
4+
// Is the process an electron application
5+
// check if we are in electron `renderer`
6+
const electronRenderCheck =
7+
navigator?.userAgent?.toLowerCase().indexOf(' electron/') > -1
8+
if (electronRenderCheck && process?.versions) {
9+
const electronMainCheck = Object.prototype.hasOwnProperty.call(
10+
process.versions,
11+
'electron',
12+
)
13+
// Both electron checks are only true if the following webPreferences are set in the main electron BrowserWindow()
14+
// webPreferences: {
15+
// sandbox: false,
16+
// nodeIntegration: true
17+
// contextIsolation: false
18+
// }
19+
return !electronMainCheck
20+
}
21+
return typeof window.document !== 'undefined'
22+
}
23+
// return false if nothing is detected
24+
return false
25+
}
326

427
const isWebWorkerEnv = () =>
528
Boolean(

0 commit comments

Comments
 (0)