E-Hentai Automated Downloads

Automates downloads through the Doggie Bag Archiver

As of 2017-04-09. See the latest version.

// ==UserScript==
// @name           E-Hentai Automated Downloads
// @description    Automates downloads through the Doggie Bag Archiver
// @include        http://e-hentai.org/*
// @include        https://e-hentai.org/*
// @include        http://g.e-hentai.org/*
// @include        https://g.e-hentai.org/*
// @include        http://exhentai.org/*
// @include        https://exhentai.org/*
// @grant          GM_xmlhttpRequest
// @run-at         document-start
// @author         etc
// @version        2.1.0
// @namespace      https://greasyfork.org/users/2168
// ==/UserScript==

if (typeof(Promise) === 'undefined') {
	console.warn('Browser does not support promises, aborting.');
	return;
}

/*-----------------------
  Assets (icons and GIFs)
  -----------------------*/

var ASSETS = {

	downloadIcon: generateSvgIcon(1792, 'rgb(0,0,0)',
		'M1344 1344q0-26-19-45t-45-19-45 19-19 45 19 45 45 19 45-19 19-45zm256 0q0-26-19-45t-45-19-45 19-19 ' +
		'45 19 45 45 19 45-19 19-45zm128-224v320q0 40-28 68t-68 28h-1472q-40 0-68-28t-28-68v-320q0-40 28-68t' +
		'68-28h465l135 136q58 56 136 56t136-56l136-136h464q40 0 68 28t28 68zm-325-569q17 41-14 70l-448 448q-' +
		'18 19-45 19t-45-19l-448-448q-31-29-14-70 17-39 59-39h256v-448q0-26 19-45t45-19h256q26 0 45 19t19 45' +
		'v448h256q42 0 59 39z'
	),

	torrentIcon: generateSvgIcon(1792, 'rgb(0,0,0)',
		'M1216 928q0-14-9-23t-23-9h-224v-352q0-13-9.5-22.5t-22.5-9.5h-192q-13 0-22.5 9.5t-9.5 22.5v352h-224q' +
		'-13 0-22.5 9.5t-9.5 22.5q0 14 9 23l352 352q9 9 23 9t23-9l351-351q10-12 10-24zm640 224q0 159-112.5 2' +
		'71.5t-271.5 112.5h-1088q-185 0-316.5-131.5t-131.5-316.5q0-130 70-240t188-165q-2-30-2-43 0-212 150-3' +
		'62t362-150q156 0 285.5 87t188.5 231q71-62 166-62 106 0 181 75t75 181q0 76-41 138 130 31 213.5 135.5' +
		't83.5 238.5z'
	),

	pickerIcon: generateSvgIcon(1792, 'rgb(252,0,97)',
		'M1333 566q18 20 7 44l-540 1157q-13 25-42 25-4 0-14-2-17-5-25.5-19t-4.5-30l197-808-406 101q-4 1-12 1' +
		'-18 0-31-11-18-15-13-39l201-825q4-14 16-23t28-9h328q19 0 32 12.5t13 29.5q0 8-5 18l-171 463 396-98q8' +
		'-2 12-2 19 0 34 15z'
	),

	doneIcon: generateSvgIcon(1792, 'rgb(0,0,0)',
		'M1412 734q0-28-18-46l-91-90q-19-19-45-19t-45 19l-408 407-226-226q-19-19-45-19t-45 19l-91 90q-18 18-1' +
		'8 46 0 27 18 45l362 362q19 19 45 19 27 0 46-19l543-543q18-18 18-45zm252 162q0 209-103 385.5t-279.5 2' +
		'79.5-385.5 103-385.5-103-279.5-279.5-103-385.5 103-385.5 279.5-279.5 385.5-103 385.5 103 279.5 279.5' +
		' 103 385.5z'
	),

    loadingGif: 'url(data:image/gif;base64,' +
		'R0lGODlhEgASAMQaAHl5d66urMXFw3l5dpSUk5WVlKOjoq+vrsbGw6Sko7u7uaWlpbm5t3h4doiIhtLSz4aGhJaWlsbGxNHRzrC' +
        'wr5SUkqKiobq6uNHRz4eHhf///wAAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgAaACwAAAAAEgASAAAFaq' +
        'AmjmRplstyrkmbrCNFaUZtaFF0HvyhWRZNYVgwBY4BEmFJOB1NlYpJoYBpHI7RZXtZZb4ZEbd7AodFDIYVAjFJJCYA4ISoI0hyu' +
        'UnAF2geDxoDgwMnfBoYiRgaDQ1WiIqPJBMTkpYaIQAAIfkEBQoAGgAsAQABABAAEAAABWSgJo4aRZEoeaxHOiqKFsyBtizopV9y' +
        'nfwJ0o43MhgNKAYjZbGQJBLXKBLRIK4IaWFbEHgFUoKYoPFKRZUK6fFIORwojBxDytgzpDkdANDc8SQTExp8fBoQEGcDiwNnJA0' +
        'NLiEAACH5BAUKABoALAEAAQAQABAAAAVloCaOmqKQKHmtVzpKksa2FIUiOKIxjHb8B5JgKCAFjgHUMHUkPR6u0WKhwVgx0YQ2cc' +
        'W6DGCDZjKJiiwWEgCQikRQ6zWpQC+QBviBxuHQEP4EKA0NGhmGGRoVFWaHiGYjEBAuIQAAIfkEBQoAGgAsAQABABAAEAAABWSgJ' +
        'o6aJJEoiaxIOj6PJsyCpigopmNyff0X0o43AgZJk0mKwSABAK4RhaJ5PqOH7GHAHUQD4ICm0YiKwCSHI7VYoDLwDClBT5Di8khE' +
        'Y+gbUBAQGgWEBRoWFmYEiwRmJBUVLiEAACH5BAUKABoALAEAAQAQABAAAAVloCaO2vOQKImtWDoCgMa2koTCsDZNGuIjpIFwQBI' +
        'YBahGI2UkORyukUKhyVgz0Yv2csW6thcNBBIVMRikSCRFoaAK8ALpQD+QCHiCZrHQBP4BKBUVGgmGCX6BUQaMBmUkFhYuIQAAIf' +
        'kEBQoAGgAsAQABABAAEAAABWagJo4aAJAoaZrp6DjaIA/a86BZnmlNo2FADEm3GwWFJAgkNZmQIpHWSCLRFK4FKWKLIHgJUoFYo' +
        'KlUpCIxabFIKRSohDxButgvJIPeoKFQNHd4JBYWGgeHBxoMDGgBjgFoJI4tIQAAIfkEBQoAGgAsAQABABAAEAAABWSgJo6a45Ao' +
        'ma1ZOkaRxrYAgBZ4oUGQVtckgpBAGhgHqEol1WiQFgvX6PHQJK4JKWaLMXgNWq7GYpGKJhMShZKSSFCH+IGEqCNIgXxAo1BoBIA' +
        'CKHkaF4YXf4JSh4hmIwwMLiEAACH5BAUKABoALAEAAQAQABAAAAVloCaOWhSRKFmsRToui0bMhOY4aKInWlVpmWCGZCgaSMIhyW' +
        'JJQSAkCsU1AgA0h+yBarUGvgHqYDzQfKmiRoOkUKQeD9RlfiFh7hgSvS6RaPB5JAwMGgiGCBoTE2gCjQJoJI0uIQAAOw==)'

};

/*---------
  Utilities
  ---------*/

function generateSvgIcon(size, color, data) {
	return format('url("data:image/svg+xml,' + 
		'<svg width=\'{0}\' height=\'{0}\' viewBox=\'0 0 {0} {0}\' xmlns=\'http://www.w3.org/2000/svg\'>' +
		'<path fill=\'{1}\' d=\'{2}\'/></svg>")', size, color, data);
}

function createButton(data) {
    var result = document.createElement(data.hasOwnProperty('type') ? data.type : 'a');
    if (data.hasOwnProperty('className')) result.className = data.className;
    if (data.hasOwnProperty('title')) result.title = data.title;
    if (data.hasOwnProperty('onClick')) {
		result.addEventListener('mouseup', data.onClick, false);
		result.addEventListener('click', function(e) { e.preventDefault(); }, false);
		result.addEventListener('contextmenu', function(e) { e.preventDefault(); }, false);
	}
    if (data.hasOwnProperty('parent')) data.parent.appendChild(result);
    if (data.hasOwnProperty('target')) result.setAttribute('target',data.target);
    if (data.hasOwnProperty('style'))
        result.style.cssText = Object.keys(data.style).map(function(x) { return x + ': ' + data.style[x] + 'px'; }).join('; ');
    return result;
}

function format(varargs) {
	var pattern = arguments[0];
	for (var i=1;i<arguments.length;++i) 
		pattern = pattern.replace(new RegExp('\\{' + (i-1) + '\\}', 'g'), arguments[i]);
	return pattern;
}

function xhr(data) {
	return new Promise(function(resolve, reject) {
		var request = {
			method: data.method,
			url: data.url,
			onload: function() { resolve.apply(this, arguments); },
			onerror: function() { reject.apply(this, arguments); }
		};
		if (data.headers) request.headers = data.headers;
		if (data.body && data.body.constructor == String) request.data = data.body;
		else if (data.body) request.data = JSON.stringify(data.body);
		GM_xmlhttpRequest(request);
	});
}

function parseHTML(html) {
	var div = document.createElement('div');
	div.innerHTML = html.replace(/src=/g, 'no-src=');
	return div;
}

function updateUI(data) {
    if (!data || data.error) return;
    var temp = (data.isTorrent ? torrentQueue[data.galleryId] : archiveQueue[data.galleryId]);
    temp.button.className = temp.button.className.replace(/\s*working/, '') + ' requested';
}

function handleFailure(data) {
    if (!data) return;
    var temp = (data.isTorrent ? torrentQueue[data.galleryId] : archiveQueue[data.galleryId]);
    temp.button.className = temp.button.className.replace(/\s*working/, '');
	if (data.error !== 'aborted')
		alert('Could not complete operation.\nReason: ' + (data.error || 'unknown'));
}

/*--------------
  Download Steps
  --------------*/

function obtainArchiverKey(data) {
    return xhr({
		method: 'GET',
		url: format('{0}//{1}/g/{2}/{3}?random={4}',
			window.location.protocol, window.location.host, data.galleryId, data.galleryToken, Date.now())
	})
	.then(function(response) {
		var div = parseHTML(response.responseText);
		var target = div.querySelector('[onclick*="archiver.php"]');
		if (!target) data.error = 'could not resolve archiver key';
		else {
			var tokens = target.getAttribute('onclick').match(/or=([^'"]+)/);
			if (!tokens) data.error = 'could not resolve archiver key';
			else data.archiverKey = tokens[1];
		}
		if (data.error) return Promise.reject(data);
		else return data;
	});
}

function obtainTorrentFile(data) {
    return xhr({
		method: 'GET',
		url: format('{0}//{1}/gallerytorrents.php?gid={2}&t={3}',
			window.location.protocol, window.location.host, data.galleryId, data.galleryToken)
	})
    .then(function(response) {
		var div = parseHTML(response.responseText);
		var forms = div.querySelectorAll('form'), result = null;
		for (var i=0;i<forms.length;++i) {
			var link = forms[i].querySelector('a');
			if (!link) continue;
			var posted = document.evaluate('.//span[contains(text(),"Posted")]', forms[i], null, 9, null).singleNodeValue;
			var seeds = document.evaluate('.//span[contains(text(),"Seeds")]', forms[i], null, 9, null).singleNodeValue;
			if (!posted || !seeds) continue;
			posted = new Date(posted.nextSibling.textContent.trim());
			seeds = parseInt(seeds.nextSibling.textContent, 10);
			if (seeds === 0) continue;
			if (result == null || (result.date - posted < 0))
				result = { date: posted, link: link.href };
		}
		if (result === null) data.error = 'could not find any seeded torrent';
		else data.fileUrl = result.link;
		if (data.error) return Promise.reject(data);
		else return data;
	});
}

function confirmDownloadRequest(data) {
    return xhr({
		method: 'GET',
		url: format('{0}//{1}/archiver.php?gid={2}&token={3}&or={4}',
			window.location.protocol, window.location.host, data.galleryId, data.galleryToken,
			data.archiverKey.replace(/--/, '-'))
	})
	.then(function(response) {
		var div = parseHTML(response.responseText);
		var costLabel = document.evaluate('.//*[contains(text(), "Download Cost:")]', div, null, 9, null).singleNodeValue;
		var sizeLabel = document.evaluate('.//*[contains(text(), "Estimated Size:")]', div, null, 9, null).singleNodeValue;
		if (!costLabel || !sizeLabel)
			return data;
		var cost = costLabel.textContent.replace(/^.+:/, '').trim();
		var size = sizeLabel.textContent.replace(/^.+:/, '').trim();
		var proceed = confirm(format('Size: {0}\nCost: {1}\n\nProceed?', size, cost));
		if (proceed) return data;
		data.error = 'aborted';
		return Promise.reject(data);
	});
}

function submitDownloadRequest(data) {
    return xhr({
		method: 'POST',
		url: format('{0}//{1}/archiver.php?gid={2}&token={3}&or={4}',
			window.location.protocol, window.location.host, data.galleryId, data.galleryToken,
			data.archiverKey.replace(/--/, '-')),
		headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
		body: 'dltype=org&dlcheck=Download+Original+Archive',
	})
	.then(function(response) {
		var div = parseHTML(response.responseText);
		var target = div.querySelector('#continue > a');
		if (target) url = target.href;
		else {
			var targets = div.querySelectorAll('script');
			for (var i=0;i<targets.length;++i) {
				var match = targets[i].textContent.match(/location\s*=\s*"(.+?)"/);
				if (!match) continue;
				url = match[1];
				break;
			}
		}
		if (url) data.archiverUrl = url;
		else data.error = 'could not resolve archiver URL';
		if (data.error) return Promise.reject(data);
		else return data;
	});
}

function waitForDownloadLink(data) {
    return xhr({
		method: 'GET',
		url: data.archiverUrl
	})
	.then(function(response) {
		if (/The file was successfully prepared/i.test(response.responseText)) {
			var div = parseHTML(response.responseText);
			var target = div.querySelector('#db a');
			if (target) {
				var archiverUrl = new URL(data.archiverUrl);
				data.fileUrl = archiverUrl.protocol + '//' + archiverUrl.host + target.getAttribute('href');
			} else data.error = 'could not resolve file URL';
		} else
			data.error = 'archiver did not provide file URL';
		if (data.error) return Promise.reject(data);
		else return data;
	})
	.catch(function() {
		if (data.error) return Promise.reject(data);
		data.error = 'could not contact archiver';
		if (/https/.test(window.location.protocol)) {
			data.error += '; this is most likely caused by mixed-content security policies enforced by the' +
				' browser that need to be disabled by the user. If you have no clue how to do that, you' +
				' should probably Google "how to disable mixed-content blocking".';
		} else {
			data.error += '; please check whether your browser is not blocking XHR requests towards' +
				' 3rd-party URLs';
		}
		return Promise.reject(data);
	});
}

function downloadFile(data) {
	var a = document.createElement('a');
	a.href = data.fileUrl;
	document.body.appendChild(a);
	a.click();
	document.body.removeChild(a);
	document.body.appendChild(a);
	return Promise.resolve(data);
}


/*----------------
  State Management
  ----------------*/

var archiveQueue = { }, torrentQueue = { };

function requestDownload(e) {
    var isTorrent = /torrentLink/.test(e.target.className);
    if (/working|requested/.test(e.target.className)) return; 
	if (e.which !== 1 && (e.which !== 3 || isTorrent)) return;
    e.preventDefault();
	e.stopPropagation();
    e.target.className += ' working';
    var tokens = e.target.getAttribute('target').match(/\/g\/(\d+)\/([0-9a-z]+)/i);
    var galleryId = parseInt(tokens[1], 10), galleryToken = tokens[2];
	var askConfirmation = (!isTorrent && e.which === 3);
    if (!isTorrent) {
        archiveQueue[galleryId] = { token: galleryToken, button: e.target };
        var promise = obtainArchiverKey({ galleryId: galleryId, galleryToken: galleryToken, isTorrent: false });
		if (askConfirmation) promise = promise.then(confirmDownloadRequest);
		promise
            .then(submitDownloadRequest)
            .then(waitForDownloadLink)
            .then(downloadFile)
            .then(updateUI)
			.catch(handleFailure);
    } else {
        torrentQueue[galleryId] = { token: galleryToken, button: e.target };
        obtainTorrentFile({ galleryId: galleryId, galleryToken: galleryToken, isTorrent: true })
            .then(downloadFile)
            .then(updateUI)
			.catch(handleFailure);
    }
	return false;
}

/*--------
  UI Setup
  --------*/

window.addEventListener('load', function() {

    // button generation (thumbnail list)
    var thumbnails = document.querySelectorAll('.id3 > a'), n = thumbnails.length;
    while (n-- > 0) {
        createButton({
			title: 'Automated download',
			target: thumbnails[n].href,
			className: 'automatedButton downloadLink',
			onClick: requestDownload,
			style: { bottom: 0, right: 0 },
			parent: thumbnails[n]
		});
        createButton({
			title: 'Torrent download',
			target: thumbnails[n].href,
			className: 'automatedButton torrentLink',
			onClick: requestDownload,
			style: { bottom: 0, left: 1 },
			parent: thumbnails[n]
		});
    }

    // button generation (gallery)
    var bigThumbnail = document.querySelector('#gd1 > div');
    if (bigThumbnail !== null) {
        createButton({
			title: 'Automated download',
			target: window.location.href,
			className: 'automatedButton downloadLink',
			onClick: requestDownload,
            style: { bottom: 0, right: 0 },
			parent: bigThumbnail
		});
        createButton({
			title: 'Torrent download',
			target: window.location.href,
			className: 'automatedButton torrentLink',
			onClick: requestDownload,
            style: { bottom: 0, left: 0 },
			parent: bigThumbnail
		});
    }

    // button generation (row list)
    var rows = document.querySelectorAll('.it5 > a'), n = rows.length;
    while (n-- > 0) {
        var div = createButton({
			type: 'div',
			className: 'automatedPicker',
			onClick: requestDownload,
			parent: rows[n].parentNode
		});
        var picker = createButton({ type: 'div', parent: div });
        createButton({
			type: 'div',
			title: 'Torrent download', 
            target: rows[n].href,
			className: 'automatedInline torrentLink',
			parent: picker
		});
        createButton({
			type: 'div',
			title: 'Automated download',
            target: rows[n].href,
			className: 'automatedInline downloadLink',
			parent: picker
		});
    }

    // document style
    var style = document.createElement('style');
    style.innerHTML =
		// Icons and colors
        '.downloadLink  { background-image: ' + ASSETS.downloadIcon + '; background-color: rgb(220,98,98); }' +
        '.torrentLink  { background-image: ' + ASSETS.torrentIcon + '; background-color: rgb(98,182,210); }' +
        '.requested  { background-image: ' + ASSETS.doneIcon + '; }' +
        '.requested, .working { background-color: rgba(128,226,126,1); }' +
        '.working { background-image: ' + ASSETS.loadingGif + ' !important; background-repeat: no-repeat; }' +
        '.automatedPicker { background-image: ' + ASSETS.pickerIcon + '; }' +
        '.automatedButton:hover, .automatedInline:hover { background-color: rgba(255,199,139,1) }' +
		// Positioning
		'.id3 > a, #gd1 > div { position: relative; display: flex; max-height: 100%; }' +
		'#gd1 > div > .downloadLink { right: -1px !important; }' +
		// Backgrounds
		'.automatedButton { background-size: 20px 20px; background-position: 5px 5px; background-repeat: no-repeat; }' +
		'.automatedPicker { background-size: 12px 12px; background-position: 2px 2px; background-repeat: no-repeat; }' +
		'.automatedInline { background-size: 13px 13px; background-position: 5px 5px; background-repeat: no-repeat; }' +
		// Others (thumbnail mode)
        '.automatedButton { display: none; position: absolute; text-align: left; cursor: pointer;' +
			'color: white; margin-right: 1px; font-size: 20px; line-height: 11px; width: 28px; height: 28px; }' +
        '.automatedButton.downloadLink  { border-radius: 0 0 5px 0 !important; }' +
        '.automatedButton.torrentLink  { border-radius: 0 0 0 5px !important; }' +
        '#gd1 > div > .automatedButton { border-radius: 0 0 0 0 !important; }' +
        '.automatedButton.working { font-size: 0px; }' +
        '*:hover > .automatedButton, .automatedButton.working, .automatedButton.requested { display: block !important; }' +
		// Others (list mode)
        '.automatedPicker { width: 16px; height: 16px; float: left; cursor: pointer; }' + 
        '.automatedPicker > div { display: none; z-index: 2; position: absolute; top: -4px; text-align: center; }' +
        '.automatedPicker:hover > div, .automatedPicker > div:hover { display: block; }' +
        '.automatedInline { border: 1px solid black; width: 23px; height: 23px; display: inline-block; }' +
        '.automatedInline:first-child { border-right: none !important; }';
    document.head.appendChild(style);

}, false);