【问题标题】:Send message from renderer process to node process with Nightmare使用 Nightmare 从渲染器进程向节点进程发送消息
【发布时间】:2016-07-05 15:26:24
【问题描述】:

如何在 Nightmare 中将消息从渲染器进程(网页)发送到主进程(节点)? Electron 中有ipc,Nightmare 是建立在 Electron 之上的,所以我想这是可能的,但我不知道怎么做。

类似的东西:

http://example.com

<script type="text/javascript">
 window.postMessage('aaaaaaaa', '*');
</script>

index.js

let nightmare = new Nightmare();
nightmare.on('message', function(e) {
 console.log(e.data); // will output aaaaaaaa
});

nightmare.goto('http://example.com').then(function() {
 console.log('loaded');
});

【问题讨论】:

  • 您能否提供更多有关您正在尝试做的事情的背景信息?
  • @Ross 我想加载可以从 javascript 向节点进程发布消息的页面,但我不知道这个 js 应该是什么样子。
  • 我应该更具体一点:您能提供示例代码来演示您正在尝试做什么吗?
  • 我已经更新了我的问题,希望现在已经清楚了。
  • 感谢您的更新。我现在尝试回答。

标签: javascript node.js electron nightmare


【解决方案1】:

如果您尝试从页面获取数据,为什么不使用.evaluate()?比如:

nightmare.goto('http://example.com')
    .evaluate(function(){
        var element = document.querySelector('some-element.query');
        return element.value;
    })
    .then(function(value){
        console.log(value);
    });

对于您的原始示例,您不能发送任意事件,直到 Nightmare 上允许插件向下到它包装的 Electron 实例。在 Nightmare #354 中讨论了将任意事件添加到 Nightmare 的核心,我试图在 Nightmare #367 中修复它。确定添加任意事件应该是#425下的插件。插件PR没有被接受,但是插件forkis already completed的插件。

【讨论】:

    猜你喜欢
    • 2022-01-11
    • 1970-01-01
    • 2019-02-07
    • 2022-01-27
    • 2014-10-08
    • 2018-05-29
    • 1970-01-01
    • 1970-01-01
    • 2016-12-12
    相关资源
    最近更新 更多