clumsyman / test

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

function parseReplacementPY(s) {
	var i = s.indexOf('=');
	var pair = i < 0 ? [s, ''] : [s.substring(0, i), s.substring(i + 1)];
	pair[0] = new RegExp('([^a-zA-Z0-9])'+pair[0]+'([^a-zA-Z0-9])', 'g');
	pair[1] = '$1<span class=c_red>'+pair[1]+'</span>$2';
	return pair;
}
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=波&',
	true);
	
	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');
	}