发送URL到Phantomjs?

时间:2013-05-30 19:32:19

标签: javascript phantomjs

screenshot.js

var page = require("webpage").create();
var homePage = "http://www.google.com/";
page.open(homePage);
page.onLoadFinished = function(status) {
  var url = page.url;
  console.log("Status:  " + status);
  console.log("Loaded:  " + url);
  page.render("google.png");
  phantom.exit();
};

终端

bin/phantomjs screenshot.js

问题: 有什么方法可以在screenshot.js之外以某种方式发送phantomjs URL(上面的var homePage的值),以便它不在脚本内部硬编码?

1 个答案:

答案 0 :(得分:5)

将网址添加到命令行

bin/phantomjs screenshot.js http://www.google.com/

这里有一个来自文档的示例:https://github.com/ariya/phantomjs/blob/master/examples/arguments.js

var system = require('system');
if (system.args.length === 1) {
    console.log('Try to pass some args when invoking this script!');
} else {
    system.args.forEach(function (arg, i) {
            console.log(i + ': ' + arg);
    });
}
phantom.exit();