简体   繁体   中英

(NodeJs) Command line arguments to PhantomJs child process along with script args

I have the following code that works:

var Path = require('path')
var Phantomjs = require('phantomjs2')
var phantomjsPath = Phantomjs.path
var childArgs = [
      Path.join(__dirname, 'phantomjs-worker.js'),
      'http://...some login url...',
      3000, //login timeout
      'http://...some address to render as image...',
      5000, //address timeout
      '...output image file path...',
      1000, //page width
      1000, //page height
      1234 //some id
    ]            
var child = Proc.spawn(phantomjsPath, childArgs, { cwd: process.cwd() })

Inside phantom-worker.js:

var argsOffset = 0;
var login = system.args[argsOffset + 1];
var logintimeout = system.args[argsOffset + 2];
var address = system.args[argsOffset + 3];
var addresstimeout = system.args[argsOffset + 4];
var output = system.args[argsOffset + 5];
var pageWidth = parseInt(system.args[argsOffset + 6]);
var pageHeight = parseInt(system.args[argsOffset + 7]);
var pageId = system.args[argsOffset + 8];
...

I need to be able to pass in

--ignore-ssl-errors=true --ssl-protocol=tlsv1

as well.

I tried adding these as the first 2 arguments but it doesn't work. It starts looking for the output image file path for some reason and obviously fails.

Is there any way to pass in these command line arguments along with the script and its child arguments?

The following works as expected. The error was not in this part of the code. Rather, the phantom process was exiting due to memory issues causing the rendered image file to be not created.

var Path = require('path')
var Phantomjs = require('phantomjs2')
var phantomjsPath = Phantomjs.path
var childArgs = [
      '--ignore-ssl-errors=true',
      '--ssl-protocol=any',
      '--web-security=false',
      Path.join(__dirname, 'phantomjs-worker.js'),
      'http://...some login url...',
      3000, //login timeout
      'http://...some address to render as image...',
      5000, //address timeout
      '...output image file path...',
      1000, //page width
      1000, //page height
      1234 //some id
    ]            
var child = Proc.spawn(phantomjsPath, childArgs, { cwd: process.cwd() })

Worker:

var webpage = require('webpage'),
    system = require('system');

var login = system.args[1];
var logintimeout = system.args[2];
var address = system.args[3];
var addresstimeout = system.args[4];
var output = system.args[5];
var pageWidth = parseInt(system.args[6]);
var pageHeight = parseInt(system.args[7]);
var pageId = system.args[8];

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