简体   繁体   English

如何动态地将外部输入注入SlimerJS脚本?

[英]How to inject external input into a SlimerJS script dynamically?

I need some help with SlimerJS. 我需要SlimerJS的帮助。 Basically my program needs to take input from stdin periodically to do the next task. 基本上,我的程序需要定期从stdin接收输入以执行下一个任务。

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. 在从外部输入读取的情况下,以下代码可与PhantomJS + CasperJS很好地配合使用,但在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 . GitHub上功能请求以及文档中缺少stdin属性显然不支持此功能

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). 两者都要求您使用所选语言编写第二个程序(可以是PhantomJS脚本)。

  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. 当您到达CasperJS脚本中必须获取输入的位置时,您将多次读取预定义的文件,直到其中包含某些内容。 So you execute the second program when you see that CasperJS is at this point. 因此,当您看到CasperJS此时就执行第二个程序。 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: CasperJS中的轮询功能将如下所示:

     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. 有一个Web服务器模块 ,当它作为CasperJS脚本运行时,可以用来向SlimerJS发送消息。 The second program would need to send actual requests. 第二个程序将需要发送实际的请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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