Skip to content

EnvironmentDetection

uupaa edited this page Oct 23, 2015 · 30 revisions

それぞれの環境における特徴点を洗い出したものが以下になります。

typeof * Browser Worker Node NW.js Electron
render
Electron
main
window object - - object object -
self object object - object object -
global - - object object object object
GLOBAL - - object - object object
document object - - object object -
WorkerLocation - object - - - -
WebView - - - - function -
__dirname - - - - string string
__filename - - - - string string
setTimeout + "" native native custom native native native
var GLOBAL = (this || 0).self || global;

(function(global) {

var inElectron = "__dirname" in global && "__filename" in global;
var hasGlobal  = !!global.global;

global.IN_ELECTRON =  inElectron;
global.IN_BROWSER  = !inElectron && !hasGlobal && "document" in GLOBAL;
global.IN_WORKER   = !inElectron && !hasGlobal && "WorkerLocation" in GLOBAL;
global.IN_NODE     = !inElectron &&  hasGlobal && !/native/.test(setTimeout);
global.IN_NW       = !inElectron &&  hasGlobal &&  /native/.test(setTimeout);

})(GLOBAL);

Clone this wiki locally