Download Many Files
I like buying programming ebooks. I moved away from paper books when I realised they took up quite a bit of space. The only problem is being left with a page of download links to work through. A single ebook usually comes in at least three different file formats. The last collection I bought had 45 download links (15 books, 3 formats per book). It put me off buying anything I need to download because I didn’t want to spend an evening clicking download links. People selling downloadable content should really make the download process easier. A single link to download everything would be perfect.
If it were free content I’d just copy the links into a text file and point wget at it. That doesn’t work as well with paid content. There is usually an authentication process in the way. I know wget supports loading cookies but it gets fiddly.
The other day I was looking at a big download list I’d been putting off for some time. I decided it would be more fun to write some JavaScript than go through the links clicking each one.
I automated the process of opening a new tab for each link. Running the following JavaScript in the developer console will fire off a download every 30 seconds. You’ll need to allow the page to open multiple tabs once this is running. That can be done using an icon in the Chrome address bar. The tabs will disappear as downloads begin.
// Edit these to control the download
// ---------------------------------------
// 30 second delay between each download
var timeBetweenDownloads = 30 * 1000;
// How to find the links
var selector = '.js-start-download a';
var books = [...document.querySelectorAll(selector)].map(x => x.href);
// ---------------------------------------
// Downloads books by opening each link in new window/tab and
// waiting 30 seconds between each download
function download(book_urls, milliseconds){
if (book_urls.length === 0) {
return;
}
const [head, ...tail] = book_urls;
window.open(head);
window.setTimeout(() => download(tail, milliseconds), milliseconds);
}
// Download everything we have found
download(books, timeBetweenDownloads);
Instructions for use:
- Navigate your browser to the download page containing links
- Press F12 to open the developer tools
- You will need to edit
selectorto find the right elements on the page- You can right click the download link and choose “Inspect” to reveal the HTML element that corresponds to the download link
- In this case the download links were
aelements nested inside adivwith the classjs-start-download - The jQuery docs are a good reference for selectors
- Click on the Console tab in developer tools
- Copy and paste the source code into the Console
- Once downloads begin, you will notice a new icon in the Chrome address bar. Click the icon and allow the page to open multiple windows