[英]nwjs - call function on binary file
// source.js
function foo(){
alert("its work!");
}
// main.js
nw.Window.open("index.html", {}, function (win) {});
setTimeout(() => {
let win = nw.Window.get();
win.evalNWBin(null, "binary.bin");
foo();
}, 2000);
i run follow command to generate binary file:我运行以下命令来生成二进制文件:
nwjc source.js binary.bin
and when i run above code it shows this error:当我运行上面的代码时,它显示了这个错误:
Uncaught ReferenceError: foo is not defined
how can I call function on binary.bin from main js?如何从主js调用binary.bin上的function?
Because you are doing "main": "index.js"
in your package.json
, the code will run in the Node context and alert
is not a function.因为您在
package.json
中执行"main": "index.js"
,所以代码将在节点上下文中运行并且alert
不是 function。
If you instead do "main": "index.html"
then have如果你改为
"main": "index.html"
然后有
<script>
nw.Window.get().evalNWBin(null, 'binary.bin');
foo();
</script>
it will work fine.它会正常工作。
You can Right-Click > Inspect Background Page
to see the node process dev tools for console log errors.您可以
Right-Click > Inspect Background Page
以查看节点进程开发工具的控制台日志错误。
Alternatively, you can expose your function on the global window
object.或者,您可以在全局
window
object 上公开您的 function。
window.foo = function () {
alert('It works');
};
And you can wait for the App Window to load by putting your code in the callback function.您可以通过将代码放入回调 function 来等待应用 Window 加载。
nw.Window.open('index.html', {}, function (win) {
win.evalNWBin(null, 'binary.bin');
win.window.foo();
});
Then you don't need to "get" the window, you already have it.那么你不需要“得到”window,你已经拥有了。 And you can access its global
window
object to reach stuff stored on it.您可以访问其全局
window
object 以访问存储在其上的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.