CDP / 扩展逃生舱
当 DevBridge 的 25 个标准工具无法覆盖你的需求时,可以使用 6 个逃生舱工具直接操作 Chrome DevTools Protocol(CDP)或调用 chrome.* 扩展 API。
先用标准工具
逃生舱工具功能强大,但操作更底层。优先使用 screenshot、computer、read_console、read_network 等专用工具——它们内置了错误处理、权限检查和重试逻辑。只有在标准工具确实无法覆盖时才使用逃生舱。
CDP 工具
cdp_command — 发送单次 CDP 命令
直接向 Chrome DevTools Protocol 发送任意命令并获取响应,适合一次性的请求/响应操作。
自省 _help: 传入 method: "_help" 可列出所有可用的 CDP 域;传入 method: "Network._help" 可查看 Network 域下的所有命令及其参数说明。
// 列出所有 CDP 域
{ "method": "_help" }
// 查看 Network 域的命令
{ "method": "Network._help" }
// 模拟设备(iPhone 14)
{
"method": "Emulation.setDeviceMetricsOverride",
"params": {
"width": 390,
"height": 844,
"deviceScaleFactor": 3,
"mobile": true
}
}
// 模拟 3G 网络
{
"method": "Network.emulateNetworkConditions",
"params": {
"offline": false,
"downloadThroughput": 375000,
"uploadThroughput": 125000,
"latency": 100
}
}cdp_subscribe — 订阅 CDP 事件
订阅 CDP 事件流,可设置 untilPredicate 在满足条件时自动停止,避免无限等待。
// 等待特定网络请求完成
{
"tabId": 123,
"eventName": "Network.responseReceived",
"untilPredicate": "event.response.url.includes('/api/data')",
"timeoutMs": 10000
}cdp_events_read — 拉取 CDP 历史事件
从 ring buffer 中拉取已捕获的 CDP 事件,无需事先订阅。适合事后回溯分析。
{
"tabId": 123,
"eventName": "Network.requestWillBeSent",
"limit": 50
}chrome.* 扩展工具
extension_call — 调用 chrome.* API
调用扩展白名单内的 chrome.* API 方法。对于返回 void 的方法,响应为 { success: true, void: true }。
支持的命名空间: chrome.storage、chrome.cookies、chrome.tabs、chrome.windows、chrome.bookmarks、chrome.history、chrome.downloads 等(具体白名单见扩展 manifest 权限声明)。
// 读取本地存储
{
"namespace": "storage.local",
"method": "get",
"args": [["myKey"]]
}
// 获取所有 cookies
{
"namespace": "cookies",
"method": "getAll",
"args": [{ "domain": "example.com" }]
}传入 method: "_help" 可查看该命名空间下可调用的方法列表:
{ "namespace": "storage.local", "method": "_help" }extension_subscribe — 订阅 chrome.* 事件
订阅 chrome.* 事件,与 cdp_subscribe 用法类似。
{
"namespace": "tabs",
"eventName": "onUpdated",
"untilPredicate": "event.changeInfo.status === 'complete'",
"timeoutMs": 15000
}extension_events_read — 拉取 chrome.* 历史事件
从 ring buffer 中拉取已捕获的 chrome.* 事件。
{
"namespace": "tabs",
"eventName": "onUpdated",
"limit": 20
}订阅 vs Pull 模式对比
| 场景 | 推荐工具 |
|---|---|
| 等待某个事件发生后再继续 | cdp_subscribe / extension_subscribe(配合 untilPredicate) |
| 事后查看发生了哪些事件 | cdp_events_read / extension_events_read |
| 发送一次命令并获取结果 | cdp_command / extension_call |
典型用例
- 模拟移动设备或网络条件:
cdp_command+Emulation.*/Network.emulateNetworkConditions - 读写扩展本地存储:
extension_call+storage.local.get / set - 等待 SPA 路由完成:
cdp_subscribe+Page.frameNavigated - 审计特定请求的完整 headers:
cdp_events_read+Network.requestWillBeSent - 监控 cookie 变化:
extension_subscribe+cookies.onChanged