Last active
February 8, 2016 13:51
-
-
Save iamsortiz/b5706bf39a7b426aedc7 to your computer and use it in GitHub Desktop.
Browser console bulk download from CSS selector and file extensions
This file contains 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
/** | |
* README.md | |
* | |
* # Browser console, bulk delayed download from CSS selector and file extensions | |
* | |
* See below "var config" jsdoc for config details. | |
* | |
* All functionallity encapsulated in "function main()" | |
* | |
* main() is self executable (last line of the code). | |
* | |
* ## TODO | |
* | |
* * Add finished confirmation after bulkDownloadDelayed() | |
* | |
* ## ATTRIBUTION | |
* * Download: http://stackoverflow.com/questions/833015/does-execcommand-saveas-work-in-firefox/833024 | |
* * JQuery Delayed function call: http://stackoverflow.com/questions/10860171/run-function-after-delay | |
*/ | |
/** | |
* Alternative for public files: | |
* wget --recursive --level=1 --no-directories --no-host-directories --accept pdf http://example.com | |
* ATTRIBUTION: http://superuser.com/questions/260087/download-all-pdf-links-in-a-web-page | |
**/ | |
//My old oneliner to copy paste | |
//var selector='.download-document a';var url = $(selector).attr('href'); var a = document.createElement('a'); a.setAttribute('download', 'mydownload.down'); a.href = url; a.style.display = 'none'; document.body.appendChild(a); a.click(); | |
/** | |
* [config description] | |
* @type {Object} | |
* @param {String} targetAnchorsSelector - CSS selector of target elements | |
* @param {String}[] targetExtensions - without '.' | |
* @param {int} downloadDelay - in seconds | |
*/ | |
var config = {}; | |
config.targetAnchorsSelector = '.listItems .block a'; | |
config.targetExtensions = ['vsd', 'pdf', 'mp4']; | |
config.downloadDelay = 10000; | |
function filterAnchors(DOMarray, regexString) { | |
return $(DOMarray).filter(function(index) { | |
var regex = new RegExp(regexString, 'g'); | |
return $(this).attr('href').match(regex); | |
}); | |
}; | |
//from: http://stackoverflow.com/questions/833015/does-execcommand-saveas-work-in-firefox/833024 | |
function download(url) { | |
var a = document.createElement('a'); | |
a.setAttribute('download', 'mydownload.down'); | |
a.href = url; | |
a.style.display = 'none'; | |
document.body.appendChild(a); | |
a.click(); | |
}; | |
//from: http://stackoverflow.com/questions/10860171/run-function-after-delay | |
function bulkDownloadDelayed(urlArr, delay) { | |
delay = delay || 1000; | |
$.each(urlArr, function(i, url) { | |
$(document).delay(delay).queue(function() { | |
console.log('Downloading url: %O', url); | |
download(url); | |
$(this).dequeue(); | |
}); | |
}); | |
}; | |
function main(config) { | |
var anchors = $(config.targetAnchorsSelector); | |
//console.log('anchors: %O', anchors); | |
var filteredAnchors = []; | |
$.each(config.targetExtensions, function(i, extension) { | |
var extensionRegex = '.' + extension + '$'; | |
var auxFiltered = filterAnchors(anchors, extensionRegex); | |
//console.log('auxFiltered for regex %O: %O', extensionRegex, auxFiltered); | |
filteredAnchors = filteredAnchors.concat(auxFiltered.toArray()); | |
}); | |
//console.log('filteredAnchors: %O', filteredAnchors); | |
var urls = jQuery.map(filteredAnchors, function(val, i) { | |
return $(val).attr('href'); | |
}); | |
//console.log('urls: %O', urls); | |
var urlsUnique = jQuery.unique(urls); | |
console.log('urlsUnique: %O', urlsUnique); | |
console.log('Last download will start in ' + ((downloadDelay / 1000) * urlsUnique.length)); | |
bulkDownloadDelayed(urlsUnique, config.downloadDelay); | |
}; | |
main(config); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment