Skip to content

Instantly share code, notes, and snippets.

@brettz9
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save brettz9/528264bb312c6e673f13 to your computer and use it in GitHub Desktop.

Select an option

Save brettz9/528264bb312c6e673f13 to your computer and use it in GitHub Desktop.
Tool for building numerically sequenced links that can be bulk-downloaded. Requires Flash-Got or equivalent browser add-on for non-same-domain links.
<!DOCTYPE html>
<html><head>
<meta charset="utf-8" />
<title>Bulk Download Helper</title>
</head>
<body>
<b>Steps</b>
<ol>
<li>If you are downloading something from a separate domain from this server, you must install <a href="https://addons.mozilla.org/en-Us/firefox/addon/flashgot/">Flashgot</a> in Firefox (or an equivalent add-on for other browsers).</li>
<li>Add a URL pattern in the text box, with <code>(s)</code> used as a variable in which to substitute a number (or <code>(s&lt;number&gt;)</code> where &lt;number&gt; is an integer indicating the total zero padding count).</li>
<li>Click "Clear and build links" (or click "Build links" to append links)</li>
<li>If you want the links to work as links rather than downloads, unclick the "Download mode" checkbox.</li>
<li>Right-click and select "Flashgot All" (or the equivalent for your browser/add-on) or if you did not need to install the add-on, you can just click "Download all".</li>
</ol>
<label>Min: <input type="number" id="min" value="1" /></label>
<label>Max: <input type="number" id="max" value="100" /></label>
<label>Download mode: <input type="checkbox" checked="checked" id="download_mode" /></label>
<br /><br />
<textarea id="url" style="width: 100%" placeholder="e.g., https://example.com/book1/chapter1/page(s2).pdf"></textarea>
<br /><br />
<button id="build">Build links</button>
<button id="clear">Clear</button>
<button id="clear_and_build">Clear and build links</button><br />
<button id="download">Download all</button><br />
<button id="bookmark">Bookmark settings</button>
<div id="output"></div><br /><br />
<script>
/*global URLSearchParams, URL */
/*jslint vars:true */
(function () {'use strict';
function $ (s) {
return document.querySelector(s);
}
var usp = new URLSearchParams(new URL(window.location.href).searchParams);
['url', 'min', 'max', 'download_mode'].forEach(function (cfg) {
var defaultCfg = usp.get(cfg);
if (defaultCfg) {
var el = $('#' + cfg);
if (el.type === 'checkbox') {
el.checked = defaultCfg === '1';
}
else {
el.value = defaultCfg;
}
}
});
$('#build').addEventListener('click', function () {
var min = parseInt($('#min').value, 10);
var max = parseInt($('#max').value, 10);
var i, a;
function replacer (n0, n1) {
var len = parseInt(n1 || 0, 10) + (n1 ? 1 : 0) + - i.toString().length + 1;
len = len < 0 ? 0 : len;
return new Array(len).join('0') + i;
}
for (i = min; i <= max; i++) {
a = document.createElement('a');
a.target = '_blank';
a.href = $('#url').value.replace(/\(s(\d*)\)/g, replacer);
a.textContent = 'url' + i;
if ($('#download_mode').checked) {
a.download = 'download';
}
$('#output').appendChild(a);
$('#output').appendChild(document.createElement('br'));
}
});
$('#clear').addEventListener('click', function () {
while ($('#output').firstChild) {
$('#output').removeChild($('#output').firstChild);
}
});
$('#clear_and_build').addEventListener('click', function () {
$('#clear').click();
$('#build').click();
});
$('#download').addEventListener('click', function () {
Array.from($('#output').querySelectorAll('a')).forEach(function (a) {
var oldDownload = a.download;
a.download = 'download';
a.target = '_blank';
a.click();
if (oldDownload) {
a.download = oldDownload;
}
else {
a.removeAttribute('download');
}
});
});
$('#bookmark').addEventListener('click', function () {
var newUSP = new URLSearchParams();
['url', 'min', 'max', 'download_mode'].forEach(function (cfg) {
var el = $('#' + cfg);
if (el.type === 'checkbox') {
newUSP.set(cfg, $('#' + cfg).checked ? '1' : '0');
}
else {
newUSP.set(cfg, $('#' + cfg).value);
}
});
window.location = window.location.href.replace(/\?.*$/, '?') + newUSP.toString();
});
$('#download_mode').addEventListener('click', function (e) {
Array.from($('#output').querySelectorAll('a')).forEach(function (a) {
if (e.target.checked) {
a.download = 'download';
}
else {
a.removeAttribute('download');
}
a.target = '_blank';
});
});
}());
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment