javascript phantomJS - 将参数传递给 JS 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16814997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:05:19  来源:igfitidea点击:

phantomJS - Pass Argument to the JS File

javascriptphantomjs

提问by asprin

Right now I'm using the following command to run phantomJS

现在我正在使用以下命令运行 phantomJS

exec('./phantomjs table.js',$op,$er);

table.js

表.js

var page = require('webpage').create();
page.open('table.php', function () {
    page.render('table.png');
    phantom.exit();
});

This serves the purpose. But now I'm required to work with a dynamic variable, namely date. So is it possible to pass a PHP or Javascript variable inside the execcommand line so that I can use that variable inside table.js?

这达到了目的。但现在我需要使用一个动态变量,即date. 那么是否可以在exec命令行中传递一个 PHP 或 Javascript 变量,以便我可以在其中使用该变量table.js

Update

更新

I tried modifying my code according to a solution posted here Passing a variable to PhantomJS via exec

我尝试根据此处发布的解决方案修改我的代码通过 exec 将变量传递给 PhantomJS

exec('./phantomjs table.js http://www.yahoo.com',$op,$er);

table.js

表.js

var args = require('system').args;
var page = require('webpage').create();
var address = system.args[1];
    page.open(address, function () {
        page.render('table.png');
        phantom.exit();
    });

But this results in 2 problems:

但这会导致两个问题:

  • The whole process takes about 3-4 minutes to finish
  • After that I get "Server Not Found" message
  • 整个过程大约需要3-4分钟完成
  • 之后,我收到“找不到服务器”消息

If I remove the modified code, everything works as expected.

如果我删除修改后的代码,一切都会按预期进行。

More Debugging

更多调试

Inside table.jsI used this:

table.js 中我使用了这个:

var args = require('system').args;
args.forEach(function(arg, i) {

    console.log(i+'::'+arg);

});

var page = require('webpage').create();
var address = 'http://www.gmail.com';
page.open(address, function () {
    page.render('github.png');
    phantom.exit();
});

On running this, my $op(from execcommand) printout out this:

在运行它时,我的$op(来自exec命令)打印输出:

Array ( [0] => 0::table.js [1] => 1::http://www.yahoo.com )

So far so good. But as soon as I put the below code, the same problems are encountered

到现在为止还挺好。但是我一放下面的代码,就遇到了同样的问题

var args = require('system').args;

var page = require('webpage').create();
var address = system.args[1]; // <--- This line is creating problem, the culprit
page.open(address, function () {
    page.render('github.png');
    phantom.exit();
}); 

Seems like that is not the correct syntax. Anything obvious that I'm unable to see?

似乎这不是正确的语法。有什么明显我看不到的吗?

采纳答案by asprin

Well, I found an alternative to the above problem. Instead of using

好吧,我找到了上述问题的替代方案。而不是使用

var address = system.args[1];

I'm doing it by following the below modification

我正在按照以下修改进行操作

var args = require('system').args;
var address = '';
args.forEach(function(arg, i) {

    if(i == 1)
    {
       address = arg;
    }

});

var page = require('webpage').create();    
page.open(address, function () { // <-- use that address variable from above
    page.render('github.png');
    phantom.exit();
});

回答by darkrat

The problem with your code is a simple oversight.

您的代码的问题是一个简单的疏忽。

You have already stored the args using

您已经使用存储了参数

var args = require('system').args;

So when you need to reference them you only have to do:

因此,当您需要引用它们时,您只需执行以下操作:

var address = args[1];

The use of "system" is looking in a completely different array

“系统”的使用是在一个完全不同的数组中寻找

回答by user3085999

I had to do this and this answers pointed me to find my final answer however as some people expressed here my browser was crashing... I found the problem and solution and thought was worth sharing...

我不得不这样做,这个答案让我找到了我的最终答案,但是正如一些人在这里表达的那样,我的浏览器崩溃了......我找到了问题和解决方案,并认为值得分享......

This will work perfectly fine if:

如果出现以下情况,这将完全正常工作:

exec('phantomjs phdemo.js http://google.com', $o, $e); ?>

var page = require('webpage').create();
var system = require('system');
var address = system.args[1]; 
page.open(address, function () {
    page.render('output.pdf');
    phantom.exit();
 }); 

However if you want to pass more than une parameter in the url address for example google.com?searchteext&date=today I found that the character '&' crashes the browser as it expects it as a different command

但是,如果您想在 url 地址中传递多个 une 参数,例如 google.com?searchteext&date=today 我发现字符 '&' 会使浏览器崩溃,因为它期望它作为不同的命令

My solution was to use the same but instead of putting & I used @ sign so the url will look something like google.com?searchteext@date=today

我的解决方案是使用相同的但不是放置 & 我使用了@ 符号,所以网址看起来像 google.com?searchteext@date=today

then at the other end I added a string replace

然后在另一端我添加了一个字符串替换

var address = address.replace(/@/gi,"&");

Then everything works perfectly fine.... There may be other ways of doing it but this worked perfectly for me

然后一切正常......可能还有其他方法可以做到,但这对我来说非常有效