Is it possible to set up an event listen for phantomjs's system.stdin
events?
example where phantomjs can read from stdin but halts everything while waiting for input:
var system = require('system');
system.stdout.writeLine('waiting for user input...');
var input = system.stdin.readLine();
// anything down here won't execute until input has been received
The ideal solution would be like nodejs's process.stdin.on()
phantomjs does not seem to give any methods to do this but it must still be possible.
I was thinking I could use fs.open()
and poll the stream for changes but I can't figure out how to open the right file for stdio/tty or if that's even relevant to what I'm trying to do.
I'm using phantomjs version 1.9.0 on Debian 6
I am not sure if this is the right way to do it or even if it is great performance-wise, however it worked fine with the exception that a first STDIN request must be made for the program so it initialises.
console.log('initialising program, waiting for user input');
var stdin = require('fs').open('/dev/stdin', 'r');
setInterval(function () {
var line = stdin.readLine();
if (line) { console.log('>>', line); }
}, 100);
console.log('logic running async');
then you can create a FIFO pipe and attach it to the process
$ mkfifo PIPE
$ phantomjs test.js < PIPE
and from there on another terminal window you could:
echo hello world > PIPE
the ouput would be:
initialising program
logic running async
>> hello world
I don't think this is going to work on windows machines. and again, this might be really bad performance-wise since it uses a defined interval to catch user's input, but if I want to debug a program it is a quick and easy way to do so, by evaluate the input from there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With