Reputation: 1775
I'm remote debugging Chrome, using the --remote-debugging-port
switch and running another Chrome instance from which I'm debugging using web sockets. That's a bit different from most examples I see online where the debugging is done using node.js.
I need to navigate to a page, then evaluating some code in it. Both these actions work as expected using this code, that I run in a page in the 2nd Chrome instance:
var socket = new WebSocket(url);
function debug(action, data){
var msg;
switch (action) {
case 'NAVIGATE':
msg = {'id':1001, 'method':'Page.navigate', 'params':{'url':data.urlToNavigate}};
break;
case 'EVALUATE':
msg = {'id':1002, 'method':'Runtime.evaluate', 'params':{'expression':data.jsCodeToEvaluate}};
break;
}
socket.send(JSON.stringify(msg));
}
The problem I have is with getting notifications. I want to evaluate the code only after navigation has completed (let's say, after the document has loaded). I get messages on the socket in the following way, but as I understand the return message of the Page.navigate
method is received when the navigation command has been started/received by the debugged page, not when the navigation completes.
socket.onmessage = function (event) {
var data = JSON.parse(event.data);
switch (data.id) {
case 1001:
//A navigation command to app page has been received but not yet navigated
break;
}
};
How can I use notifications to create a flow such as: Navigate > Wait for navigation to complete > Evaluate some code? I couldn't get any notifications.
Upvotes: 1
Views: 461
Reputation: 1775
The problem was that notifications weren't enabled. To enable notifications:
socket.send(JSON.stringify({'id':12345, 'method':'Page.enable'}));
Then, notifications are received this way:
socket.onmessage = function (event) {
var data = JSON.parse(event.data);
switch (data.method) {
case 'Page.frameNavigated':
console.log('Navigation completed');
break;
//....
}
}
Same goes for runtime notifications (Runtime.enable
), network notifications and so on...
Upvotes: 1