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://www.ciymao.com/chapter/*
// @match        https://m.zhaishuyuan.com/chapter/*
// @match        https://mp.weixin.qq.com/s/*
// @grant        none
// ==/UserScript==

javascript:(function () {
try {
	function toRE(s) {
		var i = s.indexOf('=');
		return [new RegExp(i < 0 ? s : s.substring(0, i)), i < 0 ? '' : s.substring(i + 1)];
	}
	function parseReplacements(query, fn) {
		return query.split('&')
			.filter(s => s.length > 0)
			.map(fn);
	}
	var replacements = parseReplacements('[?\?]=\u2047', toRE);
	function cleanText(content) {
		switch(content.nodeType) {
		case Node.ELEMENT_NODE:
		    switch(content.nodeName) {
			case 'SCRIPT':
			case 'STYLE':
		    	break;
			case'SPAN':
			    if (content.classList.contains('replacement'))
			        break;
		    default:
				var child = content.firstChild, next;
				while(child) {
					cleanText(child);
					next = child.nextSibling;
					if (child.nodeType == Node.TEXT_NODE && child.nodeValue == "")
					    content.removeChild(child);
					child = next;
    		    }
		    }
		    break;
		case Node.TEXT_NODE:
            var rs = replacements[0][0].exec(content.nodeValue);
            if (rs) {
            	var before = rs.input.slice(0, rs.index);
            	var after = rs.input.slice(rs.index + rs[0].length);
            	if (after.length > 0)
            	    content.parentNode.insertBefore(document.createTextNode(after), content.nextSibling);
            	var replaced = createReplacementWrapper(rs[0], replacements[0][1]);
            	content.parentNode.insertBefore(replaced, content.nextSibling);
            	content.nodeValue = before;
            }
            break;
		}
		return content;
	}
	function toggleReplacementWrapper() {
		this.classList.toggle('replaced');
	}
	function createReplacementInner(className, text) {
		var span = document.createElement('span');
		span.className = className;
		span.textContent = text;
		return span;
	}
	function createReplacementWrapper(original, replacer) {
		var span = document.createElement('span');
		span.className = 'replacement replaced';
		span.appendChild(createReplacementInner('original', original));
		span.appendChild(createReplacementInner('replacer', replacer));
		span.addEventListener('click', toggleReplacementWrapper);
		return span;
	}
	function createReplacementStyles() {
		var style = document.createElement('style');
		style.append(document.createTextNode('.replacement { border: #0099ff solid 1px; }\n'));
		style.append(document.createTextNode('.replacer { color: #ff6600; }\n'));
		style.append(document.createTextNode('.replacement.replaced .original { display: none; }\n'));
		style.append(document.createTextNode('.replacement:not(.replaced) .replacer { display: none; }\n'));
		style.append(document.createTextNode('.chapter_box { word-wrap: break-word; }\n'));
		style.append(document.createTextNode('.chapter_title {\n'
		    +'  background: #ecfcfc;\n'
		    +'  border: solid 1px #e8e8e8;\n'
		    +'  color: #8dadad;\n'
		    +'  text-align: center;\n'
		    +'}\n'));
		document.head.append(style);
		return style;
	}
	function getBody(content) {
		var x = content.indexOf('<body');
		x = content.indexOf('>', x);
		var y = content.lastIndexOf('</body>');
		return content.slice(x + 1, y);
	}
	function ciymao() {
		var global_find = $;
		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);
		}
		function findPrev(a, b) {
			return global_find(a, '#prev_store');
		}
		function loadPrev(body) {
			prev_store.innerHTML = body;
			var scripts = prev_store.querySelectorAll('#center script:not([src])');
			for (var i = 0; i < scripts.length; i++)
				eval(scripts[i].innerText);
			return prev_store;
		}
		function updatePrevChapterLinks(prev_url) {
			for (var i = 0; i < nr_pages.length; i++) {
				nr_pages[i].firstElementChild.href = prev_url;
			}
		}
		function getPrevUrl(container) {
			return container.querySelector('.chapter-page').firstElementChild.href;
		}
		function invokeDecode(container) {
			var scripts = container.querySelectorAll('script:not([src])');
			for (var i = 0; i < scripts.length; i++) {
				var idx = scripts[i].innerText.indexOf('function getDecode(){');
				if (idx >= 0) {
					eval(scripts[i].innerText.substring(idx));
					getDecode();
				}
			}
		}
		function findContent(container) {
			invokeDecode(container);
			var content = container.querySelector('.chapter-name').nextElementSibling.nextElementSibling;
			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]);
			return cleanText(content);
		}
		function findTitle(container) {
			var title = document.createElement('p');
			title.className = 'chapter_title';
			var nodes = container.querySelector('.chapter-name').childNodes;
			for(var i = 0; i < nodes.length; i++)
				title.appendChild(nodes[i]);
			return cleanText(title);
		}
		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);
					$ = findPrev;
					loadPrev(body);
					var prev_content = findContent(prev_store);
					$ = global_find;
					for (var last; last = prev_content.lastElementChild; )
						wap_content.insertBefore(last, wap_content.firstElementChild);
					var prev_title = findTitle(prev_store);
					wap_content.insertBefore(prev_title, wap_content.firstElementChild);
					updatePrevChapterLinks(getPrevUrl(prev_store));
				} else {
					alert('readyState='+xhr.readyState+', status='+xhr.status+', statusText='+xhr.statusText);
				}
			} catch(e) {
				alert(e + '\n' + e.stack);
			} finally {
				return false;
			}
		}
		if (document.styleSheets.length > 0)
		    document.styleSheets[0].disabled = true;
		var style = createReplacementStyles();
		style.append(document.createTextNode('html { -webkit-touch-callout:default; -webkit-user-select:text; }\n'));
		style.append(document.createTextNode('#loading, .show, .flt_setting, .copyright, .subhead, .book_clist, .layout-con { display: none; }\n'));
        style.append(document.createTextNode('.search-btn {\n'
			+'    width: 22px;\n'
			+'    height: 22px;\n'
			+'    background: url(https:\/\/img.zhaishuyuan.com/zhai/img/sprites.png) -7px -77px no-repeat #49A9A9;\n'
			+'    border: 0;\n'
			+'    text-indent: -9999px;\n'
			+'}\n'));
		var wap_content = findContent(document.body);
		wap_content.insertBefore(findTitle(document.body), wap_content.firstElementChild);
		var nr_pages = document.querySelectorAll('.chapter-page');
		for (var i = 0; i < nr_pages.length; i++) {
			var nr_page = nr_pages[i];
			nr_page.firstElementChild.onclick = loadPrevChapter;
		}
		var search = document.querySelector('#search>.search-text');
		if (search)
		    search.removeAttribute('placeholder');					
		$(window).off('scroll');
	}
	function zhaishuyuan() {
		var global_find = $;
		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);
		}
		function findPrev(a, b) {
			return global_find(a, '#prev_store');
		}
		function loadPrev(body) {
			prev_store.innerHTML = body;
			var scripts = prev_store.querySelectorAll('#center script:not([src])');
			for (var i = 0; i < scripts.length; i++)
				eval(scripts[i].innerText);
			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 invokeDecode(container) {
			var scripts = container.querySelectorAll('script:not([src])');
			for (var i = 0; i < scripts.length; i++) {
				var idx = scripts[i].innerText.indexOf('function getDecode(){');
				if (idx >= 0) {
					eval(scripts[i].innerText.substring(idx));
					getDecode();
				}
			}
		}
		function findContent(container) {
			invokeDecode(container);
			var content = container.querySelector('#award_win').nextElementSibling;
			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]);
			return cleanText(content);
		}
		function findTitle(container) {
			var title = container.querySelector('#nr_title');
			title.className = 'chapter_title';
			return cleanText(title);
		}
		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);
					$ = findPrev;
					loadPrev(body);
					var prev_content = findContent(prev_store);
					$ = global_find;
					for (var last; last = prev_content.lastElementChild; )
						wap_content.insertBefore(last, wap_content.firstElementChild);
					var prev_title = findTitle(prev_store);
					wap_content.insertBefore(prev_title, wap_content.firstElementChild);
					updatePrevChapterLinks(getPrevUrl(prev_store));
				} else {
					alert('readyState='+xhr.readyState+', status='+xhr.status+', statusText='+xhr.statusText);
				}
			} catch(e) {
				alert(e + '\n' + e.stack);
			} finally {
				return false;
			}
		}
		createReplacementStyles();
		var wap_content = findContent(document.body);
		wap_content.insertBefore(findTitle(document.body), wap_content.firstElementChild);
		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';
		}
		$(window).off('scroll');
	}
	function weixin() {
		createReplacementStyles();
		cleanText(document.getElementById('activity-name'));
		cleanText(document.getElementById('js_content'));
		document.getElementById('meta_content').style.display = 'none';
	}
	function mismatch() {
		alert(window.location.hostname);
	}
	var handlers = {
	    'www.ciymao.com': ciymao,
		'm.zhaishuyuan.com': ciymao,
		'mp.weixin.qq.com': weixin
	};
	(handlers[window.location.hostname] || mismatch)();
} catch(e) {
	alert(e + '\n' + e.stack);
}
})();