Skip to content

Instantly share code, notes, and snippets.

@mistermark
Last active November 21, 2015 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mistermark/b6cdfdded4d69bd376d1 to your computer and use it in GitHub Desktop.
Save mistermark/b6cdfdded4d69bd376d1 to your computer and use it in GitHub Desktop.
Download all MP3 files from a page, without having to click all the links. Works only in Firefox and Chrome. Safari doesn't work, IE is untested.
var config = {
timeout: 0,
batch: 5,
count: 0
};
var init = function(cb) {
if(!config.mp3List) {
config.mp3List = $("a[href*='.mp3']");
config.splicedArray = [];
for (var i = 0; i < config.mp3List.length; i++) {
var obj = config.mp3List[i];
$(obj).attr("download", $(obj).attr("href").substring($(obj).attr("href").lastIndexOf("/") + 1));
}
}
if(cb) cb();
}
var batchDownload = function() {
if(config.splicedArray.length > 0) {
config.timeout = 1200 * config.batch;
}
config.count = config.count++;
setTimeout(function() {
config.splicedArray = config.mp3List.splice(0, config.batch);
for (var i = 0; i < config.splicedArray.length; i++) {
var obj = config.splicedArray[i];
var mp3Link = $(obj).attr("href");
console.log(config.mp3List.length, mp3Link.substring(mp3Link.lastIndexOf("/") + 1));
$(obj)[0].click();
}
if (config.mp3List.length > 0) {
batchDownload();
}
}, config.timeout);
}
init(function(){
batchDownload();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment