Last active
August 29, 2015 13:56
-
-
Save oranheim/8920439 to your computer and use it in GitHub Desktop.
Google Search example (works with node-phantom, but not with node-phantom-simple)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var doSearch, displayResults; | |
//var phantom = require('node-phantom'); | |
var phantom = require('node-phantom-simple'); | |
var assert = require('assert'); | |
doSearch = function (ph, page) { | |
console.log("doSearch"); | |
page.evaluate(function () { | |
$('input[name=q]').val('what is phantomjs'); | |
$('form').trigger('submit'); | |
return true; | |
}, function (err, result) { | |
assert.ifError(err); | |
assert.ok(result); // fails because page.evaluate() is never called | |
}); | |
}; | |
displayResults = function (ph, page) { | |
console.log("displayResults"); | |
page.evaluate(function () { | |
var hArr = []; | |
$('h3 a').each(function (i) { | |
hArr.push([i + 1, $(this).text(), ' // ' + $(this).attr('href')].join(': ')); | |
}); | |
return hArr; | |
}, function (err, result) { | |
assert.ifError(err); | |
assert.ok(result); | |
console.log(result); | |
ph.exit(); | |
}); | |
}; | |
phantom.create(function (err, ph) { | |
return ph.createPage(function (err, page) { | |
page.onLoadFinished = function (status) { | |
console.log("Status: %s", status); | |
if (status === 'success') { | |
page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', function () { | |
if (!ph.state) { | |
doSearch(ph, page); | |
ph.state = 'results'; | |
} else { | |
displayResults(ph, page); | |
} | |
}); | |
} else { | |
console.log('Connection failed.'); | |
ph.exit(); | |
} | |
}; | |
page.open("http://google.com", function (err, status) {}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment