Skip to content

Instantly share code, notes, and snippets.

@khilnani
Created January 26, 2014 06:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save khilnani/8629308 to your computer and use it in GitHub Desktop.
Save khilnani/8629308 to your computer and use it in GitHub Desktop.
Phantom.js script to take screenshots of a google search result
#!/usr/bin/env phantomjs
//--------------------------------------------------------
var page = require('webpage').create(),
system = require('system'),
action = null,
q = null;
//--------------------------------------------------------
if (system.args.length === 1) {
console.log('Usage: google.js <some Query>');
phantom.exit(1);
} else {
q = system.args[1];
}
//--------------------------------------------------------
start = function () {
console.log('ACTION: start');
page.evaluate( function(q) {
$('input[name="q"]').val( q );
$('form[action="/search"]').submit();
}, q);
page.render('start.png');
action = viewList;
}
viewList = function () {
console.log('ACTION: viewList');
page.render('viewList.png');
phantom.exit();
}
//--------------------------------------------------------
work = function () {
if(action == null) action = start;
//console.log( "URL: " + page.url );
action.call();
}
injectJQuery = function (callback) {
// console.log('injecting JQuery');
page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", callback);
}
page.onLoadFinished = function(status) {
// console.log('Status: ' + status);
if(status == 'success') {
injectJQuery( work );
} else {
console.log('Connection failed.');
phantom.exit();
}
}
page.onConsoleMessage = function(msg){
console.log('PAGE: ' + msg);
};
page.onResourceReceived = function (response) {
if(response.stage == "end")
console.log('Response (#' + response.id + ', status ' + response.status + '"): ' + response.url);
}
page.onUrlChanged = function (url) {
console.log("URL: " + url);
}
//--------------------------------------------------------
page.open('http://www.google.com');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment