brenden / node-webshot

Easy website screenshots in Node.js
2.12k stars 286 forks source link

Retrieving content of console log of webshot #98

Open DerekDomino opened 9 years ago

DerekDomino commented 9 years ago

Hello,

Is there a way to get back the content of the messages logged to the console by the frontend-level javascript run by webshot?

I would like to debug javascript code run by webshot.

Thanks

LorenzGardner commented 9 years ago

Yes, and No. There isn't a built in option for webshot, but it is a feature of phantomJS so you can get ot it with minimal effort. I've done it a few times now.

You need to open up the webshot.phantom.js, and modify the code to add a listener to the page object for the onConsoleMessage event from phatomjs. Simply adding a console.log to that handler should do. Note, that only the msg argument is currently used, you will always get undefined for the others (line number and source id).

For reference here is the error handler you might want to stick a console.log into: https://github.com/brenden/node-webshot/blob/master/lib/webshot.phantom.js#L24

And you can also add a onConsoleMessage listener right bellow that. Documentation on that here: http://phantomjs.org/api/webpage/handler/on-console-message.html