Posted by SF on Thu 28th Jun 15:59
download | new post | report as spam
- //Load Modules
- phantom.casperPath = 'C:\\Development\\PhantomJS\\casper';
- phantom.injectJs(phantom.casperPath + '\\bin\\bootstrap.js');
- var system = require('system');
- var casper = require('casper').create({
- loadImages: true,
- loadPlugins: true,
- verbose: true,
- clientScripts: ['C:\\Development\\PhantomJS\\jquery.js'],
- viewportSize: {
- width: 1366,
- height: 768,
- },
- pageSettings: {
- javascriptEnabled: true,
- localToRemoteUrlAccessEnabled: true,
- userAgent: 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.56 Safari/536.5',
- XSSAuditingEnabled: false
- }
- });
- //Check Params
- if (system.args.length < 4 || system.args.length > 4) {
- console.log('Usage: downloadimages.js Start_URL Image_Selector NextButton_Selector');
- phantom.exit(1);
- }
- //Setup
- var i = 0;
- //Function to zero pad the image filenames
- function pad(number, length) {
- var str = '' + number;
- while (str.length < length) { str = '0' + str; }
- return str;
- }
- //Load each page
- casper.on('load.finished', function(status) {
- this.echo(this.getTitle() + ' (' + this.getCurrentUrl() + ') loaded');
- //Attempt to download based on selector (img src)
- var srcurl = this.evaluate(function(selector) {
- return $(selector).attr('src');
- },{selector: system.args[2]});
- if (srcurl == null) {
- this.echo(' No img src found');
- //Attempt to download based on selector (background-image)
- var bgurl = this.evaluate(function(selector) {
- return $(selector).css('background-image').replace(/^url|[\(\)]/g, '');
- },{selector: system.args[2]});
- if (bgurl == null || bgurl == 'none') {
- this.echo(' No background-image found');
- }
- else {
- var filename = pad(i,3) + bgurl.substr(bgurl.length - 4);
- this.echo(' Found img src URL: ' + bgurl + ' , downloading as: ' + filename);
- this.download(bgurl, filename);
- i = i + 1;
- }
- }
- else {
- var filename = pad(i,3) + srcurl.substr(srcurl.length - 4);
- this.echo(' Found img src URL: ' + srcurl + ' , downloading as: ' + filename);
- this.download(srcurl, filename);
- i = i + 1;
- }
- //Attempt to find next button based on selector
- if (this.exists(system.args[3])) {
- this.echo(' Clicking next button (30 second timeout)');
- this.click(system.args[3]);
- //this.wait(30000);
- }
- else {
- this.echo(' No next button found');
- this.exit();
- };
- });
- //Load initial page
- console.log('Loading - ' + system.args[1]);
- casper.start(system.args[1]);
- //GO GO GO
- casper.run();
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily
.After submitting an amendment, you'll be able to view the differences between the old and new posts easily
