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 = GLOBAL || (this || 0).self || global;

(function(global) {

var inElectron         = "__dirname" in global && "__filename" in global;
var inWorker           = "WorkerLocation" in global;
var hasDocument        = "document" in global;
var hasCustomTimer     = !/native/.test(setTimeout);

global["IN_EL"]        =  inElectron;                 // electron
global["IN_EL_RENDER"] =  inElectron &&  hasDocument; // electron render process. (frontend)
global["IN_EL_MAIN"]   =  inElectron && !hasDocument; // electron main process. (backend)
global["IN_BROWSER"]   = !inElectron &&  hasDocument;
global["IN_WORKER"]    = !inElectron && !hasDocument && inWorker;
global["IN_NODE"]      = !inElectron &&  hasCustomTimer;
global["IN_NW"]        = !inElectron && !hasCustomTimer;

})(GLOBAL);

Clone this wiki locally