Skip to content

Instantly share code, notes, and snippets.

@syshen
Created January 17, 2012 02:42
Show Gist options
  • Select an option

  • Save syshen/1624230 to your computer and use it in GitHub Desktop.

Select an option

Save syshen/1624230 to your computer and use it in GitHub Desktop.
var page = new WebPage();
function googleSearch()
{
console.log('Load http://www.google.com');
page.render('google.png');
console.log('render complete');
page.next = searchResult;
page.evaluate(function() {
document.querySelector('input[name=q]').value = 'syshen';
document.querySelector('form[name=f]').submit();
});
console.log('search submitted');
}
function searchResult()
{
console.log('Load search result');
page.render('google-search.png');
page.next = showSyshen;
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function() {
console.log('js included');
var links = page.evaluate(function() {
var l = $("h3.r a");
return Array.prototype.map.call(l, function(e) {
return e.getAttribute('href');
});
});
for (var i = 0; i < links.length; i++) {
console.log(links[i]);
}
var href = page.evaluate(function() {
return $("h3.r a")[1].href;
});
page.open(href);
});
}
function showSyshen()
{
console.log('Access syshen.cc');
window.setInterval(function() { page.render('syshen.png'); phantom.exit(0);}, 1000);
}
page.viewportSize = {width: 1024, height:768};
page.open('http://www.google.com', function(status) {
if (status === 'success') {
console.log('connect');
if (!page.hasOwnProperty('next')) {
googleSearch();
} else {
page.next();
}
} else {
page.render('google-error.png');
console.log('Error!');
phantom.exit(1);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment