// ==UserScript== // @name wanbentxt // @namespace http://tampermonkey.net/ // @version 0.1 // @description https://m.wanbentxt.com/ 的小说下载 // @author You // @match https://m.wanbentxt.com/* // @require http://libs.baidu.com/jquery/2.0.0/jquery.min.js // @require https://cdn.jsdelivr.net/npm/file-saver@1.3.8/FileSaver.min.js // @grant GM_xmlhttpRequest // @downloadURL none // ==/UserScript== (function () { 'use strict'; var DivInited = false; var WCContent, WCWords, WCQuit, WCSave, WCContinue; var pagenum = 0, chapternum = 0; var title, Id, authior, desc; var listpage = []; var oldurl = 'null'; function initDiv() { console.log("initDiv"); if (DivInited) return; DivInited = true; var content = document.createElement("div"); document.body.appendChild(content); content.outerHTML = `
保存
取消
繼續
`; WCContent = document.querySelector("#CWDownContent"); WCWords = document.querySelector("#CWDownWords"); WCQuit = document.querySelector("#CWDownQuit"); WCSave = document.querySelector("#CWDownSave"); WCContinue = document.querySelector("#CWCContinue"); WCContinue.style.display = "none"; WCQuit.onclick = function () { DivInited = false; WCContent.style.display = "none"; WCWords.innerHTML = ''; WCContent.parentNode.removeChild(WCContent); }; WCContinue.onclick = function () { }; WCSave.onclick = function () { SaveText(); }; } function ShowWords(value) { WCWords.innerHTML = (title ? title + '
' : '') + value; } function inits() { var content = document.createElement("div"); document.body.appendChild(content); content.outerHTML = `
下载
`; var initsSave = document.querySelector("#initsSave"); initsSave.onclick = function () { initDiv(); run(); } } function getElementRootText(element) { let ret = ""; for (const i of element.childNodes) { if (i.nodeType === i.TEXT_NODE || i.nodeName == 'T') { ret += i.nodeValue + '\r\n'; } } return ret.replace(/^\s+|\s+$/g, ""); } async function gethtml(url) { return new Promise((resolve, reject) => { GM_xmlhttpRequest( { url: url, method: "GET", onload: function (response) { resolve(response.responseText); } }); }); } async function getpage(pageurl) { if (!pageurl) return; pagenum++; let pagestr = await gethtml(pageurl); let pagedoc = $(''); pagedoc.html(pagestr); var lista = pagedoc.find('.chapterList > ul>a'); console.log(lista); for (let i = 0; i < lista.length; i++) { chapternum++; ShowWords(`正在下载
第${pagenum}页
第${chapternum}章节`) let url = lista[i].href; console.log(url); let str = await gethtml(url); let doc = $(''); doc.html(str); let title = doc.find('.readerTop h3').text(); let content = doc.find('.raderCon'); //console.log(content); /* let listtxt = []; for (const i of doc[0].querySelectorAll(".raderCon")) { let line = getElementRootText(i); console.log(line); listtxt.push(line); } listpage.push({ title: title, content: listtxt.join('\r\n') }) */ let value = content.html(); //console.log(value); value = value.replace(/
/g, "\r\n"); value = value.replace(/一秒记住.*?找不到书请留言!/, ''); value = value.replace(/一秒记住.*?m.wanbentxt.com/, ''); value = value.replace(/支持.*?找不到书请留言!/, ''); value = value.replace(/支持.*?你们的宣传支持就是最的动力/, ''); value = value.replace(/支持.*?你们的支持就是大大的动力!/, ''); value = value.replace(/支持.*?分享给你们的好友!/, ''); value = value.replace(/【完本神站】.*?永不丢失!/, ''); value = value.replace('更多绝版完结好文搜索', ''); value = value.replace('书荒就搜', ''); value = value.replace(/&/g, "&"); value = value.replace(/</g, "<"); value = value.replace(/>/g, ">"); value = value.replace(/ /g, " "); value = value.replace(/"/g, "'"); value = value.replace(/<[^<>]+?>/g, ''); value = value.replace(/完[\s\S]{0,8}本[\s\S]{0,8}神[\s\S]{0,8}站/, ''); //console.log(value); listpage.push({ title: title, content: value }); } var next = pagedoc.find('.page > a:nth-child(3)'); if (next.length > 0) { let nexturl = 'https://m.wanbentxt.com' + next.attr('href'); if (oldurl != nexturl) { oldurl = nexturl; console.log(nexturl); await getpage(nexturl); } else { console.log('end'); console.log(listpage); } } } async function gettitle() { var m = /m\.wanbentxt\.com\/(\w+)/.exec(location.href); console.log(m); if (m) { Id = m[1]; let Idurl = 'https://m.wanbentxt.com/' + Id + '/'; let Idstr = await gethtml(Idurl); let Iddoc = $(''); Iddoc.html(Idstr); desc = Iddoc.find('.bookInfo').text(); authior = Iddoc.find('.bookPhrMid p:nth-child(1)').text(); title = Iddoc.find('.bookPhr h2').text(); return 'https://m.wanbentxt.com/' + Id + '/all.html' } return null; } async function run() { let reurl = await gettitle(); await getpage(reurl); console.log('end'); SaveText(); } function SaveText() { if (!listpage || listpage.length == 0) return; let save = []; $.each(listpage, function (index, value) { save.push(value.title + '\r\n' + value.content + '\r\n\r\n'); }); ShowWords(`已下载完成
共${pagenum}页
共${chapternum}章节`) var blob = new Blob([title, "\r\n", authior, "\r\n", desc, "\r\n\r\n", save.join('\r\n')], { type: "text/plain;charset=utf-8" }); saveAs(blob, title + '.txt'); } inits(); })();