我正在开始使用 Grunt,并希望将变量传递给我通过 exec 运行的 PhantomJS 脚本。我想要做的是传递一个 url 供脚本从中获取屏幕截图。任何帮助将不胜感激,谢谢!
Darren
Grunt 脚本
exec('phantomjs screenshot.js',
function (error, stdout, stderr) {
// Handle output
}
);
截图.js
var page = require('webpage').create();
page.open('http://google.com', function () {
page.render('google.png');
phantom.exit();
});
命令行参数可通过模块访问require('system').args
(模块System
)。第一个始终是脚本名称,然后是后续参数
该脚本将枚举所有参数并写入控制台。
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);
});
}
对于你的情况,解决方案是
Grunt
exec('phantomjs screenshot.js http://www.google.fr',
function (error, stdout, stderr) {
// Handle output
}
);
截图.js
var page = require('webpage').create();
var address = system.args[1];
page.open(address , function () {
page.render('google.png');
phantom.exit();
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)