javascript 通过 exec 将变量传递给 PhantomJS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16752882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a variable to PhantomJS via exec
提问by iamdarrenhall
I'm getting started with Grunt and wanting to pass a variable to a PhantomJS script I'm running via exec. What I want to be able to do is pass a url in for the script to take the screen capture from. Any help would be greatly appreciated, thanks!
我开始使用 Grunt 并希望将变量传递给我通过 exec 运行的 PhantomJS 脚本。我希望能够做的是为脚本传递一个 url 以从中获取屏幕截图。任何帮助将不胜感激,谢谢!
Darren
达伦
Grunt script
咕噜脚本
exec('phantomjs screenshot.js',
function (error, stdout, stderr) {
// Handle output
}
);
screenshot.js
截图.js
var page = require('webpage').create();
page.open('http://google.com', function () {
page.render('google.png');
phantom.exit();
});
回答by Cybermaxs
Command-line arguments are accessible via module require('system').args
(Module System
). The first one is always the script name, which is then followed by the subsequent arguments
命令行参数可通过模块require('system').args
(Module System
)访问。第一个总是脚本名称,然后是后续参数
This script will enumerate all arguments and write out to console.
此脚本将枚举所有参数并写出到控制台。
var args = require('system').args;
if (args.length === 1) {
console.log('Try to pass some arguments when invoking this script!');
}
else {
args.forEach(function(arg, i) {
console.log(i + ': ' + arg);
});
}
In your case, the solution is
在您的情况下,解决方案是
Grunt
咕噜声
exec('phantomjs screenshot.js http://www.google.fr',
function (error, stdout, stderr) {
// Handle output
}
);
screenshot.js
截图.js
var page = require('webpage').create();
var address = system.args[1];
page.open(address , function () {
page.render('google.png');
phantom.exit();
});
回答by DynamicDan
Here is an easy way to pass and pick args that are applicable. Very flexible and easy to maintain.
这是传递和选择适用的参数的简单方法。非常灵活且易于维护。
Use like:
像这样使用:
phantomjs tests/script.js --test-id=457 --log-dir=somedir/
OR
或者
phantomjs tests/script.js --log-dir=somedir/ --test-id=457
OR
或者
phantomjs tests/script.js --test-id=457 --log-dir=somedir/
OR
或者
phantomjs tests/script.js --test-id=457
Script:
脚本:
var system = require('system');
// process args
var args = system.args;
// these args will be processed
var argsApplicable = ['--test-id', '--log-dir'];
// populated with the valid args provided in availableArgs but like argsValid.test_id
var argsValid = {};
if (args.length === 1) {
console.log('Try to pass some arguments when invoking this script!');
} else {
args.forEach(function(arg, i) {
// skip first arg which is script name
if(i != 0) {
var bits = arg.split('=');
//console.log(i + ': ' + arg);
if(bits.length !=2) {
console.log('Arguement has wrong format: '+arg);
}
if(argsApplicable.indexOf(bits[0]) != -1) {
var argVar = bits[0].replace(/\-/g, '_');
argVar = argVar.replace(/__/, '');
argsValid[argVar] = bits[1];
}
}
});
}
// enable below to test args
//require('utils').dump(argsValid);
//phantom.exit();