clumsyman / 123du

// ==UserScript==
// @name         123du
// @namespace    http://tampermonkey.net/
// @version      0.2.4
// @updateURL https://openuserjs.org/meta/clumsyman/123du.meta.js
// @downloadURL https://openuserjs.org/install/clumsyman/123du.user.js
// @description  load multile chapters
// @author       clumsyman
// @copyright 2018, clumsyman (https://openuserjs.org//users/clumsyman)
// @license MIT
// @match        https://m.123dux.com/dudu-*/*/*.html
// @grant        none
// ==/UserScript==

javascript:(function () {
function getBody(content) {
	var x = content.indexOf('<body');
	x = content.indexOf('>', x);
	var y = content.lastIndexOf('</body>');
	return content.slice(x + 1, y);
}
function queryContent(url) {
	try {
		var xhr = new XMLHttpRequest();
		xhr.open('GET', url, false);
		xhr.overrideMimeType('text\/html; charset=gbk');
		xhr.send();
		if (xhr.readyState == 4 && xhr.status == 200) {
			load_store.innerHTML = getBody(xhr.responseText);
			var content = findContent(load_store);
			cleanup(content);
			var nextUrl = getNextUrl(findPageSet(load_store));
			return [content.innerHTML, nextUrl];
		} else {
			alert('readyState='+xhr.readyState+', status='+xhr.status+', statusText='+xhr.statusText);
		}
	} catch(e) {
		alert(e + '\n' + e.stack);
	} finally {
		load_store.innerHTML = '';
	}
}
function createStore(id) {
	var store = document.getElementById(id);
	if (!store) {
		store = document.createElement('div');
		store.id = id;
		store.style.display = 'none';
		document.body.appendChild(store);
	}
	return store;
}
function findContent(container) {
	return container.querySelector('.TxtContent');
}
function findPageSet(container) {
	return container.querySelector('#PageSet');
}
function getNextUrl(pageSet) {
	if (pageSet && pageSet.lastChild && 
		pageSet.lastChild.nodeName == "A" && 
		pageSet.lastChild.text == "下—页") {
		return pageSet.lastChild.href;
	}
}
function cleanup(content) {
	if (content.lastChild.nodeName == "#text" && 
		content.lastChild.nodeValue == "  本章没完,请点击下—页继续阅读!如果被转码了请退出转码或者更换浏揽器即可。") {
		content.removeChild(content.lastChild);
		if (content.lastChild.nodeName == "BR") {
			content.removeChild(content.lastChild);
			if (content.lastChild.nodeName == "#text" &&
				content.lastChild.nodeValue.endsWith("….")) {
				content.lastChild.nodeValue = content.lastChild.nodeValue.slice(0, -2);
			}
		}
	}
}
function restyle() {
	var style = document.body.querySelector('style#clumsyman');
	if (style) {
		alert('found embedded <style>');
		return false;
	}
	style = document.body.appendChild(document.createElement('style'));
	style.id = 'clumsyman';
	style.append(document.createTextNode([
    '.TxtContent {font-size: 20px; line-height: 22px; }',
		'.DivBottom, .DivBottom + div, h2.vbgcol, .S2 { display: none; }',
		'#header { top: 0; }',
		'#footer { bottom: 0; }',
		'#header, #footer { position: fixed; width: 100%; }',
		'#header.hide, #footer.hide { display: none; }',
		'.VolumeTop { font-size: inherit; height: auto; margin: 0; padding: 0;}',
		'.VolumeNext { margin: 0; }'
	].join('\n')));
	var anchor = document.body.querySelector('a[name="tobottomebb"]');
	if (anchor && anchor.nextSibling &&
		anchor.nextSibling.nodeName == "#text" &&
		anchor.nextSibling.nodeValue == "ff\n") {
		anchor.nextSibling.parentNode.removeChild(anchor.nextSibling);
	}

	var node = document.body.querySelector('.sau.FCol');
    if (!node) return false;
	node.parentNode.insertBefore(node, node.previousElementSibling);
	document.body.querySelector('.VolumeTop.vbgcol').remove();

	var header = document.body.appendChild(document.createElement('div'));
	header.id = "header";
	header.appendChild(document.body.querySelector('.Nav.bgcol'));
	header.appendChild(document.body.querySelector('.searchForm.bgcol'));
	
	var footer = document.body.appendChild(document.createElement('div'));
	footer.id = "footer";
	var navs = document.body.querySelectorAll('.VolumeNext.vbgcol.FCol');
	var nav0 = navs[0];
	var nav1 = navs[1];
	var nav2 = navs[2];
	var prev = nav0.querySelector('span:first-child');
	var next = nav0.querySelector('span:last-child');
	var cover = nav0.querySelector('span.m');
	var shelf = nav2.querySelector('span:first-child');
	shelf.classList.add('m');
	var mark = nav2.querySelector('span.m');
	var top = nav0.querySelector('span:last-child');
	nav2.replaceChild(next, shelf);
	nav1.replaceChild(shelf, nav1.querySelector('span.m'));
	var report = document.body.querySelector('.sErr.FCol span');
	nav1.replaceChild(report, nav1.lastElementChild);
	footer.appendChild(nav1);
	footer.appendChild(nav2);
	nav0.remove();
	document.body.querySelector('.sErr.FCol').remove();

	var content = findContent(document.body);
	content.addEventListener("click", function() {
		header.classList.toggle("hide");
		footer.classList.toggle("hide");
	});
	return true;
}
function completeContent() {
	var load_store = createStore('load_store');
	var content = findContent(document.body);
	cleanup(content);
	var nextUrl = getNextUrl(pageSet);
	while (nextUrl) {
		var nextPage = queryContent(nextUrl);
		content.innerHTML += nextPage[0];
		nextUrl = nextPage[1];
	}
	pageSet.parentNode.removeChild(pageSet);
}
try {
	if (!restyle()) {
		return;
	}
    var pageSet = findPageSet(document.body);
	if (pageSet) {
		window.setTimeout(completeContent, 1000);
	}
} catch(e) {
	alert(e + '\n' + e.stack);
}
})();