【问题标题】:Is there a way to access and control "Remote Devices" section of Chrome Devtools programmatically?有没有办法以编程方式访问和控制 Chrome Devtools 的“远程设备”部分?
【发布时间】:2019-09-01 06:16:40
【问题描述】:

我了解 Chrome Devtools 协议通过 API 公开 Google Chrome DevTools 以进行编程控制,并且 puppeteer 提供了一个 Node.js 实现来执行此操作。但是,如果我没记错的话,puppeteer 不允许我们做 所有 DevTools 协议可以做的事情。

如果我想远程调试我的 android chrome 浏览器,就像我可以手动那样,通过转到我的 DevTools 的“远程设备”部分,有没有办法绕过 puppeteer?是否有一个包可以让我们访问 DevTools 协议的更多核心功能?

【问题讨论】:

    标签: node.js google-chrome-devtools puppeteer


    【解决方案1】:

    是的,你可以这样做。 puppeteer 只是 Chrome DevTools Protocol 的包装。要了解该协议的工作原理,您可能需要查看 Protocol Fundamentals,其中解释了基础知识。

    要在这个较低级别进行通信,您还可以使用 puppeteer。查看CDPSession 文档,该文档公开了 API 的“低级”部分。

    小例子:

    const client = await page.target().createCDPSession(); // creates a "low level" session
    await client.send('COMMAND'); // sends the command to the browser
    client.on('MESSAGE', () => { /* message from the browser received */ });
    

    替代品

    如果您不想使用 puppeteer,还有多个其他库。 Node.js 使用最多的是chrome-remote-interface,比puppeteer 更底层。根据to the docs

    chrome-remote-interface [...] 是 Chrome 调试协议的通用 1:1 Node.js 绑定。如果您需要原始协议的所有功能,例如实现您自己的高级 API,请使用它。

    还有多个其他库(也适用于其他语言)。你可以找到full list here

    【讨论】:

    • 这看起来像我可以使用的东西。谢谢!您能帮我列出 client.send('COMMAND') 可以接受的参数列表吗?我只能在 github repo 上找到一个示例,即 await session.send('Animation.enable');
    • 您可以在Chrome DevTools Protocol Viewer 中找到完整列表。举个例子,你可以找到Animation.enablehere
    • 我尝试手动找到解决方案。我在无头模式下运行 chrome 并在 localhost:9222 开始调试会话。虽然我能够看到针对其他 devtools 选项卡(如网络、内存、审计等)的事件,但我没有看到“远程设备”选项卡的任何事件。附加调试显示如下错误 Uncaught TypeError: Cannot set property 'discoverUsbDevices' of undefined at HTMLInputElement.eval 当我点击复选框“发现远程设备”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-29
    • 2020-10-22
    • 2018-10-25
    • 1970-01-01
    • 2015-04-03
    • 1970-01-01
    相关资源
    最近更新 更多