最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Detect an Electron instance via JavaScript - Stack Overflow

programmeradmin3浏览0评论

I have a webapp which will run on a website and as a standalone Electron instance (.exe for Windows).

I'd like to tell via JavaScript if the webapp is running inside ElectronJS or not, in order to display some extra features for the online version. Is there any way to detect the Electron framework instance? I'd like to avoid writing two slightly different versions of the webapp.

I have a webapp which will run on a website and as a standalone Electron instance (.exe for Windows).

I'd like to tell via JavaScript if the webapp is running inside ElectronJS or not, in order to display some extra features for the online version. Is there any way to detect the Electron framework instance? I'd like to avoid writing two slightly different versions of the webapp.

Share Improve this question asked May 11, 2020 at 8:25 Gianluca GhettiniGianluca Ghettini 11.6k23 gold badges99 silver badges167 bronze badges 1
  • 2 This might help: github.com/electron/electron/issues/2288 – Felix Kling Commented May 11, 2020 at 8:29
Add a comment  | 

4 Answers 4

Reset to default 14

Just use this code (got it from is-electron "library")

function isElectron() {
    // Renderer process
    if (typeof window !== 'undefined' && typeof window.process === 'object' && window.process.type === 'renderer') {
        return true;
    }

    // Main process
    if (typeof process !== 'undefined' && typeof process.versions === 'object' && !!process.versions.electron) {
        return true;
    }

    // Detect the user agent when the `nodeIntegration` option is set to true
    if (typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent.indexOf('Electron') >= 0) {
        return true;
    }

    return false;
}

Based on electron issue:

  • For main scripts, they're running as a Node process, so use process.versions.hasOwnProperty('electron') or equivalent

  • For renderer scripts, they're running in the browser, so use /electron/i.test(navigator.userAgent) or equivalent

Electron exposes full access to Node.js both in the main and the renderer process.

Source: Electron Application Architecture

This means that in your renderer thread (i.e. the user-facing part of your application) you can access NodeJS native modules such as fs.

I would recommend adopting a similar approach to browsers: avoid user-agent-based conditionals and prefer a is-this-feature-available? model if you can:

// renderer.js
try {
  const fs = require('fs');
  // test something (quick) that only the `fs` module can do
} catch (e) {
  // probably not an electron app
}

However please do read https://www.electronjs.org/docs/tutorial/security first!


Further thoughts

Presumably you must somehow derive two artefacts from one single codebase: a web app and a desktop app.

You could perhaps consider injecting/exposing an environment variable during the build process assuming you can run two different build configurations.

you can use for simplicity a library named is-electron

发布评论

评论列表(0)

  1. 暂无评论