// ==UserScript==
// @name wanbentxt
// @namespace http://tampermonkey.net/
// @version 0.3
// @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/[email protected]/FileSaver.min.js
// @grant GM_xmlhttpRequest
// ==/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 = `
<div id="CWDownContent" >
<div style="width:360px;height:100px;position:fixed;left:50%;top:50%;margin-top:-50px;margin-left:-180px;z-index:100000;background-color:#ffffff;border:1px solid #afb3b6;opacity:0.95;filter:alpha(opacity=95);box-shadow:5px 5px 20px 0px#000;">
<div id="CWDownWords" style="font-size:12px;position:absolute;width:290px;height:90px;padding: 8px;border-radius: 10px;float: left;">
</div>
<div style="float: right;">
<div id="CWDownSave" style="width:43px;height:26px;cursor: pointer;background-color:#3169da;margin: 5px 5px 3px 3px;">
<span style="line-height:25px;display:block;color:#FFF;text-align:center;font-size:14px;">保存</span>
</div>
<div id="CWDownQuit" style="width:43px;height:26px;cursor: pointer;background-color:#3169da;margin: 3px;">
<span style="line-height:25px;display:block;color:#FFF;text-align:center;font-size:14px;">取消</span>
</div>
<div id="CWCContinue" style="width:43px;height:26px;cursor: pointer;background-color:#3169da;margin: 3px;">
<span style="line-height:25px;display:block;color:#FFF;text-align:center;font-size:14px;">繼續</span>
</div>
</div>
</div>
</div>
`;
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 + '<br>' : '') + value;
}
function inits() {
var content = document.createElement("div");
document.body.appendChild(content);
content.outerHTML = `
<div id="initsContent">
<div style="width:40px;height:25px;position:fixed;left:10PX;top:10PX;z-index:100000;/*! background-color:#ffffff; *//*! border:1px solid #afb3b6; *//*! opacity:0.95; */filter:alpha(opacity=95);">
<div id="initsSave" style="/*! width:43px; *//*! height:26px; */cursor: pointer;background-color:#3169da;/*! margin: 2px 5px 3px 10px; */">
<span style="line-height:25px;display:block;color:#FFF;text-align:center;font-size: 10px;">下载</span>
</div>
</div>
</div>
`;
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 = $('<html></html>');
pagedoc.html(pagestr);
var lista = pagedoc.find('.chapterList > ul>a');
console.log(lista);
for (let i = 0; i < lista.length; i++) {
chapternum++;
ShowWords(`正在下载<br>第${pagenum}页<br>第${chapternum}章节`)
let url = lista[i].href;
console.log(url);
let str = await gethtml(url);
let doc = $('<html></html>');
doc.html(str);
let title = doc.find('.readerTop h3').text();
if (!title) {
title = $(lista[i]).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 = textchange(value);
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);
}
}
}
function textchange(value) {
value = value.replace(/<br>/g, "\r\n");
var onenum = value.indexOf("\r\n")
var onerow = value.substring(0, onenum);
console.log(onerow);
console.log(onerow.length);
while (!onerow) {
onenum = value.indexOf("\r\n", onenum)
onerow = value.substring(0, onenum);
}
if (onerow) {
if (onerow.indexOf('wanbentxt') > 0 || onerow.indexOf('完本神站') > 0|| /完[\s\S]{0,12}本[\s\S]{0,12}神[\s\S]{0,12}站/.test(onerow))
value = value.substring(onenum);
}
var lastnum = value.lastIndexOf("\r\n")
var lastrow = value.substring(lastnum);
console.log(lastrow);
console.log(lastrow.length);
if (lastrow) {
if (lastrow.indexOf('wanbentxt') > 0 || lastrow.indexOf('完本神站') > 0 || /完[\s\S]{0,12}本[\s\S]{0,12}神[\s\S]{0,12}站/.test(lastrow))
value = value.substring(0,lastnum);
}
console.log(value);
return;
value = value.replace('\^完^本^神^站^\分类精确、支持神站请多多分享给你的小伙伴吧!', '');
value = value.replace(/一秒记住.*?wanbentxt\.com/i, '');
value = value.replace(/一秒记住.*?Wanbentxt\.coΜ/i, '');
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(/【完本神站】.*?永不丢失!?/g, '');
value = value.replace(/【完本神站】.*?想看的书!?/g, '');
value = value.replace(/喜欢神站.*?书友们!?/, '');
value = value.replace(/喜欢神站.*?朋友!?/, '');
value = value.replace(/更多[\u4e00-\u9fa5].?好书[\u4e00-\u9fa5].?/g, '');
value = value.replace(/[\u4e00-\u9fa5].?入娄约木货是复读机[\u4e00-\u9fa5].?/g, '');
value = value.replace('书荒就搜', '');
value = value.replace('看好书就搜', '');
value = value.replace(/[\u4e00-\u9fa5]?本神站[\u4e00-\u9fa5]?/, '');
value = value.replace(/www.Wanbentxt.com/ig, '');
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,12}本[\s\S]{0,12}神[\s\S]{0,12}站)?/, '');
value = value.replace("\r\n\r\n", "\r\n");
return value;
}
async function gettitle() {
var m = /m\.wanbentxt\.com\/(\d+)/.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 = $('<html></html>');
Iddoc.html(Idstr);
desc = Iddoc.find('.bookInfo').text();
if (!desc)
desc = desc.replace(/(?www.wanbentxt.com\)?/, '');
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 t = /m\.wanbentxt\.com\/(\d+)\/(\d+)/.exec(location.href);
console.log(t);
if (!t || t.length == 0) {
console.log('null');
let reurl = await gettitle();
await getpage(reurl);
console.log('end');
SaveText();
}
else {
console.log('not null');
let content = $('.raderCon');
let value = content.html();
console.log(value);
value = textchange(value);
console.log(value);
}
}
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(`已下载完成<br>共${pagenum}页<br>共${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();
})();