Last active
August 29, 2015 14:01
-
-
Save chitacan/f0e6f5d548022ce7ec98 to your computer and use it in GitHub Desktop.
short script to request android chrome's tab info via adb.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Before run this script, you should connect your android device on your machine. | |
var net = require('net'); | |
var cmds = [ | |
{ | |
cmd : 'host:transport-any', | |
expect : function(res) { | |
return 'OKAY' === res; | |
} | |
}, | |
{ | |
cmd : 'localabstract:chrome_devtools_remote', | |
expect : function(res) { | |
return 'OKAY' === res; | |
} | |
}, | |
{ | |
cmd : 'GET /json HTTP/1.1\r\n\r\n', | |
expect : function(res) { | |
return true; | |
} | |
} | |
] | |
function prefixLen(cmd) { | |
var hexlen = cmd.length.toString(16); | |
var prefix = ('000'.concat(hexlen)).slice(-4); | |
return prefix.concat(cmd); | |
} | |
var socket = new net.Socket({ | |
readable : true, | |
writable : true, | |
allowHalfOpen : true | |
}); | |
socket.writeData = function() { | |
var data = this.cmds.shift(); | |
if (!data) return; | |
this.expect = data.expect; | |
this.write(prefixLen(data.cmd)); | |
} | |
socket.writeNext = function(res) { | |
if (this.expect && this.expect(res)) | |
this.writeData(); | |
} | |
socket.connect({port: '5037'}, function() { | |
this.cmds = cmds; | |
this.writeData(); | |
this.on('data', function(chunk) { | |
var res = chunk.toString(); | |
console.log(res); | |
this.writeNext(res); | |
}); | |
this.on('drain', function() { | |
console.log('drain'); | |
}); | |
this.on('end', function() { | |
console.log('FIN'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment