Is it possible to read from stdin with PhantomJS?

1,988 views
Skip to first unread message

Cameron Tinker

unread,
Dec 3, 2011, 3:23:41 PM12/3/11
to phantomjs
I'm writing a script that I'd like to leave running idle on my server
until it receives input from the caller through stdin. Is there any
simple way to read from stdin in PhantomJS? I believe that there is a
function for reading from stdin, but I'm unsure of how to implement
code to trap input from stdin. Thanks much.

Cameron Tinker

unread,
Dec 3, 2011, 11:33:40 PM12/3/11
to phantomjs
I found this code snippet from a bug report at
http://code.google.com/p/phantomjs/issues/detail?id=62:
var fs = require('fs'),
input = fs.open('/dev/stdin', 'r');
console.log(input.readLine());

It appears as though this code only works for unix systems. Is it
possible to add support for stdin in Windows? I'm running Windows
Server 2008 R2 and would like to be able to read from stdin using
PhantomJS in Windows.

I also found this pull request on Github:
https://github.com/jontuk/phantomjs/commit/c68ef82eb679feb87812e58a1d12546cbfd7b0ff
From this pull request, what would be some example code to use
getStdinLine()? Can this be invoked directly from the javascript API?
Thanks much.

Ariya Hidayat

unread,
Dec 4, 2011, 12:41:42 AM12/4/11
to phan...@googlegroups.com
This should be done via System module, see
http://code.google.com/p/phantomjs/issues/detail?id=270.

--
Ariya Hidayat, http://ariya.ofilabs.com

Cameron Tinker

unread,
Dec 8, 2011, 8:25:34 PM12/8/11
to phantomjs
I took a look at the System module, but I'm unsure of how to use it
with PhantomJS. Is there some example code to show how to use the
System module with PhantomJS? I tried requiring the System module by
using the code: require('system'), but I received an unknown module
exception. Please let me know if I need to install the module. Thanks
much.

Ariya Hidayat

unread,
Dec 8, 2011, 11:24:07 PM12/8/11
to phan...@googlegroups.com
> I took a look at the System module, but I'm unsure of how to use it
> with PhantomJS. Is there some example code to show how to use the
> System module with PhantomJS? I tried requiring the System module by
> using the code: require('system'), but I received an unknown module
> exception. Please let me know if I need to install the module. Thanks
> much.

You are not missing anything. It does not work because it's not implemented yet.

Cameron Tinker

unread,
Dec 9, 2011, 3:15:11 PM12/9/11
to phantomjs
I thought that was the case. Thanks for clarifying. I will look
forward to seeing this implemented in the future.

Kem Mason

unread,
Aug 5, 2016, 9:28:30 PM8/5/16
to phantomjs
    var system = require('system');
    system.stdout.write('enter password: ');
    var password = system.stdin.readLine();
    system.stdout.write("password: " + password);


I know this is an old thread, but google brought me here, this works on osx, not sure about Windows.
Reply all
Reply to author
Forward
0 new messages