使用 Nightmare 从渲染器进程向节点进程发送消息
Send message from renderer process to node process with Nightmare
如何在 Nightmare 中将消息从渲染进程(网页)发送到主进程(节点)? Electron 中有 ipc,而 Nightmare 是建立在 Electron 之上的,所以我认为这是可能的,但我不知道如何实现。
类似的东西:
<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');
});
如果您尝试从页面获取数据,为什么不使用 .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, which I tried to fix in Nightmare #367. It was determined that adding arbitrary events should be a plugin under #425. The plugin PR has not been accepted, but the plugin for the plugin fork is already completed.
中讨论了将任意事件添加到 Nightmare 的核心
如何在 Nightmare 中将消息从渲染进程(网页)发送到主进程(节点)? Electron 中有 ipc,而 Nightmare 是建立在 Electron 之上的,所以我认为这是可能的,但我不知道如何实现。
类似的东西:
<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');
});
如果您尝试从页面获取数据,为什么不使用 .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, which I tried to fix in Nightmare #367. It was determined that adding arbitrary events should be a plugin under #425. The plugin PR has not been accepted, but the plugin for the plugin fork is already completed.
中讨论了将任意事件添加到 Nightmare 的核心