chrome-remote-interface: Chrome remote debugging protocol interface for Node.js

3,357 views
Skip to first unread message

Andrea Cardaci

unread,
Apr 23, 2013, 10:59:05 AM4/23/13
to google-chrome-...@googlegroups.com
Hello,

I used the Remote Debugging Protocol to automate some tests and generate HAR files in a previous project of mine: chrome-har-capturer.

Now I decided to extract the part related to the protocol itself to another module: chrome-remote-interface. This is just a helper for the already quite simple WebSocket protocol, but it exposes a more suitable interface for a Node.js application and it also takes care of connecting to Chrome.

I found it useful for writing automation/data collecting scripts, here's the GitHub example:
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');
});
I hope someone else will find it useful and of course any feedback will be welcome.

Cheers,

Andrea

Andrea Cardaci

unread,
Sep 22, 2013, 11:20:43 AM9/22/13
to google-chrome-...@googlegroups.com
FYI: It now features a REPL interface (thanks to Andrey Sidorov).

Andrey Sidorov

unread,
Sep 23, 2013, 8:36:04 AM9/23/13
to google-chrome-...@googlegroups.com

Jethro Lam

unread,
Jun 3, 2014, 5:26:23 PM6/3/14
to google-chrome-...@googlegroups.com

Hi,

For a stress test I am trying to remotely command Chrome to start 10 tabs and have them all navigate the same URL at the same moment.  I am not a JS developer, just looking something that works quick and dirty.

Can you point me to the right direction (1) how to remotely open a new tab via chrome-remote-interface, (2) once the tabs are opened, how does "chooseTab" work?  For instance can you verify if this is correct?

Chrome({'chooseTab':(function(tabs){return 5;})}, function (chrome) {...})   // to control tab #5

Regards,
CJ Lam

Andrea Cardaci

unread,
Jun 3, 2014, 6:32:11 PM6/3/14
to google-chrome-...@googlegroups.com
Hi Jethro,
chrome-remote-interface itself is not able to open a new tab and AFAIK there is no method of the Remote Debugging Protocol to do that. I don't know your setup but you could try to inject some JavaScript code in the first opened tab to spawn 10 (popup) windows at the same time.

var Chrome = require('chrome-remote-interface');
Chrome(function (chrome) {
    with
 (chrome) {
        var url = 'http://example.com';
        var params = {
            'expression': 'for (var i = 0; i < 10; i++)' +
                'window.open("' + url + '", "_blank");'
        };
        Runtime.evaluate(params, function (error, response) {
            console.log(response);
            close();
        });
    }
}).on('error', function () {
    
console.error('Cannot connect to Chrome');
});

Good luck!

--
You received this message because you are subscribed to a topic in the Google Groups "Google Chrome Developer Tools" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/google-chrome-developer-tools/g7Ll04tuB3A/unsubscribe.

To unsubscribe from this group and all its topics, send an email to google-chrome-develo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-chrome-developer-tools/ec13973f-7703-4144-9701-20fe05edecef%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Chapunux

unread,
Nov 26, 2014, 11:24:27 PM11/26/14
to google-chrome-...@googlegroups.com
Currently I am converting c# module to node js. In c# I used below method to get innerHtml of particular div.

var json = @"{""method"":""Runtime.evaluate"",""params"":{""expression"":""document.getElementById('" + id + @"').innerHTML"",""objectGroup"":""console"",""includeCommandLineAPI"":true,""doNotPauseOnExceptions"":false,""returnByValue"":false},""id"":1}";

return this.SendCommand(json);

and I saw this forum and I am trying to use Runtime.evaluate() in chrome-remote-interface. But I am not getting response except some null error. 

Runtime.evaluate({'expression':'document.getElementById("divID").innerHTML','objectGroup':'console','includeCommandLineAPI':true,'doNotPauseOnExceptions':false,'returnByValue':false}, function (error, response) { console.log(response); close(); });

Can you help me to get innerHtml of that element.

Jurko 007

unread,
May 25, 2016, 9:22:20 AM5/25/16
to Google Chrome Developer Tools
Hello,

I am using chrome-remote-interface with nwjs. I am wondering, how would someone use chrome-remote-interface with multiple windows on one --remote-debugging-port browser at the same time? If it is possible to open new chromium windows with a specific port, that would be awesome, but I do not believe that is possible.

Any suggestions?

Jurko 007

unread,
May 25, 2016, 9:22:20 AM5/25/16
to Google Chrome Developer Tools
I am opening multiple windows on a nwjs instance and connecting to nwjs instance through --remote-debugging-port. Now my question is, is it possible to connect to multiple windows at the same time and evaluate them separately with this library?

I know it uses its debugging port to connect, but is it possible to set a debugging port to a window, so we could control its window with chrome-remote-interface?

Jurko 007

unread,
May 31, 2016, 10:39:28 AM5/31/16
to Google Chrome Developer Tools
Found a way. Every window is registered as a tab, so Chrome.List finds all windows, and we can connect to every separate window through its index or object.

Jurko 007

unread,
Jun 8, 2016, 4:22:50 AM6/8/16
to Google Chrome Developer Tools
Unfortunately cannot proceed with newer versions of nw.js, which are using newer versions of chromium. In the older version we can set url: file:/// but in new versions it doesn't work like that. 

This is the first tab info that is successfully opened:

{

description: "",

devtoolsFrontendUrl: "/devtools/inspector.html?ws=localhost:12406/devtools/page/8573B029-FCF9-4EB2-B8BA-A5D2FFCD5EE4",

faviconUrl: "chrome://extension-icon/laeohklakmaeckjcebinbjcagklmbcnm/24/1",

id: "8573B029-FCF9-4EB2-B8BA-A5D2FFCD5EE4",

title: "nw-guardian-app",

type: "app",

url: "chrome-extension://laeohklakmaeckjcebinbjcagklmbcnm/index.html",

webSocketDebuggerUrl: "ws://localhost:12406/devtools/page/8573B029-FCF9-4EB2-B8BA-A5D2FFCD5EE4"

}

This is how I currently open new windows:

Chrome.New({

  port: that.options.port,

  url: 'file:///appDir/index.html'

  }, function (err, tab) {

});


Reply all
Reply to author
Forward
0 new messages