このGoogleフィードAPIの例を、Phantomjsで動作するように翻訳しようとしています。Phantomjsの例に従って、次のようになります。
var page = require('webpage').create();
page.onConsoleMessage = function(msg) {
console.log(msg);
};
// Our callback function, for when a feed is loaded.
function feedLoaded(result) {
if (!result.error) {
// Loop through the feeds, putting the titles onto the page.
// Check out the result object for a list of properties returned in each entry.
// http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON
for (var i = 0; i < result.feed.entries.length; i++) {
var entry = result.feed.entries[i];
console.log(entry.title);
}
}
}
page.includeJs("http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0", function() {
google.load("feeds", "1");
var feed = new google.feeds.Feed("http://www.digg.com/rss/index.xml");
feed.includeHistoricalEntries(); // tell the API we want to have old entries too
feed.setNumEntries(250); // we want a maximum of 250 entries, if they exist
// Calling load sends the request off. It requires a callback function.
feed.load(feedLoaded);
phantom.exit();
});
出力は次のようになります。
ReferenceError: Can't find variable: google
私はvargoogleを定義しようとしました。インクルードの直後ですが、運がありません。私はPhantomjsとjs全般に不慣れです。どんなポインタでも大歓迎です。