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;

// --- environment detection -------------------------------
// https://github.com/uupaa/WebModule/wiki/EnvironmentDetection
GLOBAL["IN_EL"]      = "__dirname" in GLOBAL && "__filename" in GLOBAL;
GLOBAL["IN_BROWSER"] = !GLOBAL["IN_EL"] && !GLOBAL.global && "document" in GLOBAL;
GLOBAL["IN_WORKER"]  = !GLOBAL["IN_EL"] && !GLOBAL.global && "WorkerLocation" in GLOBAL;
GLOBAL["IN_NODE"]    = !GLOBAL["IN_EL"] &&  GLOBAL.global && !/native/.test(setTimeout);
GLOBAL["IN_NW"]      = !GLOBAL["IN_EL"] &&  GLOBAL.global &&  /native/.test(setTimeout);

Clone this wiki locally