Skip to content

Instantly share code, notes, and snippets.

@jesusalber1
Created August 26, 2016 11:39
Show Gist options
  • Save jesusalber1/98269acb0e0bc3c577617c342d7e595f to your computer and use it in GitHub Desktop.
Save jesusalber1/98269acb0e0bc3c577617c342d7e595f to your computer and use it in GitHub Desktop.
CasperJS: quick start
/* Run: $ casperjs example.js */
var casper = require('casper').create({
verbose: true, /* Production mode: false */
logLevel: 'debug',
pageSettings: {
loadImages: false,
loadPlugins: false
}
});
var URL = 'http://www.example.com';
var nextURL = 'http://www.iana.org/domains/reserved';
var startTime = new Date;
/* Make it happen */
casper.start(URL, function() {
this.echo(this.getTitle());
});
/* Alt: http://docs.casperjs.org/en/latest/modules/casper.html#waitfor */
casper.thenOpen(nextURL, function() {
this.echo(this.getTitle());
});
casper.run(function() {
this.echo('Elapsed time: ' + ((new Date) - startTime) + ' ms', 'info');
this.exit();
});
/* EXTRA: Don't request youtube videos */
casper.on('resource.requested', function(requestData, request) {
if (/(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|[a-zA-Z0-9_\-]+\?v=)([^#\&\?\n<>\'\"]*)/gi.test(requestData.url)) {
casper.log('Skipped: ' + requestData.url, 'info');
request.abort();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment