Skip to content

Instantly share code, notes, and snippets.

@uupaa
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save uupaa/32ad2969106a2c02efbc to your computer and use it in GitHub Desktop.

Select an option

Save uupaa/32ad2969106a2c02efbc to your computer and use it in GitHub Desktop.
node-webkit detection version 1

node-webkit detection methods

  • var A = /native/.test(setTimeout);
  • var B = typeof global !== "undefined" && !/native/.test(global.setTimeout);
  • var C = A && B;

test results

Environment A B C
node-webkit 0.11.5 true true true
Node.js 0.10.33 false true false
Chrome 39 true false false
Firefox 34 true false false
Safari 8 true false false
IE 11 true false false

試作

(function(global) {
    var _runOnNode = false, _runOnWorker = false, _runOnBrowser = false, _runOnNodeWebKit = false;
    if (global.global) { // Node or NodeWebKit
        _runOnNodeWebKit = /native/.test(setTimeout);
        _runOnNode = !_runOnNodeWebKit;
    } else {
        _runOnWorker = "WorkerLocation" in global;
        _runOnBrowser = "document" in global;
    }
    console.log({ nw: _runOnNodeWebKit, node: _runOnNode, thread: _runOnWorker, browser: _runOnBrowser });

})((this || 0).self || global);

WebModulePattern に落とし込んだ最終形

(function(global) {

// --- define / local variables ----------------------------
// var _isNodeOrNodeWebKit = !!global.global;
// var _runOnNodeWebKit =  _isNodeOrNodeWebKit && /native/.test(setTimeout);
// var _runOnNode       =  _isNodeOrNodeWebKit && !/native/.test(setTimeout);
// var _runOnWorker     = !_isNodeOrNodeWebKit && "WorkerLocation" in global;
// var _runOnBrowser    = !_isNodeOrNodeWebKit && "document" in global;

// 動作環境確認用
// console.log({ nw: _runOnNodeWebKit, node: _runOnNode, worker: _runOnWorker, browser: _runOnBrowser });

})((this || 0).self || global);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment