将cUrl与Chrome远程调试结合使用

时间:2018-10-12 16:33:40

标签: curl chrome-remote-debugging

我正在尝试使用cUrl连接到Chrome远程调试,但是Chrome的响应返回“ 200 OK”,但没有数据。

我使用以下方法设置chrome:

chrome.exe --headless --remote-debugging-port=12345

我可以使用以下方法成功获取页面列表:

>curl -i "http://localhost:12345/json"

HTTP/1.1 200 OK
Content-Length:612
Content-Type:application/json; charset=UTF-8
[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:12345/devtools/page/19d24d3a-25b7-4ee8-a5cf-4f3d17778575",
  ...
   "webSocketDebuggerUrl": "ws://localhost:12345/devtools/page/19d24d3a-25b7-4ee8-a5cf-4f3d17778575"
} ]

但是,我只能从调试器URL获得空的成功响应:

>echo {"id":0,"method":"Page.navigate","params":{"url":"https://stackoverflow.com/"}}|curl -i "http://localhost:12345/devtools/page/19d24d3a-25b7-4ee8-a5cf-4f3d17778575" -H "Content-Type: application/json" -d -

HTTP/1.1 200 OK
Content-Length:0
Content-Type:text/plain

诸如Page.navigate之类的命令以及格式错误的请求均返回200 OK,但什么也不执行。

我想念的是什么?

1 个答案:

答案 0 :(得分:1)

您需要使用websocket来管理chrome。引用chromedevtools.github.io

  

您的应用程序可以通过请求以下内容来发现可用页面:   http://localhost:9222/json并获取带有信息的JSON对象   关于可检查页面以及您的WebSocket地址   可以用来开始检测它们

这是它的工作方式(至少在我的Mac上是这样):

运行Chrome

docker pull deepsweet/chromium-headless-remote:69
docker run -it --rm -p 9222:9222 deepsweet/chromium-headless-remote:69

获取WebSocket地址

curl -i "http://localhost:9222/json"
HTTP/1.1 200 OK
Content-Length:361
Content-Type:application/json; charset=UTF-8

[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF",
   "id": "DC33B65CA373BE2770F2A1031C3B4CBF",
   "title": "about:blank",
   "type": "page",
   "url": "about:blank",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF"
} ]

发送命令

echo '{ "id":2, "method":"Page.navigate", "params":{"url": "http://www.stackoverflow.com"} }' | websocat -t - ws://localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF
在示例中使用

websocat。您也可以编写一个简单的脚本来完成此操作,例如here

确保页面已打开

curl -i "http://localhost:9222/json"
HTTP/1.1 200 OK
Content-Length:432
Content-Type:application/json; charset=UTF-8

[ {
   "description": "",
   "devtoolsFrontendUrl": "/devtools/inspector.html?ws=localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF",
   "id": "DC33B65CA373BE2770F2A1031C3B4CBF",
   "title": "Stack Overflow - Where Developers Learn, Share, & Build Careers",
   "type": "page",
   "url": "https://stackoverflow.com/",
   "webSocketDebuggerUrl": "ws://localhost:9222/devtools/page/DC33B65CA373BE2770F2A1031C3B4CBF"
} ]