How to inject external input into a SlimerJS script dynamically?

669 views Asked by At

I need some help with SlimerJS. Basically my program needs to take input from stdin periodically to do the next task.

The following code works well with PhantomJS+CasperJS in case of reading from an external input, but fails to do the same in SlimerJS+CasperJS.

some code ...

var nextLink = system.stdin.readLine();

some code ...

Any thoughts about how to solve this problem ?

1

There are 1 answers

0
Artjom B. On

This simply isn't supported as evident by the feature request on GitHub and lack of stdin property in the documentation.

There are mainly two workarounds possible. Both of the require that you write a second program in the language of your choice (can be a PhantomJS script).

  1. When you come to the point in the CasperJS script where it is necessary to get input, you read a predefined file multiple times until something is inside it. So you execute the second program when you see that CasperJS is at this point. The program may be a simple command like

    echo my text > predefinedFile.txt
    

    or something more elaborate. The polling functions in CasperJS would be something like this:

    function poll(){
        var content = fs.read(predefinedFile).trim();
        if (!content) {
            this.wait(1000, poll);
        } else {
            // do something sensible
        }
    }
    //....
    casper.then(poll);
    
  2. There is a webserver module that you can use to send messages to SlimerJS when it is running as a CasperJS script. The second program would need to send actual requests.