clumsyman / zhaishuyuan

// ==UserScript==
// @name         zhaishuyuan
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  load multile chapters
// @author       clumsyman
// @copyright 2018, clumsyman (https://openuserjs.org//users/clumsyman)
// @license MIT
// @match        https://m.zhaishuyuan.com/chapter/*
// @grant        none
// ==/UserScript==

javascript:(function () {
	function parseReplacement(s) {
		var i = s.indexOf('=');
		var pair = i < 0 ? [s, ""] : [s.substring(0, i), s.substring(i + 1)];
		pair[0] = new RegExp(pair[0], "g");
		return pair;
	}
	function parseReplacements(query) {
		return query.split('&')
			.filter(s => s.length > 0)
			.map(parseReplacement);
	}
	var replacements = parseReplacements('chuáng=床&'
		+'xiong=胸&'
		+'xing=性&'
		+'xiǎo=小&'
		+'ting=挺&'
		+'nong=弄&'
		+'luàn=乱&'
		+'làng=浪&'
		+'jing=精&'
		+'jiāo=交&'
		+'jiān=奸&'
		+'dong=洞&'
		+'dàng=荡&'
		+'chun=唇&'
		+'chou=抽&'
		+'cháo=潮&'
		+'you=诱&'
		+'yin=阴&'
		+'yào=药&'
		+'tui=腿&'
		+'shè=射&'
		+'sāo=骚&'
		+'rou=肉&'
		+'mén=门&'
		+'máo=毛&'
		+'luo=裸&'
		+'hun=混&'
		+'huā=花&'
		+'féi=肥&'
		+'chā=插&'
		+'si=私&'
		+'sè=色&'
		+'ru=乳&'
		+'nv=女&'
		+'nǎ=奶&'
		+'mo=摸&'
		+'mi=迷&'
		+'lu=露&'
		+'ku=裤&'
		+'ji=激&'
		+'fu=府&'
		+'bo=波&');
	function getBody(content) {
		var x = content.indexOf('<body');
		x = content.indexOf('>', x);
		var y = content.lastIndexOf('</body>');
		return content.slice(x + 1, y);
	}
	function cleanContent(content) {
		var a_c = content.querySelectorAll('a.c_red');
		for (var i = 0; i < a_c.length; i++)
			a_c[i].parentElement.removeChild(a_c[i]);
		a_c = content.querySelectorAll('a.c_green');
		for (var i = 0; i < a_c.length; i++)
			a_c[i].parentElement.removeChild(a_c[i]);
		content.innerHTML = replacements.reduce(function(s, p) {
			return s.replace(p[0], '<span class=\"c_red\">'+p[1]+'</span>');
		}, content.innerHTML);
	}
	function loadPrev(body) {
		var prev_store = document.getElementById('prev_store');
		if (!prev_store) {
			prev_store = document.createElement('div');
			prev_store.id = 'prev_store';
			prev_store.style.display = 'none';
			document.body.appendChild(prev_store);
		}
		prev_store.innerHTML = body;
		return prev_store;
	}
	function updatePrevChapterLinks(prev_url) {
		for (var i = 0; i < nr_pages.length; i++) {
			nr_pages[i].firstElementChild.firstElementChild.href = prev_url;
		}
	}
	function getPrevUrl(container) {
		return container.querySelector('.nr_page').firstElementChild.firstElementChild.href;
	}
	function findContent(container) {
		return container.querySelector('#award_win').nextElementSibling;
	}
	var nr_title = document.getElementById('nr_title');
	var wap_content = findContent(document.body);
	cleanContent(wap_content);
	wap_content.insertBefore(nr_title, wap_content.firstElementChild);
	function loadPrevChapter() {
		try {
			var xhr = new XMLHttpRequest();
			xhr.open('GET', getPrevUrl(document.body), false);
			xhr.overrideMimeType('text\/plain; charset=gb2312');
			xhr.send();
			if (xhr.readyState == 4 && xhr.status == 200) {
				var body = getBody(xhr.responseText);
				var prev_store = loadPrev(body);
				var prev_title = prev_store.querySelector('#nr_title');
				var prev_content = findContent(prev_store);
				cleanContent(prev_content);
				updatePrevChapterLinks(getPrevUrl(prev_store));
				for (var last; last = prev_content.lastElementChild; )
					wap_content.insertBefore(last, wap_content.firstElementChild);
				wap_content.insertBefore(prev_title, wap_content.firstElementChild);
			} else {
				alert('readyState='+xhr.readyState+', status='+xhr.status+', statusText='+xhr.statusText);
			}
		} catch(e) {
			alert(e + "\n" + e.stack);
		} finally {
			return false;
		}
	}
	var nr_pages = document.querySelectorAll('.nr_page');
	for (var i = 0; i < nr_pages.length; i++) {
		var nr_page = nr_pages[i];
		nr_page.firstElementChild.firstElementChild.onclick = loadPrevChapter;
		nr_page.querySelector('.m-left-right').style.margin = '0 12px';
	}
	loadPrevChapter();
})();