Skip to content

Instantly share code, notes, and snippets.

@namklabs
Last active July 28, 2016 21:27
Show Gist options
  • Save namklabs/e18174f763bb0db2be3c2e0cab9b6891 to your computer and use it in GitHub Desktop.
Save namklabs/e18174f763bb0db2be3c2e0cab9b6891 to your computer and use it in GitHub Desktop.
This function will download all of the files on a page. Use in dev console. Just tweak the selector to filter what types of links will be downloaded. Eliminates duplicate downloads. Requires jQuery. http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet
// requires jQuery
// tested only in Chrome console dev tools
// use jQuerify bookmarklet if your page doesn't have jQuery: http://www.learningjquery.com/2009/04/better-stronger-safer-jquerify-bookmarklet
function batchDownload( $jquery_collection_of_a_elements, buffer_ms, random_ms, timeonly_bool ){
if( $jquery_collection_of_a_elements.length < 1 ){
throw "Nothing to download! Empty jQuery collection.";
}
//optional time to wait between each download with an optional random time buffer to seem less automated. Use to avoid being banned for spamming a server.
this.delay = {
buffer: parseInt( buffer_ms ) || 500,
};
this.delay.random = parseInt( random_ms ) || this.delay.buffer;
this.selector = $jquery_collection_of_a_elements.selector;//save our selector in case you are curious later.
this.$files = $jquery_collection_of_a_elements;//$('.main-content li a[href$=".pdf"]');
this.downloaded = {};//index of unique files
this.unique = 0;//counts unique downloads
this.complete = false;//not done yet
this.lastfile = false;//whether the last file downloaded was successful or skipped.
this.calculateDelay = function(){
var itemdelay = this.delay.buffer + ( Math.ceil( Math.random() * this.delay.random ) );
return itemdelay;
}
this.getFile = function(){
var $currentFile = this.$files.eq(0);//get the first element in the collection
$currentFile.attr('download','');//give the link a download attribute. This just makes thinks a lot less complicated than the iframe method of downloading stuff via javascript.
var href = $currentFile.attr('href');//snag the link to the file
if( typeof href === "undefined"){
// jQuery element is not a link to a file because it has no href. Ignore.
console.log( $currentFile[0] + ' is not a download link. Skipping...' );
this.lastfile = false;
} else {
console.log('Requesting file ' + href );
if( this.downloaded[ href ] ){
// file has already been downloaded!
console.log( 'That file has already been downloaded. Skipping...' );
this.lastfile = false;
} else {
// the file has not been downloaded. download it.
console.log( 'Download initiated for ' + href );
this.unique++;//increment the number of unique downloads.
this.downloaded[ href ] = true;
$currentFile[0].click();//click the dom element to start the download.
this.lastfile = true;
}
}
this.$files = this.$files.not( $currentFile );//remove the link from our list so we don't try to download it again.
if( this.$files.length > 0 ){//if we run out of links, stop.
var that = this;
var msdelay = this.lastfile ? this.calculateDelay() : 0;//if the last file was skipped, download this file without delay.
this.currentTimeout = setTimeout( function(){
that.getFile();//there are links left, so call this function again.
}, msdelay );
} else {
console.log( 'Downloading complete. ' + this.unique + ' files downloaded.' );
this.complete = true;
}
}
this.go = this.start = this.begin = function(){
this.getFile();//start
};
this.pause = this.stop = function(){
if( typeof this.currentTimeout !== "undefined" ){
clearTimeout( this.currentTimeout );
}
};
this.delay.average = this.delay.buffer + this.delay.random / 2;
this.delay.estimated = this.delay.average * this.$files.length;
this.delay.minutes = Math.floor( this.delay.estimated / 1000 / 60 );
this.delay.seconds = Math.floor( ( this.delay.estimated / 1000 / 60 - this.delay.minutes ) * 60 );
console.log( this.$files.length + ' downloads queued (may include duplicates).');
console.log( 'Estimated time to complete: ' + this.delay.minutes + ' minutes and ' + this.delay.seconds + ' seconds.' );
if( !timeonly_bool ){
// if we don't just want a time estimate, start the actual batch download.
this.getFile();//start the first download.
}
return this;
}// end batchDownload().
// example usage:
var downloadAllPDFs = new batchDownload( $('a[href$=".pdf"]'), 250, 1000 );
// that's it!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment