本文介绍了沟通“退出”来自Chromium通过DevTools协议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面在一个无头Chromium实例中运行,我通过DevTools协议操纵它,使用Node中的Puppeteer NPM包。



m将脚本注入页面。在某些时候,我希望脚本给我回电话并给我发送一些信息(通过DevTools协议或其他方式公开的某些事件)。

什么是最好的方法来做到这一点?如果可以使用Puppeteer来完成它会很棒,但我并不反对让自己的手变脏,并且手动收听协议消息。



我知道我可以通过操纵DOM并监听DOM更改来完成此操作,但这听起来不是一个好主意。解析方案

>好的,我发现了一个内置的方法来在 Puppeteer 中执行此操作。 Puppeteer 定义了一种名为。

  page.exposeFunction(name,puppeteerFunction)

该方法在页面的窗口对象中定义一个具有给定名称的函数。该函数在页面上是异步的。当它被调用时,你定义的 puppeteerFunction 被作为一个回调执行,具有相同的参数。参数不是JSON序列化的,而是作为 JSHandles 传递的,所以它们暴露了对象本身。就我个人而言,我在发送它们之前选择了JSON序列化值。



我查看了代码,它实际上只是通过发送控制台消息来工作,就像Pasi的答案,木偶控制台挂钩忽略。但是,如果您直接听控制台(即通过管道 stdout )。您仍然可以看到它们以及常规消息。



由于控制台信息实际上是由WebSocket发送的,所以它非常高效。我有点反对使用它,因为在大多数进程中,控制台通过stdout传输有问题的数据。

示例



Node



 异步函数示例(){
const puppeteer = require(puppeteer);
let browser = await puppeteer.launch({
// arguments
});
让page =等待browser.newPage();

等待page.exposeFunction(callPuppeteer,function(data){
console.log(Node receive some data!,data);
});

等待page.goto(http://www.example.com/target);



Page



在页面的javascript内:

  window.callPuppeteer(JSON.stringify({
thisCameFromThePage:hello!
}));


I have a page running in a headless Chromium instance, and I'm manipulating it via the DevTools protocol, using the Puppeteer NPM package in Node.

I'm injecting a script into the page. At some point, I want the script to call me back and send me some information (via some event exposed by the DevTools protocol or some other means).

What is the best way to do this? It'd be great if it can be done using Puppeteer, but I'm not against getting my hands dirty and listening for protocol messages by hand.

I know I can sort-of do this by manipulating the DOM and listening to DOM changes, but that doesn't sound like a good idea.

解决方案

Okay, I've discovered a built-in way to do this in Puppeteer. Puppeteer defines a method called exposeFunction.

page.exposeFunction(name, puppeteerFunction)

This method defines a function with the given name on the window object of the page. The function is async on the page's side. When it's called, the puppeteerFunction you define is executed as a callback, with the same arguments. The arguments aren't JSON-serialized, but passed as JSHandles so they expose the objects themselves. Personally, I chose to JSON-serialize the values before sending them.

I've looked at the code, and it actually just works by sending console messages, just like in Pasi's answer, which the Puppeteer console hooks ignore. However, if you listen to the console directly (i.e. by piping stdout). You'll still see them, along with the regular messages.

Since the console information is actually sent by WebSocket, it's pretty efficient. I was a bit averse to using it because in most processes, the console transfers data via stdout which has issues.

Example

Node

async function example() {
    const puppeteer = require("puppeteer");
    let browser = await puppeteer.launch({
        //arguments
    });
    let page = await browser.newPage();

    await page.exposeFunction("callPuppeteer", function(data) {
        console.log("Node receives some data!", data);
    });

    await page.goto("http://www.example.com/target");
}

Page

Inside the page's javascript:

window.callPuppeteer(JSON.stringify({
    thisCameFromThePage : "hello!"
}));

这篇关于沟通“退出”来自Chromium通过DevTools协议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 04:33