我正在使用Phantomjs 2.1.1:
require('phantomjs-polyfill')
var server = require('webserver').create();
var fs = require('fs');
var port = 6000;
var service = server.listen(port, function (request, response) {
var width = 1920;
var height = 1080;
page = require('webpage').create();
system = require('system');
page.settings.clearMemoryCaches = true;
page.clearMemoryCache();
//Screen resolution
page.viewportSize = {width: width, height: height};
//User agent
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.settings.javascriptEnabled = true;
page.settings.webSecurityEnabled = false;
page.settings.localToRemoteUrlAccessEnabled = true;
page.onConsoleMessage = function(msg) {
system.stderr.writeLine('Console: ' + msg);
};
page.onClosing = function(closingPage) {
CurrentStep = 1;
ScreenShot("closing.png");
console.log('URL: ' + page.url);
console.log('The page is closing! URL: ' + closingPage.url);
};
page.open("http://test.com", function(status) {
page.evaluate(function(w, h) {
document.body.style.width = w + "px";
document.body.style.height = h + "px";
}, width, height);
if (status === "success") {
console.log("Page loaded");
}
});
page.evaluate(function() {
var number = 123456.789;
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
// expected output: "123.456,79 €"
// the Japanese yen doesn't use a minor unit
console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
// expected output: "¥123,457"
// limit to three significant digits
console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number));
});
}
});
console.log("Server started!");
但是,当我使用以下命令运行脚本时:
phantomjs --web-security=false ./test.js
我收到错误:
Console: ReferenceError: Can't find variable: Intl
我已经看到PhantomJs 2.1.1不支持polyfill,这就是为什么我安装了phantomjs-polyfill的原因,但是也许我没有将其正确添加到测试脚本中。
如何使这部分执行没有问题:
var number = 123456.789;
console.log(new Intl.NumberFormat('de-DE', { style: 'currency', currency: 'EUR' }).format(number));
// expected output: "123.456,79 €"
// the Japanese yen doesn't use a minor unit
console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number));
// expected output: "¥123,457"
// limit to three significant digits
console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number));
以前有人遇到过这个问题吗?有可能解决吗?