简体   繁体   中英

Meteor PhantomJS Dynamic URL parameters

I've installed PhantomJS in my Meteor app using the instructions in the answer here: Installing/Using Phantom.js with Meteor but the method involved:

(private/phantomDriver.js)

var page = require('webpage').create();
page.open('http://github.com/', function (){
  console.log('Page Loaded');
  page.render('github.png');
  phantom.exit();
});

has a set URL... how can I pass parameters to the file to change the URL? eg

page.open(URL, etc...)

This:

var URL = newURL
spawn(phantomjs.path, ['assets/app/phantomDriver.js', URL]);

Logs

"stdout: ReferenceError: Can't find variable: URL" to the console.

Artjom B.'s link didn't solve the problem (needed to use spawn(phantomjs.path) whereas exec expects a string I don't know of) - though it did lead me to the answer so, thanks!

Also made use of require('system').args; to access the arguments sent via spawn

Final code:

server.js:

spawn(phantomjs.path, ['assets/app/phantom_driver.js',URL]);

private/phantomDriver.js

var page = require('webpage').create();
var args = require('system').args;
var URL = args[1]

page.open(URL, function(status) {
  console.log('Page loaded. Status: ' + status);
  page.render('github.png');
  phantom.exit();
})

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