Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

phantomJS - Pass Argument to the JS File

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

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

table.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 exec command line so that I can use that variable inside table.js?

Update

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

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

table.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

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

More Debugging

Inside table.js I used this:

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 exec command) printout out this:

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?

like image 984
asprin Avatar asked May 29 '13 13:05

asprin


2 Answers

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

like image 111
darkrat Avatar answered Nov 10 '22 15:11

darkrat


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

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

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

like image 4
user3085999 Avatar answered Nov 10 '22 14:11

user3085999