wanbentxt

https://m.wanbentxt.com/ 的小说下载

目前為 2019-08-15 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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/[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();
            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(/<br>/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(/&amp;/g, "&");
            value = value.replace(/&lt;/g, "<");
            value = value.replace(/&gt;/g, ">");
            value = value.replace(/&nbsp;/g, " ");
            value = value.replace(/&quot/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 = $('<html></html>');
            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(`已下载完成<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();



})();