0

I am working with PhantomJS and have a file named file1.js which has the following code:

var fs = require('fs');
system = require('system');
var inputfile=system.args[1];
var stream = fs.open(inputfile,'r');
var data = stream.read(); 
var config = JSON.parse(data); 
var user_input=config.input;
var output_format=config.output;
stream.close();

The above code reads a JSON file and takes the user_input and output_format that were stored in the JSON file.

I have another file as well that is named as file2.js and now I want to call file2.js here from inside file1.js and want to pass both user_input and output_format as arguments.

The code of file2.js doesn't need to be added here. The important thing is that file1.js should call file2.js and pass these two arguments to it. file2.js will then perform the required operations on those arguments.

I tried injectJS but it doesn't work since it only works with pages e.g. page.injectJS. Also, injectJS only accepts one parameter which is a filename and nothing else. Thus, I am unable to achieve what I wanna achieve.

Please help me. Thanks in advance.

Azeem
  • 11,148
  • 4
  • 27
  • 40
Awais
  • 1
  • 1
  • What do you mean by _calling a file_ in this context? Do you want to call a specific function from another file? – Azeem May 15 '20 at 14:22
  • No, I want to call a complete file. See when code in file1.js completes execution, it should call file2.js so that it gets executed. but the code of file1.js should also pass parameters to file2.js while calling it. – Awais May 15 '20 at 16:27
  • Right. How would those passed parameters be received? How would they be used? Through environment variables or setting some globals by calling a function? – Azeem May 15 '20 at 16:52
  • It could be anyway. I just want file1.js to pass parameters to file2.js while calling it. It doesn't matter which way it should be – Awais May 15 '20 at 21:22
  • There are multiple SO threads discussing the same thing. You need to look for them. Here's one approach that you use: https://stackoverflow.com/a/36049001/7670262 – Azeem May 16 '20 at 07:16

1 Answers1

0

Just try this flow. may be it will help you

var page = require("webpage").create()
page.open(url, function () {
    page.includeJs(file1.js, function () {
        page.evaluate(function (params1, params2) {
            page.includeJs(file2.js, function () {
                page.evaluate(function (params3, params4) {
                    console.log(params3, param4)//this will print 10,20
                }, params1, params2)
            })
        }, 10, 20)
    })
})
Nilesh Chavan
  • 361
  • 3
  • 10