chrome-cdp

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Chrome CDP

Chrome CDP

Lightweight Chrome DevTools Protocol CLI. Connects directly via WebSocket — no Puppeteer, works with 100+ tabs, instant connection.
轻量级Chrome DevTools Protocol命令行工具(CLI)。通过WebSocket直接连接——无需Puppeteer,支持100+标签页,连接即时。

Prerequisites

前置条件

  • Chrome with remote debugging enabled: open
    chrome://inspect/#remote-debugging
    and toggle the switch
  • Node.js 22+ (uses built-in WebSocket)
  • 启用远程调试的Chrome浏览器:打开
    chrome://inspect/#remote-debugging
    并开启开关
  • Node.js 22+(使用内置WebSocket)

Commands

命令

All commands use
scripts/cdp.mjs
. The
<target>
is a unique targetId prefix from
list
; copy the full prefix shown in the
list
output (for example
6BE827FA
). The CLI rejects ambiguous prefixes.
所有命令均使用
scripts/cdp.mjs
<target>
list
命令输出中唯一的targetId前缀;复制
list
输出中显示的完整前缀(例如
6BE827FA
)。CLI会拒绝模糊的前缀。

List open pages

列出已打开的页面

bash
scripts/cdp.mjs list
bash
scripts/cdp.mjs list

Take a screenshot

截取屏幕截图

bash
scripts/cdp.mjs shot <target> [file]    # default: /tmp/screenshot.png
Captures the viewport only. Scroll first with
eval
if you need content below the fold. Output includes the page's DPR and coordinate conversion hint (see Coordinates below).
bash
scripts/cdp.mjs shot <target> [file]    # 默认值:/tmp/screenshot.png
仅捕获视口区域。如果需要捕获折叠内容,请先使用
eval
命令滚动页面。输出内容包含页面的DPR(设备像素比)和坐标转换提示(见下方坐标部分)。

Accessibility tree snapshot

可访问性树快照

bash
scripts/cdp.mjs snap <target>
bash
scripts/cdp.mjs snap <target>

Evaluate JavaScript

执行JavaScript代码

bash
scripts/cdp.mjs eval <target> <expr>
Watch out: avoid index-based selection (
querySelectorAll(...)[i]
) across multiple
eval
calls when the DOM can change between them (e.g. after clicking Ignore, card indices shift). Collect all data in one
eval
or use stable selectors.
bash
scripts/cdp.mjs eval <target> <expr>
注意: 当DOM在多次
eval
调用之间可能发生变化时(例如点击“忽略”后,卡片索引会偏移),避免使用基于索引的选择器(
querySelectorAll(...)[i]
)。建议在单次
eval
调用中收集所有数据,或使用稳定的选择器。

Other commands

其他命令

bash
scripts/cdp.mjs html    <target> [selector]   # full page or element HTML
scripts/cdp.mjs nav     <target> <url>         # navigate and wait for load
scripts/cdp.mjs net     <target>               # resource timing entries
scripts/cdp.mjs click   <target> <selector>    # click element by CSS selector
scripts/cdp.mjs clickxy <target> <x> <y>       # click at CSS pixel coords
scripts/cdp.mjs type    <target> <text>         # Input.insertText at current focus; works in cross-origin iframes unlike eval
scripts/cdp.mjs loadall <target> <selector> [ms]  # click "load more" until gone (default 1500ms between clicks)
scripts/cdp.mjs evalraw <target> <method> [json]  # raw CDP command passthrough
scripts/cdp.mjs stop    [target]               # stop daemon(s)
bash
scripts/cdp.mjs html    <target> [selector]   # 完整页面或元素的HTML代码
scripts/cdp.mjs nav     <target> <url>         # 导航至指定URL并等待加载完成
scripts/cdp.mjs net     <target>               # 资源计时条目
scripts/cdp.mjs click   <target> <selector>    # 通过CSS选择器点击元素
scripts/cdp.mjs clickxy <target> <x> <y>       # 点击指定CSS像素坐标位置
scripts/cdp.mjs type    <target> <text>         # 在当前焦点位置执行Input.insertText;与eval不同,此命令可在跨源iframe中生效
scripts/cdp.mjs loadall <target> <selector> [ms]  # 点击“加载更多”按钮直到其消失(默认两次点击间隔1500ms)
scripts/cdp.mjs evalraw <target> <method> [json]  # 直接传递原始CDP命令
scripts/cdp.mjs stop    [target]               # 停止守护进程(可指定目标)

Coordinates

坐标

shot
saves an image at native resolution: image pixels = CSS pixels × DPR. CDP Input events (
clickxy
etc.) take CSS pixels.
CSS px = screenshot image px / DPR
shot
prints the DPR for the current page. Typical Retina (DPR=2): divide screenshot coords by 2.
shot
命令以原生分辨率保存图片:图片像素 = CSS像素 × DPR。CDP输入事件(如
clickxy
等)使用CSS像素
CSS像素 = 截图图片像素 / DPR
shot
命令会打印当前页面的DPR。典型的Retina屏幕(DPR=2):将截图坐标除以2即可得到CSS像素坐标。

Tips

提示

  • Prefer
    snap --compact
    over
    html
    for page structure.
  • Use
    type
    (not eval) to enter text in cross-origin iframes —
    click
    /
    clickxy
    to focus first, then
    type
    .
  • Chrome shows an "Allow debugging" modal once per tab on first access. A background daemon keeps the session alive so subsequent commands need no further approval. Daemons auto-exit after 20 minutes of inactivity.
  • 如需获取页面结构,优先使用
    snap --compact
    而非
    html
    命令。
  • 在跨源iframe中输入文本时,使用
    type
    命令(而非eval)——先使用
    click
    /
    clickxy
    命令聚焦输入框,再执行
    type
    命令。
  • Chrome会在首次访问每个标签页时显示“允许调试”弹窗。后台守护进程会保持会话活跃,因此后续命令无需再次获得批准。守护进程在闲置20分钟后会自动退出。