如何在HTTP协议中使用Google Chrome远程调试协议

How to use Google Chrome Remote Debugging Protocol in HTTP?

本文关键字:协议 程调试 调试 Chrome HTTP Google      更新时间:2023-09-26

我参考了http://code.google.com/chrome/devtools/docs/remote-debugging.html。

首先,我开始了一个新的镀铬过程。

chrome --remote-debugging-port=9222 --user-data-dir=remote-profile

然后我想尝试一些选项写在http://code.google.com/intl/ja/chrome/devtools/docs/protocol/tot/index.html,但我怎么能使用它们?我已经知道如何在WebSocket中使用这些方法,但我必须在HTTP中使用它。

我尝试了这个nodejs代码,但是失败了。

var http = require('http');
var options = {
  host: 'localhost',
  port: 9222,
  path: '/devtools/page/0',
  method: 'POST'
};
var req = http.request(options, function (res) {
  console.log(res.headers);
  res.on('data', function (chunk) {
    console.log(chunk);
  });
});
req.on('error', function (e) { console.log('problem' + e.message); });
req.write(JSON.stringify({
  'id': 1,
  'method': "Page.enable"
}));
req.end();

错了吗?

我知道这是一个相当老的问题,但是当我试图做类似的事情时,我遇到了它。

有一个名为chrome-remote-interface的npm模块,它使使用Chrome远程调试API变得更加容易:https://github.com/cyrus-and/chrome-remote-interface

npm install chrome-remote-interface
然后你可以像这样在代码中使用这个模块:
    var Chrome = require('chrome-remote-interface');
    Chrome(function (chrome) {
        with (chrome) {
            on('Network.requestWillBeSent', function (message) {
                console.log(message.request.url);
            });
            on('Page.loadEventFired', close);
            Network.enable();
            Page.enable();
            Page.navigate({'url': 'https://github.com'});
        }
    }).on('error', function () {
        console.error('Cannot connect to Chrome');
    });

我想它的意思是"请注意,我们目前正在公开一个不需要客户端WebSocket实现的基于http的协议。"

我不确定这意味着你现在可以有HTTP而不是WebSocket

还有一个很棒的NPM模块叫做Weinre,它可以让你轻松地使用Chrome调试/远程调试工具。如果你必须跨浏览器测试,它允许你使用Chrome工具,甚至在某些版本的IE。更多信息请访问MSDN博客