Derpibooru - Download All

Adds a download button that lets you download all images in a gallery.

As of 2014-09-29. See the latest version.

// ==UserScript==
// @name        Derpibooru - Download All
// @namespace   Selbi
// @include     http*://*derpiboo.ru/*
// @include     http*://*derpibooru.org/*
// @version     1.1
// @description Adds a download button that lets you download all images in a gallery.
// ==/UserScript==

const INTERVAL_TIME = 1000;
const URL_NAME = location.protocol + "//" + location.hostname + location.pathname;

$(".metabar:eq(0)").append('<a href="javascript:void()" id="GetAllLinks">Get All Links</a> <a href="javascript:void()" id="StartDownload" style="display:none">Start Download!</a>');

var pagecount = (/\d+/).exec($(".last a").attr("href"));
if (pagecount == null) pagecount = 1;
var linksarr = [];
var i = 0;

$("#GetAllLinks").click(function(){
	$(this).attr('style', 'pointer-events:none; cursor:default;').html("Grabbings Links... (0)");
	for (var int=1; int<=pagecount; int++){
		$.ajax({
			async: false,
			type: 'GET',
			url: URL_NAME + "?page=" + int,
			success: function(data){
				$(".image_container", data).each(function(){
					linksarr.push($(this).attr("data-download-uri"));					
					$("#GetAllLinks").html("Grabbings Links... (" + linksarr.length + ")");
				});
			}
		});
	}
	$("#GetAllLinks").hide();
	$("#StartDownload").html("Start Download! (0/" + linksarr.length + ") [Warning: Opens a new tab for each image!]").show();
});

function downloadImage(){
	window.open(linksarr[i], '_blank');
	// GM_openInTab(linksarr[i]);

	$("#StartDownload").html("Downloading... (" + (i+1) + "/" + linksarr.length + ")");
	i++;
	if (i < (linksarr.length)) {
		setTimeout(function(){downloadImage()}, INTERVAL_TIME);
	} else {
		$("#StartDownload").attr('style', 'pointer-events:none; cursor:default;').html("Finished Download! (" + i + "/" + linksarr.length + ")");
	}
}

$("#StartDownload").click(function(){
	$(this).html("Downloading... (0/" + linksarr.length + ")");
	setTimeout(function(){downloadImage()}, INTERVAL_TIME);
	var timer = setInterval();
});