1

I am trying to pass an argument from the command line and check or uncheck a check box on my local website. If I substitute the system.args[4] in the returnVars function with a true or false it works, but the argument I pass from the command line doesn't affect the check box.

var returnVars = function(){
    if (system.args.length > 4) {
        console.log(system.args[4]); // this is getting the correct value from the args
        return system.args[4];
    }
};

page.open(address, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit(1);
    } else {
        var  returnable = page.evaluate(function(r,s) {
            return document.getElementById(r).checked = s;
        }, 'returnable', returnVars());

        window.setTimeout(function () {
            page.render(output);
            phantom.exit();
        }, 200);
    }
});

I am using the rasterize.js example and replacing the zoom option with my own. I call it with:

phantomjs rasterize.js mywebsite.com c:\foo.pdf "letter" false
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
James Morris
  • 353
  • 5
  • 20
  • I am using the rasterize.js example and replacing the zoom option with my own. phantomjs rasterize.js http://mywebsite.com c:\foo.pdf "letter" false – James Morris Nov 17 '14 at 20:56

1 Answers1

0

You're passing a string "false" into evaluate and setting the checked property to this string. The string is then evaluated to true, because !!"false" === true and !!"true" === true. If hope you see the problem now.

This is easy to fix. Since you're passing JavaScript code (false) as a string, you need to execute the string so that it is JavaScript code again:

document.getElementById(r).checked = eval(s);

eval is usually evil, but since this is your local script, there is nothing you can break security-wise.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222