Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to run PhantomJS from node.js as a command line argument

I was recently going to test out running phantomJS from python as a commandline argument, I haven't got round to it yet but have seen examples. Because PhantomJS is run from the command line this seems to be possible. The result that PhantomJS would spit out would go straight into a variable.

Before I go down that path, making this work in node.js would actually be more useful for me and it got me thinking, can i just use to node to run PhantomJS as a program gets run from the commandline and store the data result that PhantomJS would normally spit out into a variable?

I would rather not use phantomjs-node because it seems to be using too many tricks.

The reason for all of this is to be able to run PhantomJS at the same time as another action the program takes and use the resulting data its recorded for some other stuff.

Simply put, you can run system command line stuff in python, can I do the same in node.js?

Cheers :)

Edit: I understand that node and phantom use different js environments, that's cool because I just want to run phantom as its own process and catch all that output data into a node.js variable (the data will be a array of a pair, string and floating point.) I don't want to 'drive' with phantom, I will craft the loaded javascript files todo what I want. All I want is phantom output. :)

like image 769
Joseph Avatar asked Jun 29 '12 10:06

Joseph


People also ask

How do I run PhantomJS script?

Go to the “bin” folder and check phantomjs.exe file. If you are using it on a Windows OS, then you can set the path variable under the environment variable for fast access through command prompt. The command to run the PhantomJS program: C:\> phantomjs [options] file.

How do I pass command line arguments to a node js program?

In Node. js, all command-line arguments received by the shell are given to the process in an array called argv(arguments-values). Method 1: Using process. argv: It is the simplest way to receive arguments with the help of process.


1 Answers

From NPM: https://npmjs.org/package/phantomjs

var path = require('path')
var childProcess = require('child_process')
var phantomjs = require('phantomjs')
var binPath = phantomjs.path

var childArgs = [
  path.join(__dirname, 'phantomjs-script.js'),
  'some other argument (passed to phantomjs script)'
]

childProcess.execFile(binPath, childArgs, function(err, stdout, stderr) {
  // handle results
})
like image 97
Donald Derek Avatar answered Sep 23 '22 09:09

Donald Derek