Skip to content

isElectron

依据 User-Agent 检测当前是否运行在 Electron 渲染进程中。

前置依赖

参数名类型说明
deps.userAgentNavigator['userAgent']渲染进程 UA 字符串;Electron 含 Electron/x.y.z

函数签名

typescript
interface IsElectronDeps {
  userAgent: Navigator['userAgent']
}

function isElectron(deps: IsElectronDeps): boolean

参数

参数名类型必填说明
deps.userAgentstring通常为 navigator.userAgent

返回值

类型说明
booleanUA 匹配 /\belectron\//i 时为 true

工作原理

  1. deps.userAgent 做大小写不敏感匹配,查找 electron/ 子串。
  2. import.meta.env.VITE_ELECTRON 解耦,开发时无需单独 --mode electron
  3. 嵌入 qiankun 的普通浏览器 UA 返回 false。

异常

无异常抛出。