Reputation: 845
I was trying to search some way to create more than one instance of 'window' and then having communicated these windows instances using some messages. Then I came across link here. But I am not sure if we can pass some object at run time using script. Any help would be appreciated.
Thanks
Upvotes: 1
Views: 3478
Reputation: 113
My deepest apologies to alexpods but stackoverflow won't let me add a comment so instead I have to lift your entire answer and make modifications to it.
In nw.js at least (most?) 0.19.0, emit no longer works, returning the handler for gui.Window.open doesn't work, and .postMessage requires 2 arguments.
So here is an updated answer, a modified version of alexpods (because I can't add comments to his answer). Also thank you for your answer alexpods because it definitely helped me.
Use postMessage with JSON.stringify and JSON.parse. You can use any of these techniques in node-webkit. So in current window you add event listener to "message" event and open new window :
First window
var gui = require('nw.gui');
var newWindow; //we declare a global variable to store the handler for our new window
window.addEventListener('message', function(event) {
var data = JSON.parse(event.data);
console.log(data);
}, false);
gui.Window.open('/some/path', {}, function(newWin) {
newWindow = newWin;
newWindow.on ('loaded', function(){ //We only post a message when the new window is ready to listen to events.
newWindow.window.postMessage(JSON.stringify({ some: 'very important data1' }), "*"); //the second parameter specifies the message origin. afaik, this is merely a string and has no effect beyond being there
});
});
In the new opened window add event listener to 'message' event:
New opened window
window.addEventListener('message', function(event) {
var data = JSON.parse(event.data);
console.log(data);
}, false);
Now you can communicate between these two windows using postMessage. For example:
First window
You'll notice that the postMessage shown above is wrapped in a 'loaded' event, which is itself wrapped in a onComplete event. This is because there is no guarantee that by the time it is called, that newWindow is ready to receive events, or that it has even been assigned the window handle yet, respectively. If you don't want to put your code there, you may probably want to at least check that the the window has already been created.
if (typeof(newWindow) != 'undefined') {
if (newWindow.window.document.readyState == "complete") {
newWindow.window.postMessage(JSON.stringify({ some: 'very important data1' }), "*");
}
}
New opened window
window.opener.postMessage(JSON.stringify({ some: 'very important data2' }), "*");
Upvotes: 2
Reputation: 605
nw.js has an EventEmmitter on each window object.
// mainWindow.html
var gui = require('nw.gui');
var newWindow = gui.Window.open('test.html');
var yourObject = {msg: "lorem ipsum"}
newWindow.emit("testEvent", yourObject);
// test.html
var gui = require('nw.gui');
var win = gui.Window.get();
win.on("testEvent", function(data) {
console.log(data.msg);
// lorem ipsum
});
Upvotes: 1
Reputation: 48485
Use postMessage
with JSON.stringify
and JSON.parse
. You can use any of these techniques in node-webkit
. So in current window you add event listener to "message" event and open new window :
First window
var gui = require('nw.gui');
window.addEventListener('message', function(event) {
var data = JSON.parse(event.data);
console.log(data);
}, false);
var newWindow = gui.Window.open('/some/path');
In the new opened window add event listener to 'message' event:
New opened window
window.addEventListener('message', function(event) {
var data = JSON.parse(event.data);
console.log(data);
}, false);
Now you can communicate between these two windows using postMessage
. For example:
First window
newWindow.window.postMessage(JSON.stringify({ some: 'very important data1' }));
New opened window
window.opener.postMessage(JSON.stringify({ some: 'very important data2' }));
Upvotes: 1