少し背景...私はjavascriptとphantom.jsに少し慣れていないので、これがjavascriptまたはphantom.jsのバグ(機能?)かどうかわかりません。
以下は正常に完了します(phantom.exit()が欠落していることをお詫びします。完了したら、ctrl + cを実行する必要があります)。
var page = require('webpage').create();
var comment = "Hello World";
page.viewportSize = { width: 800, height: 600 };
page.open("http://www.google.com", function (status) {
if (status !== 'success') {
console.log('Unable to load the address!');
phantom.exit();
} else {
page.includeJs('http://code.jquery.com/jquery-latest.min.js', function() {
console.log("1: ", comment);
}, comment);
var foo = page.evaluate(function() {
return arguments[0];
}, comment);
console.log("2: ", foo);
}
});
これは機能します:
page.includeJs('http://code.jquery.com/jquery-latest.min.js', function() {
console.log("1: ", comment);
}, comment);
出力:1: Hello World
だがしかし:
page.includeJs('http://code.jquery.com/jquery-latest.min.js', function(c) {
console.log("1: ", c);
}, comment);
出力:1: http://code.jquery.com/jquery-latest.min.js
そしてそうではありません:
page.includeJs('http://code.jquery.com/jquery-latest.min.js', function() {
console.log("1: ", arguments[0]);
}, comment);
出力:1: http://code.jquery.com/jquery-latest.min.js
2番目のピースを見ると、これは機能します。
var foo = page.evaluate(function() {
return arguments[0];
}, comment);
console.log("2: ", foo);
出力:2: Hello World
この:
var foo = page.evaluate(function(c) {
return c;
}, comment);
console.log("2: ", foo);
出力:2: Hello World
しかし、これではありません:
var foo = page.evaluate(function() {
return comment;
}, comment);
console.log("2: ", foo);
出力:
ReferenceError:変数が見つかりません:コメント
phantomjs://webpage.evaluate():2
phantomjs://webpage.evaluate():3
phantomjs://webpage.evaluate():3
2:null
良いニュースは、何が機能し、何が機能しないかを知っていますが、少し一貫性はどうですか?
includeJs
なぜとの違いはevaluate
?
匿名関数に引数を渡す適切な方法はどれですか?