E(x)-Hentai One-Click DL

Enables one-click DL archive downloading (GP/Credits are still required). Supports E-Hentai & EXHentai.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         E(x)-Hentai One-Click DL
// @description  Enables one-click DL archive downloading (GP/Credits are still required). Supports E-Hentai & EXHentai.
// @author       Daku ([email protected])
// @namespace    https://github.com/DakuTree/userscripts
// @homepage     https://github.com/DakuTree/userscripts
// @homepageURL  https://github.com/DakuTree/userscripts
// @supportURL   https://github.com/DakuTree/userscripts/issues
// @icon         https://e-hentai.org/favicon.ico
// @match        https://e-hentai.org/g/*
// @match        https://exhentai.org/g/*
// @version      1.4.1
// ==/UserScript==

/******SETTINGS******/
var showCostPopup = false; // set to true to show a confirm popup including the cost of the gallery (this is still ignored if you have free gallery downloads via donation)
var downloadOriginal = true;
/********************/

//Auto-redirect to https & non g.e (NOTE: This will be done by EH automatically some time in the future so this is just a temp-fix.)
if (location.hostname !== 'exhentai.org' && location.origin !== "https://e-hentai.org") {
	location.href = (location.href.replace(/^https?:\/\/(?:g\.)?e-hentai\.org/, "https://e-hentai.org"));
} else if (location.hostname === 'exhentai.org' && location.protocol !== 'https:') {
	location.href = (location.href.replace(/^https?:\/\/exhentai\.org/, "https://exhentai.org"));
}

var a = document.getElementsByClassName('g2')[0].getElementsByTagName('a')[0]; // "Archive Download" link
var xs = a.getAttributeNode('onclick').nodeValue.split('?')[1].split("'")[0]; //  Grab arg from onclick attr
a.removeAttribute('onclick'); // Remove default onclick

a.addEventListener('click', function () {
	var downloadGallery = true;

	if (showCostPopup) {
		var http_cost = new XMLHttpRequest();
		http_cost.open("GET", location.origin + '/archiver.php?' + xs, false);
		http_cost.onreadystatechange = function () {
			if (http_cost.readyState == 4 && http_cost.status == 200) {
				var match_cost = http_cost.responseText.match(/Download Cost:\s+(.*)/);

				//If gallery has a download cost, and is not free, ask the user if they want to DL the gallery via a confirm popup.
				if (match_cost && match_cost[1].replace(/<(?:.|\n)*?>/gm, '').replace(/&nbsp;/gi, '').trim() !== "Free!") {
					var cost = match_cost[1].replace(/<(?:.|\n)*?>/gm, '').replace(/&nbsp;/gi, '').trim();
					downloadGallery = confirm("This gallery is not free.\nThe cost is: " + cost + ".\nAre you sure you want to DL?");
				}
			}
		};
		http_cost.send();
	}

	if (downloadGallery) {
		var http = new XMLHttpRequest();
		var params = "dlcheck=Download Original Archive&dltype=org";
		if (!downloadOriginal) {
			params = "dlcheck=Download Resample Archive&dltype=res";
		}
		http.open("POST", location.origin + '/archiver.php?' + xs, true);
		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

		http.onreadystatechange = function () {
			if (http.readyState == 4 && http.status == 200) {
				if (http.responseText == 'Insufficient funds.') {
					alert('Insufficient funds.');
				}
				else if (http.responseText.indexOf("Please wait...") !== -1) { //Check if pop-up opened.
					var match = /<a href="(http.*?)"/g.exec(http.responseText);
					window.location.href = match[1] + "?start=1";
				}
				else {
					//WTF?
				}
			}
		};
		http.send(params);
	}

	return false;
}, false);