2

Imagine a script like

system = require "system"

system.stdout.write "What's your name? "
name = system.stdin.readLine()
system.stdout.writeLine "Hello, #{name}" 

To be run via

casperjs name.coffee

I'd like to be able to interact with the user in the terminal used to run the script, but I get stuck in the readLine() call.

4

2 回答 2

2

正如GarethOwen 所指出的,这确实是可能的。这是 Unix 命令的一个非常基本的 CasperJS 实现cat

var system = require('system'),
    casper = require('casper').create();
while (!system.stdin.atEnd()) {
    var line = system.stdin.readLine();
    casper.log(line);
}
casper.exit();

请注意,此模块主要在 C++ 中实现: https ://github.com/ariya/phantomjs/blob/master/src/system.h

stdin//是 PhantomJs 类的实例stdouthttps : //github.com/ariya/phantomjs/blob/master/src/filesystem.hstderrFile

于 2015-07-20T22:36:18.657 回答
1

根据文档,phantomJS 可以与标准输入通信。看这个例子:

https://github.com/ariya/phantomjs/blob/master/examples/stdin-stdout-stderr.js

关于进程间通信的文档在这里:

https://github.com/ariya/phantomjs/wiki/Inter-Process-Communication

但我自己从未尝试过。

于 2013-07-10T08:08:31.240 回答