Node-Webkit start function in different window

大憨熊 提交于 2019-12-10 15:38:39

问题


My node-webkit application consists of a control window and a presentation window.

The control window gathers data and eventually launches the presentation window via the window.open function.

The presentation window now has access to some information in the global variable.

Now I want to render a graphical representation of that information by creating SVG elements and so forth.

I already have a javascript function to do exactly that, but I need some way of starting that function from the control window.

I can't call it directly, since then the function has to access the other window's DOM.

I have tried using the eval function on the other window's object, but that crashes node-webkit with the message

[18719:0522/205047:ERROR:breakpad_linux.cc(1225)] crash dump file written to 
/tmp/chromium-renderer-minidump-788bf8d0c68301d5.dmp

What would be the best way to do this?

Use setInterval to regularly check a global variable?


回答1:


One solution I've found to be pretty effective is attaching pub/sub functionality to the globalvariable. The setup I've used so far is jQuery-based, though it could also be constructed without. First it is initialized using a variant of this code:

    var messages = messages || {};
    global.Message = global.Message || function( id ) {
      var callbacks, method,
      message = id && messages[ id ];
      if ( !message ) {
        callbacks = jQuery.Callbacks();
        message = {
          publish: callbacks.fire,
          subscribe: callbacks.add,
          unsubscribe: callbacks.remove
        };
        if ( id ) {
        messages[ id ] = message;
        }
      }
      return message;
    };

Then anywhere between the windows events can be published and subscribed to using the following pattern. One window can publish the data:

global.Message("someButtonClicked").publish(data);

Then the other can listen for it.

global.Message("someButtonClicked").subscribe(onButtonClicked);

function onButtonClicked(data) {
  console.log(data);
};



回答2:


You can inject and eval a piece of JS by using Window.eval():

https://github.com/rogerwang/node-webkit/wiki/Window#windowevalframe-script



来源:https://stackoverflow.com/questions/23815026/node-webkit-start-function-in-different-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!