简体   繁体   中英

How to inject external input into a SlimerJS script dynamically?

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 ?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM