bumprat / 新御书屋

// ==UserScript==
// @name         新御书屋
// @namespace    http://tampermonkey.net/
// @version      0.1
// @license MIT
// @description  try to take over the world!
// @author       You
// @match        http://www.xinyushuwu.com/*
// @grant        none
// ==/UserScript==

(function() {
	'use strict';
	var script = document.createElement("script");
	script.type = "text/javascript";
	script.src = "http://libs.baidu.com/jquery/2.0.0/jquery.min.js";
	script.onload = function(){
		var $ = window.$;
		var chapters = $('.ml_list a[href]');
		window.passage = $('<div id="ppppp"></div>');
		window.errs = $('<div style="color:red"></div>');
		window.loadInterval = $('<input type="text" value="0"></input>');
		window.loadPassage = $('<button style="margin-right:20px">读取全文</button>');
		window.download = $('<button disabled>下载</button>');
		window.copy = $('<button disabled>复制</button>');
		window.copy.click(function(){
			selectText(window.passage[0]);
			document.execCommand("copy");
		});
		window.loadPassage.click(async function(){
			window.output = new Array(chapters.length);
			let promises = []
			function one(i, o){
				let p = new Promise((resolver)=>{
					console.log($(o).attr('href'))
					$.ajax({
						type: "get",
						url:$(o).attr('href'),
						beforeSend: function(xhr){
							xhr.overrideMimeType("text/csv;charset=gb2312");
						},
						success: function(xmlDoc, textStatus){
							if(textStatus == 'success')
							{
								var title = "<br/>第"+(i+1)+"章:"+$(o).html()+"<br/>";
								window.output[i] = title + $(xmlDoc).find('#articlecontent').html();
								window.passage.html(window.output.join('').trim());
								resolver()
							}
						},
						error: function(){
							var title = "章节:"+$(o).html();
							window.errs.html(title + " 加载错误!<br/>" + window.errs.html() );
							resolver()
						},
						timeout: i*window.loadInterval.val()
					});
				})
				return p
				//promises.push(p)
			}
			let i = 0
			for(let c of chapters.toArray()){
				await one(i, c)
				i++
			}
			//await Promise.all(promises)
			window.copy[0].disabled = false
			window.download[0].disabled = false
			var audio = new Audio('http://fjdx.sc.chinaz.net/Files/DownLoad/sound1/201903/11250.wav');
			audio.play();
		});
		window.download.click(function(){
			function download(filename, data) {
				var blob = new Blob([data.replace(/([^\r])\n/g, "$1\r\n")], {type: 'text/plain'});
				if(window.navigator.msSaveOrOpenBlob) {
					window.navigator.msSaveBlob(blob, filename);
				}
				else{
					var elem = window.document.createElement('a');
					elem.href = window.URL.createObjectURL(blob);
					elem.download = filename;
					document.body.appendChild(elem);
					elem.click();
					document.body.removeChild(elem);
				}
			}
			let title = $('.introduce h1').text()
			download(title+'____.txt',window.passage[0].innerText)
		})
		function selectText(divid)
		{
			var div;
			if (document.selection)
			{
				div = document.body.createTextRange();
				div.moveToElementText(divid);
				div.select();
			}
			else
			{
				div = document.createRange();
				div.setStartBefore(divid);
				div.setEndAfter(divid) ;
				window.getSelection().removeAllRanges();
				window.getSelection().addRange(div);
			}
		}
		$('.main').append(window.loadInterval);
		$('.main').append(window.loadPassage);
		$('.main').append(window.download);
		$('.main').append(window.copy);
		$('.main').append(window.errs);
		$('.main').append(window.passage);
	};
	document.body.appendChild(script);
})();