2016-04-23 26 views
12

可用当节点上下文(node-main)执行时,检查时的WebKit上下文中NW.js

setTimeout(function() { 
    console.log(nw); 
}, 20); 

抛出因为WebKit的上下文是

NW是没有定义

未准备好(从头开始window在NW.js中不可用< = 0.12,window.nw,在NW.js> = 0.13中)。而

setTimeout(function() { 
    console.log(nw); 
}, 200); 

工作得很好,但setTimeout看起来像一个黑客,将其设置为安全延迟值可能会导致不良的滞后性。

如何从Node上下文中检查WebKit上下文和nw的可用性?有没有合理的方法,就像一个可以处理的事件?

+0

一旦WebKit加载选项,是否正在运行您的节点主功能? –

+0

@RahatMahbub我希望有一个官方(也许没有文档)的方式来完成这个工作,就像全局事件一样。这是有道理的,但如果没有,那么从html页面回调是唯一的选择,我想。 – estus

+0

@estus有办法做到这一点,但我不认为这是一个特别干净的方式。如果您愿意,我可以将其添加到我的答案中。 – Harry

回答

1

的解决方案,我开始拿出貌似

APP-的node.js

process.once('webkit',() => { 
    console.log(nw); 
}); 

app.html

<html> 
<head> 
    <script> 
     global.process.emit('webkit'); 
    </script> 
    ... 

我很高兴知道已经有一个事件要监听,所以跨平台的客户端脚本可能会忽略与NW相关的代码。

2

以下实现相同的事情,但反过来。

在HTML文件中:

<body onload="process.mainModule.exports.init()">

在您的节点主要JS文件:

exports.init = function() { console.log(nw); }

这里,当Webkit的语境/ DOM是存在的初始化函数只调用。

2

你可以使用pollit :) ...

var pit = require("pollit"); 
foo = function(data) { 
    console.log(nw); 
}; 
pit.nw('nw', foo);  

我测试过它,它为我工作:)。这模块化解决方案,我给这个附近。

在webkit启动并运行前,nw对象才存在,即浏览器 窗口已创建。这发生在节点启动后,这就是为什么你 得到这个错误。要使用nw api,您可以创建可以听取 的事件或调用全局函数,前者更可取。下面的代码将演示两者并且应该给你一个关于NodeWebKit如何相互连接的好主意。

本示例创建一个窗口,打开devtools并允许您切换 屏幕。它还显示控制台中的鼠标位置。它还演示了如何使用DOM发送事件,即body.onclick()和附加Node内的事件,即我们将捕获minimize事件并将它们写入控制台。

为此,您需要使用SDK版本NW。这是我的包裹。JSON

{ 
    "name": "hello", 
    "node-main": "index.js", 
    "main": "index.html", 
    "window": { 
    "toolbar": true, 
    "width": 800, 
    "height": 600 
    }, 
    "dependencies" : { 
    "robotjs" : "*", 
    "markdown" : "*" 
    } 
} 

你需要两个文件是index.html

<!DOCTYPE html> 
<html> 
    <head> 
    <script> 
     var win = nw.Window.get(); 
     global.win = win; 
     global.console = console; 
     global.main(nw); 
     global.mouse(); 
     var markdown = require('markdown').markdown; 
     document.write(markdown.toHTML("-->Click between the arrows to toggle full screen<---")); 
    </script> 
    </head> 
    <body onclick="global.mouse();"> 
    </body> 
</html> 

index.js

var robot = require("robotjs"); 

global.mouse = function() { 
    var mouse = robot.getMousePos(); 
    console.log("Mouse is at x:" + mouse.x + " y:" + mouse.y); 
    global.win.toggleFullscreen(); 
} 

global.main = function(nw_passed_in) { 
    global.win.showDevTools(); 
    console.log("Starting main"); 
    console.log(nw_passed_in); 
    console.log(nw); 
    global.win.on('minimize', function() { 
    console.log('n: Window is minimized from Node'); 
    }); 
} 

当运行这个我用

nwjs --enable-logging --remote-debugging-port=1729 ./ 

使用

http://localhost:1729/ 

如果需要,可以再打开浏览器进行调试。

如果您想在nw对象存在的情况下立即执行某些操作,则可以轮询它。我会使用eventEmitter,如果你不想使用事件发射器,你可以轻松地把它包装在一个函数中,并递归地调用它。以下将显示在设置nw对象之前所花费的毫秒数。在我的系统上,这个范围在43 - 48毫秒之间。使用递归函数没有什么不同。如果您将此添加到上面的代码中,您会看到记录到控制台的所有内容。

var start = new Date().getTime(); 
var events = require('events'); 
var e = new events.EventEmitter(); 

var stop = 0; 
e.on('foo', function() { 
if(typeof nw === 'undefined') { 
    setTimeout(function() { 
    e.emit('is_nw_defined'); 
    }, 1); 
} 
else { 
    if(stop === 0) { 
    stop = new Date().getTime(); 
    } 
    setTimeout(function() { 
    console.log(stop - start); 
    console.log(nw); 
    e.emit('is_nw_defined'); 
    }, 2000); 
} 
}); 
e.emit('is_nw_defined'); 
+0

感谢'pollit'软件包,看起来像一个黑客,但一个很好,性能受到影响,过度延迟几乎为零。 – estus

1

解决方案1:

您可以使用onloadsee reference

main.js:

var gui = require("nw.gui"), 
    win = gui.Window.get(); 

onload = function() { 
    console.log("loaded"); 
    console.log(win.nw); 
}; 

的index.html:

<!DOCTYPE html> 
<html> 
    <head> 
     <script type="text/javascript" src="main.js"></script> 
    </head> 
    <body></body> 
</html> 

的package.json:

{ 
    "name": "Freebox", 
    "main": "index.html" 
} 

解决方案2:

(为了防止问题,但没有必要)。

var gui = require("nw.gui"), 
    win = gui.Window.get(); 

onload = function() { 
    console.log("loaded"); 
    var a = function() { 
     if (!win.nw) return setTimeout(a, 10); 
     console.log(win.nw); 
    }; 
}; 
+0

这种解决方案是错误的。 require('nw.gui')是获取nw对象的传统方式。你也错过了OP关于上下文的观点,在这个答案的任何一点你都不在节点上下文中。 – Harry

+0

@它是一个例子。 –