Skip to content
This repository has been archived by the owner on Oct 27, 2019. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
new demo loads a url and pipes the console to node
  • Loading branch information
subtleGradient committed Oct 28, 2013
1 parent a010405 commit e9e1313
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 11 deletions.
87 changes: 87 additions & 0 deletions demo-console-pipe.js
@@ -0,0 +1,87 @@
#!/usr/bin/env node
/*jshint asi:true*/

var async = require('async')
var createHeadlessInspector = require('./lib/HeadlessInspector').createHeadlessInspector

exports.repl = function(){

console.log('Launching HeadlessInspector for Google Chrome Canary...')
console.log(' everything you type in this repl will be executed in the context of the currently open window')

var child_process = require('child_process')
var http = require('http')
var url = require('url')
var repl = require("repl")

launchChrome(['--homepage=about:blank'], function(error, chrome, json){
if (error) throw Error(error)

var webSocketDebuggerUrl = json[0].webSocketDebuggerUrl
createHeadlessInspector({ws:webSocketDebuggerUrl}, function(error, inspector){
if (error) console.error(error);

inspector.ConsoleAgent.enable()
var lastConsoleMessage
inspector.on("Console.messageAdded", function(message){
lastConsoleMessage = message
console[lastConsoleMessage.type](lastConsoleMessage.text)
})
inspector.on("Console.messageRepeatCountUpdated", function(count){
lastConsoleMessage.repeatCount = count
console[lastConsoleMessage.type](lastConsoleMessage.text)
})

function remoteEval(code, callback){
inspector.RuntimeAgent.evaluate(code, function(error, result, wasThrown){
if (error) return callback(error, result)
if (wasThrown) return callback(JSON.stringify(result,null,2), result)
callback(null, result)
})
}

remoteEval('location = ' + JSON.stringify(process.argv[2] || 'http://localhost:8080/'), function(){
console.warn(arguments)
})

})
})

function launchChrome(args, callback){
// http://peter.sh/experiments/chromium-command-line-switches/
var bin = '/Applications/Google Chrome Canary.app/Contents/MacOS/Google Chrome Canary'
args = args.concat(['--no-first-run', '--remote-debugging-port=9223', "--js-flags=--expose-gc", '--enable-memory-info', '--user-data-dir='+process.env.TMPDIR+'/.chrome-user-data',
'about:blank'
])
args.reverse()
console.warn()
console.warn('bin', bin)
console.warn('args', args)
var chrome = child_process.execFile(bin, args)
chrome.on('exit', function(exitCode){
console.log(bin, 'exited with', exitCode)
process.exit(exitCode)
})
process.on('exit', function(){
chrome.kill()
})
// process.on('uncaughtException', function(error){
// chrome.kill()
// console.error('uncaughtException', error)
// })
setTimeout(function(){
http.get(url.parse('http://localhost:9223/json'), function(response){
response.on('data', function(data){
callback(null, chrome, JSON.parse(''+data))
})
}).on('error', function(error){
chrome.kill()
callback(error)
})
}, 250)
}

}

////////////////////////////////////////////////////////////////////////////////
if (module.id == '.') exports.repl()
28 changes: 17 additions & 11 deletions package.json
@@ -1,16 +1,22 @@
{
"name":"headless-inspector",
"version":"0.1.0",
"description":"headless webkit remote inspector client",
"name": "headless-inspector",
"version": "0.1.1",
"description": "headless webkit remote inspector client",
"author": "Thomas Aylott <thomas@subtlegradient.com> (http://subtlegradient.com/)",
"keywords": [ "websocket", "webkit", "inspector", "debug" ],
"repository": { "type": "git", "url": "https://github.com/subtleGradient/node-headless-inspector.git" },
"bugs": "https://github.com/subtleGradient/node-headless-inspector/issues",
"dependencies":{
"ws":"~0.4"
"keywords": [
"websocket",
"webkit",
"inspector",
"debug"
],
"repository": {
"type": "git",
"url": "https://github.com/subtleGradient/node-headless-inspector.git"
},
"devDependencies":{
"bugs": "https://github.com/subtleGradient/node-headless-inspector/issues",
"dependencies": {
"ws": "~0.4"
},
"license":"MIT",
"copyright":"Copyright © 2012 Facebook"
"license": "MIT",
"copyright": "Copyright © 2012 Facebook"
}

0 comments on commit e9e1313

Please sign in to comment.