// ==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
// @grant GM.xmlHttpRequest
// @run-at document-start
// @author etc
// @version 2.1.5
// @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'
),
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);
if (typeof(GM_xmlhttpRequest) !== 'undefined') GM_xmlhttpRequest(request);
else if (typeof(GM) !== 'undefined') GM.xmlHttpRequest(request);
else reject(new Error('Could not submit XHR 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'));
}
function xpathFind(root, nodeType, text) {
return document.evaluate('.//' + (nodeType || '*') + '[contains(text(), "' + text + '")]', root, null, 9, null).singleNodeValue;
}
function pickTorrent(candidates, lastUpdateDate) {
var currentScore = 0, currentCandidate = null;
// Get max values
var maxSeeds = candidates.reduce(function(p,n) { return Math.max(p, n.seeds); }, 0);
var maxSize = candidates.reduce(function(p,n) { return Math.max(p, n.size); }, 0);
var newest = candidates.reduce(function(p,n) { return Math.max(p, n.date.valueOf()); }, 0);
// Calculate scores
candidates.forEach(function(candidate) {
var seedScore = candidate.seeds / maxSeeds;
var sizeScore = candidate.size / maxSize;
var dateScore = 1;
if (lastUpdateDate && newest > lastUpdateDate) {
dateScore = (candidate.date.valueOf() - lastUpdateDate) / (newest - lastUpdateDate);
if (dateScore < 0) dateScore = 0.1; // galleries posted before the last update automatically get 0.1
}
// Total score
var score = seedScore * sizeScore * dateScore;
if (currentScore >= score) return;
currentScore = score;
currentCandidate = candidate;
});
return currentCandidate;
}
/*--------------
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'), candidates = [ ];
var findValue = function(text) {
var target = xpathFind(forms[i], 'span', text);
return (target ? target.nextSibling.textContent.trim() : null);
};
for (var i=0;i<forms.length;++i) {
var link = forms[i].querySelector('a');
if (!link) continue;
// Gather torrent data
var posted = new Date(findValue('Posted')), size = findValue('Size'),
seeds = parseInt(findValue('Seeds'), 10) || 0;
size = parseFloat(size, 10) * (/MB/i.test(size) ? 1024 : (/GB/i.test(size) ? 1024 * 1024 : 1));
// Ignore torrents with invalid sizes or no seeds
if (size === 0 || seeds === 0) continue;
candidates.push({ link: link.href, date: posted, size: size, seeds: seeds });
}
if (candidates.length === 0) data.error = 'could not find any suitable torrent';
else data.fileUrl = pickTorrent(candidates, data.date).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 = xpathFind(div, '*', 'Download Cost:');
var sizeLabel = xpathFind(div, '*', 'Estimated Size:');
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 enqueueDownload(data) {
downloadQueue = downloadQueue
.then(function() { return downloadFile(data); })
.then(delayPromise(500));
return downloadQueue;
}
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);
}
function delayPromise(delay) {
return function(data) {
return new Promise(function(resolve) {
setTimeout(function() { resolve(data); }, delay);
});
};
}
/*----------------
State Management
----------------*/
var archiveQueue = { }, torrentQueue = { };
var downloadQueue = Promise.resolve();
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(enqueueDownload)
.then(updateUI)
.catch(handleFailure);
} else {
// Try to find out gallery's last update date if possible
var galleryDate = xpathFind(document, 'td', 'Posted:'); // gallery page
if (galleryDate) galleryDate = galleryDate.nextSibling;
else // thumbnail mode
galleryDate = document.evaluate('./ancestor::tr/td[@class="itd"]', e.target, null, 9, null).singleNodeValue;
if (galleryDate !== null) galleryDate = new Date(galleryDate.textContent.trim());
// Gather data
torrentQueue[galleryId] = { token: galleryToken, button: e.target };
obtainTorrentFile({ galleryId: galleryId, galleryToken: galleryToken, isTorrent: true, date: galleryDate })
.then(enqueueDownload)
.then(updateUI)
.catch(handleFailure);
}
return false;
}
/*--------
UI Setup
--------*/
window.addEventListener('load', function() {
// button generation (thumbnail / extended)
var thumbnails = document.querySelectorAll('.gl3t, .gl1e > div'), n = thumbnails.length;
while (n-- > 0) {
createButton({
title: 'Automated download',
target: thumbnails[n].querySelector('a').href,
className: 'automatedButton downloadLink',
onClick: requestDownload,
style: { bottom: 0, right: -2 },
parent: thumbnails[n]
});
createButton({
title: 'Torrent download',
target: thumbnails[n].querySelector('a').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
});
}
// 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; }' +
'.automatedButton:hover { background-color: rgba(255,199,139,1) }' +
// Positioning
'#gd1 > div, .gl3t, .gl1e > div { position: relative; }' +
// Backgrounds
'.automatedButton { background-size: 20px 20px; 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; }' +
'#gd1 > div:hover .automatedButton, .gl3t:hover .automatedButton, .gl1e > div:hover .automatedButton, .automatedButton.working, .automatedButton.requested { display: block !important; }';
document.head.appendChild(style);
}, false);