Created
August 26, 2016 11:39
-
-
Save jesusalber1/98269acb0e0bc3c577617c342d7e595f to your computer and use it in GitHub Desktop.
CasperJS: quick start
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
/* 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