Skip to content

Commit

Permalink
Resolve an issue when the nodeIntegration option is set to false (closes
Browse files Browse the repository at this point in the history
 #1)
  • Loading branch information
cheton committed Aug 2, 2017
1 parent 89a905e commit 4acaa6c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
13 changes: 11 additions & 2 deletions index.js
@@ -1,11 +1,20 @@
// https://github.com/electron/electron/issues/2288
function isElectron() {
if (typeof window !== 'undefined' && window.process && window.process.type === 'renderer') {
// Renderer process
if (typeof window !== 'undefined' && typeof window.process === 'object' && window.process.type === 'renderer') {
return true;
}
if (typeof process !== 'undefined' && process.versions && !!process.versions.electron) {

// 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;
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "is-electron",
"version": "2.0.0",
"version": "2.1.0",
"description": "Detect if running in Electron.",
"homepage": "https://github.com/cheton/is-electron",
"main": "index.js",
Expand Down
39 changes: 38 additions & 1 deletion test/index.js
@@ -1,7 +1,44 @@
var test = require('tap').test;
const test = require('tap').test;
const isElectron = require('../');

test('should return false in Node.js', function(t) {
const isElectron = require('../');
t.equal(isElectron(), false);
t.end();
});

test('Renderer process', function(t) {
const window = global.window;
global.window = {};
global.window.process = global.window.process || {};
global.window.process.type = 'renderer';

t.equal(isElectron(), true);
global.window = window;
t.equal(isElectron(), false);

t.end();
});

test('Main process', function(t) {
const electron = process.versions.electron;
process.versions.electron = 'x.y.z';

t.equal(isElectron(), true);
process.versions.electron = electron;
t.equal(isElectron(), false);

t.end();
});

test('Detect the user agent when the `nodeIntegration` option is set to true', function(t) {
const navigator = global.navigator;
global.navigator = {};
global.navigator.userAgent = 'Electron';

t.equal(isElectron(), true);
global.navigator = navigator;
t.equal(isElectron(), false);

t.end();
});

0 comments on commit 4acaa6c

Please sign in to comment.