Greasy Fork

Greasy Fork is available in English.

起点小说vip自动换源

对起点的vip小说自动搜索笔趣阁,要设置滚动模式,自动加载形式,支持手机和网页

目前为 2021-03-23 提交的版本,查看 最新版本

// ==UserScript==
// @name         起点小说vip自动换源
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  对起点的vip小说自动搜索笔趣阁,要设置滚动模式,自动加载形式,支持手机和网页
// @author       JucyZhu
// @match        https://m.qidian.com/book/*
// @match        https://read.qidian.com/chapter/*
// @match        https://vipreader.qidian.com/chapter/*
// @require      https://cdn.bootcss.com/jquery/1.9.0/jquery.min.js
// @connect      www.xbiquge.la
// @connect      m.xbiquge.la
// @run-at      document-end
// @grant        GM_xmlhttpRequest
// ==/UserScript==
(function() {
	//'use strict';
	// Your code here...
	// 声明全局参数
	var data = {
			book: {},
			currentChapter: {},
			chapters: [],
			isApp: false
		},
		chapterNum = 0,
		first_time = true,
		lock = false,
		show = false,
		searchurl = "http://www.xbiquge.la/modules/article/waps.php";
	getBookChapterInfor();
	if ($(".j_subscribeBtn").length > 0 || $(".btn-primary").length > 0) {
		getBookContents()
	}
	$(window).scroll(function() {
		if (data.isApp) {
			show = ($(".read-article .read-section").last().offset().top - $(document).scrollTop() < $(
				window).height());
			} else {
			show = ($("#j_chapterBox .text-wrap:last").offset().top - $(document).scrollTop() < $(window)
				.height());
		}
		updata();
		if (($(window).scrollTop() * 1.05 > $(document).height() - $(window).height()) && show && lock) {
			lock = false;
			loadbook();
		}
	});
	//统一事件执行
	function updata() {
		//有没有到vipstate==0 &&(登录按钮,订阅按钮长度大于0(有.btn-primary)
		//或者.j_subscribeBtn vip章节全部订阅和订阅本章按钮长度大于0 
		//或者发现.w-all前往vip章节长度大于0)
		getBookContents()
		if (data.currentChapter.vipState == 0) {
			if ($(".btn-primary").length > 0) {
				//手机版				
				data.currentChapter.vipState = 1;
				data.currentChapter.chapterName = $(".read-article .read-section h3").last().text();			
				$(".btn-primary").remove();
				findLastChapter()	
				data.currentChapter.id = data.currentChapter.id + 1
			} else if ($(".j_subscribeBtn").length > 0 || $(".w-all").length > 0) {
				//网页版				
				data.currentChapter.vipState = 1;
				data.currentChapter.chapterName = $(".text-head:last h3 span:last").text()
				findLastChapter()				
				$(".w-all").remove();
				data.currentChapter.id = data.currentChapter.id + 1			
			}
			//findLastChapter()
		} else if (data.currentChapter.vipState == 1) {
			if (data.isApp) {
				chapterNum = $(".read-section").length
			} else {
				chapterNum = $(".text-wrap").length
			}
		}
	};

	function loadbook() {
		//updata();
		//更新data
		if (chapterNum > 3) {
			if (data.isApp) {
				$(".read-section").first().remove()
				chapterNum = $(".read-section").length
			} else {
				$(".text-wrap").first().remove()
				chapterNum = $(".text-wrap").length
			}
		}
		if (data.currentChapter.vipState == 1 && chapterNum <= 3) {
			insertCurrentVipContents();
			//console.log(data);
		}
	};

	function findLastChapter() {
		//遍历目录找到当前目录的url
		var chapter = "";
		var current_chapter = data.currentChapter.chapterName;
		if (data.chapters.length > 0) {
			console.log("------已获取《" + data.book.bookName + "》的目录------")
			for (var i = 0, len = data.chapters.length; i < len; i++) {
				chapter = data.chapters[i].text;
				if (current_chapter == chapter || current_chapter.indexOf(chapter) != -1 ||
					chapter
					.indexOf(current_chapter) != -1) {
					data.currentChapter.id = i;
					break;
				}
			}
		} else {
			console.log("章节目录没有解析到");
			first_time = true;
			//return getBookContents();
		}
	};

	function getBookChapterInfor() {
		if (typeof(g_data.book) == "undefined") {
			//说明是电脑网页端
			data.book.bookName = g_data.bookInfo.bookName;
			data.book.authorName = g_data.bookInfo.authorName;
			data.currentChapter.chapterName = $(".j_chapterName:last span:first").text();
			data.currentChapter.vipState = g_data.chapter.vipStatus;
			//data.currentChapter.content = [];
			data.currentChapter.id = 0;
			data.isApp = false;
		} else {
			//手机网页端
			data.book.bookName = g_data.book.bookName;
			data.book.authorName = g_data.book.authorName;
			data.currentChapter.chapterName = g_data.chapter.chapterName;
			data.currentChapter.vipState = g_data.chapter.vipStatus;
			//data.currentChapter.content = [];
			data.currentChapter.id = 0;
			data.isApp = true;
		}
		//data = tmp_data;
		//console.log(tmp_data);
		//return new Promise((resolve) => setTimeout(resolve, time));
		return data;
	};
	//根据章节名获取笔趣阁的目录列表,并赋值给data.chapters
	function getBookContents() {
		//从当前页面获取小说名称和当前章节		
		//getBookChapterInfor();
		//仅第一次运行
		if (first_time) {
			first_time = false;
			//console.log(data.book.booName);
			console.log("------开始加载VIP章节信息------");
			//根据bookname搜索书籍,返回response
			runAsync(searchurl, "POST", "searchkey=" + data.book.bookName).then((result) => {
				//根据搜索结果 解析出符合要求的书籍url
				//根据搜索结果解析小说网址				
				function parseURL(result) {
					//console.log(result.responseText);
					var responseText = result.responseText;
					// 解析笔趣阁标题和内容
					var $html = $('<div></div>').html(responseText);
					var $table = $("tbody tr", $html);
					//console.log($table.text())
					var title = "",
						author = "",
						href = "";
					//console.log(data);
					for (var i = 0, len = $table.length; i < len; i++) {
						title = $(".even a", $table[i]).text();
						author = $(".even:last", $table[i]).text();
						//console.log(title+" "+author)
						if (title == data.book.bookName && author == data.book.authorName) {
							href = $(".even a", $table[i]).attr("href");
							break;
						}
					}
					return runAsync(href, "GET", "")
				}
				// 根据小说网址请求获取小说章节信息
				return parseURL(result);
				//console.log(url);
			}).then((response) => {
				//根据请求结果response 解析 目录查找到适合当前及下一章的url,并请求获得promise结果
				function parseChaptersURL(response) {
					var responseText = response.responseText;
					//console.log(responseText);
					// 解析笔趣阁的小说目录
					var $html = $('<div></div>').html(responseText);
					data.chapters = $("#list dl a", $html);
					//初始化当前章节id序号					
					//遍历目录找到当前目录的url
					if(data.currentChapter.vipState == 1){
						findLastChapter();
						insertCurrentVipContents();
					}					
					return response
				};
				return parseChaptersURL(response);
			})
		}
	};

	//根据当前章节名称,如果是vip则从笔趣阁获取内容资源并插入
	function insertCurrentVipContents() {
		//根据找到的当前章节url获取说内容并解析
		console.log("------正在请求《" + data.currentChapter.chapterName + "》的内容------")
		var href = "http://www.xbiquge.la" + $(data.chapters[data.currentChapter.id]).attr("href")
		GM_xmlhttpRequest({
			method: "GET",
			url: href,
			headers: {
				"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
			},
			data: "",
			onload: function(response) {
				//console.log("请求成功");		
				function parseChaptersContent(response) {
					var responseText = response.responseText;
					// 解析笔趣阁的小说目录
					var $html = $('<div></div>').html(responseText);
					var contents = $("#content", $html).text().replace($(
							"#content p", $html)
						.text(), "").split("\n\n");
					//插入网页	
					insertChapter(contents);
				}
				parseChaptersContent(response);
			},
			onerror: function(response) {
				console.log("请求失败: href: " + href);
			}
		})
	};

	// 插入文章
	function insertChapter(contents) {
		var section, paragraph, p = "",
			i = 0,
			mlen = -1;
		//contents = data.currentChapter.content;
		//console.log("------开始插入" + data.currentChapter.chapterName + "------")
		//console.log(data)
		if (data.isApp) {
			//console.log("------开始加载下一章1------");	
			//手机中插入
			chapterNum = $(".read-section").length
			section = $(".read-article .read-section").last();
			//$(".btn-primary", section).remove();
			$(".read-article").append(section.clone());
			section = $(".read-article .read-section").last();
			paragraph = $("p", section).first();
			$(".btn-primary", section).remove();
			$("p", section).remove();
			$("h3", section).text(data.currentChapter.chapterName);
			for (i = 0, mlen = contents.length; i < mlen; i++) {
				p = contents[i];
				paragraph.text(p);
				section.append(paragraph.clone());
			}
			//$(".read-article .read-section").eq(-2).remove();
		} else {
			chapterNum = $(".text-wrap").length
			//web中插入			
			section = $("#j_chapterBox .text-wrap:last");
			$("#j_chapterBox").append(section.clone());
			section = $("#j_chapterBox .text-wrap:last");
			//$("#j_chapterBox .text-wrap").eq(-2).remove()
			//section = $("#j_chapterBox .text-wrap:last");			
			paragraph = $(".read-content p:first", section);
			$(".vip-limit-wrap", section).remove();
			$(".admire-wrap", section).remove();
			$(".read-content p", section).remove();
			$("h3:first span:first", section).text(data.currentChapter.chapterName);
			for (i = 0, mlen = contents.length; i < mlen; i++) {
				p = contents[i];
				paragraph.text(p);
				$(".read-content", section).append(paragraph.clone());
			}

		}
		data.currentChapter.id = data.currentChapter.id + 1;
		data.currentChapter.chapterName = data.chapters[data.currentChapter.id].text;
		console.log("------已经插入<<" + data.chapters[data.currentChapter.id - 1].text + ">>------")
		lock = true;
	};

	function runAsync(url, send_type, data_ry) {
		console.log("请求数据: url: " + url + " method: " + send_type + " data: " + data_ry);
		var p = new Promise((resolve, reject) => {
			GM_xmlhttpRequest({
				method: send_type,
				url: url,
				headers: {
					"Content-Type": "application/x-www-form-urlencoded;charset=utf-8"
				},
				data: data_ry,
				onload: function(response) {
					//console.log("请求成功");
					//console.log(response.responseText);
					//resolve(response.responseXML);
					resolve(response);
				},
				onerror: function(response) {
					console.log("请求失败: url: " + url + " method: " + send_type +
						" data: " +
						data_ry);
					reject("请求失败: url = " + url);
				}
			});
		})
		return p;
	};
	//截取str中startStr和endStr之间的元素
	function stringBetween(str, startStr, endStr) {
		var start = str.indexOf(startStr),
			end = str.indexOf(endStr);
		console.log("start = " + start + ";end = " + end)
		var doc = str.substring(start, end) + endStr;
		return doc;
	}

})();