吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3042|回复: 35
收起左侧

[其他原创] 【油猴脚本】 某电子书网站,电子书打包下载脚本

  [复制链接]
bh4ger 发表于 2024-1-31 10:45
脚本用途:合并一部电子书多个章节为一个html文件 ,并清理网页中的html代码, 生成的html 适合转换为 kindle 的 mobi 等格式
使用方法:打开电子书的章节索引页面,会自动生成一个 “点击下载电子书”的链接,点击即可下载。

避免侵权,网站名称已隐藏。 代码仅供参考
[JavaScript] 纯文本查看 复制代码
// ==UserScript==
// [url=home.php?mod=space&uid=170990]@name[/url]         **文学电子书下载
// [url=home.php?mod=space&uid=467642]@namespace[/url]    http://tampermonkey.net/
// [url=home.php?mod=space&uid=1248337]@version[/url]      2024-01-31
// @description  打包下载**文学电子书下载!
// [url=home.php?mod=space&uid=686208]@AuThor[/url]       You
// [url=home.php?mod=space&uid=195849]@match[/url]        https://www.*ing*ue*oke.com/*/
// [url=home.php?mod=space&uid=593100]@Icon[/url]         https://www.*ing*ue*oke.com/favicon.ico
// [url=home.php?mod=space&uid=609072]@grant[/url]        none
// ==/UserScript==

(function() {
    'use strict';
    var contentList = document.getElementById('content-list')
    if(contentList === null){
        return;
    }
    var bookDescribe = contentList.querySelector('.book-describe').innerText;
    var metas = bookDescribe.split('\n\n');
    var title = metas[0];
    var author = metas[1].split(':')[1];
    var guojia = metas[2].split(':')[1];
    var niandai = metas[3].split(':')[1];
    var description= metas[5];
    var booklist = document.querySelector('.book-list');
    var sections = [];
    var tasks = [];
    booklist.querySelectorAll('a').forEach(function(a,i){
        sections.push({title:a.title,content:i});
        var t = fetch(a.href).then(function(response){
            return response.text()}
                                  ).then(function (text) {
            var div = document.createElement('div');
            div.innerHTML = text;
            var nr1=div.querySelector('#nr1 div');
            sections[i].content = nr1.innerHTML;
        });
        tasks.push(t);
    });
    Promise.all(tasks).then(function(){
        var html = ['<!DOCTYPE html>',
                    '<html lang="zh-CN">',
                    '<head>',
                    '<meta charset="utf-8">',
                    '<meta name="author" content="' + author + '">',
                    '<meta name="description" content="'+ description +'">',
                    '<title>' + title + '</title>',
                    '</head>',
                    '<body>',
                    '<article>',
                    '<h1>' + title + ' <small> '+ author +'</small></h1>'];
        sections.forEach(function(section){
            html.push('<section>');
            html.push('<h2>' + section.title + '</h2>');
            html.push(section.content);
            html.push('</section>');
        });
        html.push('</article>');
        html.push('</body>');
        html.push('</html>');
        contentList.querySelector('.book-describe').innerHTML = '<p><a id="download">点击下载电子书</a></p>' + contentList.querySelector('.book-describe').innerHTML;
        var a = document.getElementById('download');
        a.href = 'data:text/plain;charset=utf-8,'+html.join('\n');
        a.download = title + '.html';
    });
})();

免费评分

参与人数 10吾爱币 +13 热心值 +10 收起 理由
soughing + 1 + 1 我很赞同!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
sun51000 + 1 + 1 谢谢@Thanks!
hwh425 + 1 用心讨论,共获提升!
塞北的雪 + 1 + 1 文章里有#符号会被认为是URL的hash符号,导致内容被截取
china-ray + 1 + 1 谢谢@Thanks!
wsldboy + 1 + 1 我很赞同!
hanwuj2012 + 1 + 1 我很赞同!
ll090822 + 1 + 1 热心回复!
DouhiroKwok + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| bh4ger 发表于 2024-1-31 11:44


// ==UserScript==
// @name         星月文学电子书下载
// @namespace    http://tampermonkey.net/
// @version      2024-01-31
// @description  打包下载星月文学电子书下载!
// @author       You
// @match        https://www.xingyueboke.com/*/
// @icon         https://www.xingyueboke.com/favicon.ico
// @grant        none
// ==/UserScript==

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
helh0275 + 1 + 1 谢谢@Thanks!

查看全部评分

塞北的雪 发表于 2024-1-31 18:30
本帖最后由 塞北的雪 于 2024-1-31 19:05 编辑

狗尾续貂,改进了样式和下载过大文件可能下载不全的问题。

[JavaScript] 纯文本查看 复制代码
// ==UserScript==
// @name         星月文学电子书下载
// @namespace    https://www.52pojie.cn/thread-1886476-1-1.html
// @version      2024-01-31
// @description  打包下载星月文学电子书下载!
// @author       bh4ger
// @match        https://www.xingyueboke.com/*/
// @icon         https://www.xingyueboke.com/favicon.ico
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    var contentList = document.getElementById('content-list')
    if(contentList === null){
        return;
    }
    var bookDescribe = contentList.querySelector('.book-describe').innerText;
    var metas = bookDescribe.split('\n\n');
    var title = metas[0];
    var author = metas[1].split(':')[1];
    var guojia = metas[2].split(':')[1];
    var niandai = metas[3].split(':')[1];
    var description= metas[5];
    var booklist = document.querySelector('.book-list');
    var sections = [];
    var tasks = [];
    booklist.querySelectorAll('a').forEach(function(a,i){
        sections.push({title:a.title,content:i});
        var t = fetch(a.href)
        .then(function(response){
            return response.text()
        })
        .then(function (text) {
            var div = document.createElement('div');
            div.innerHTML = text;
            var nr1=div.querySelector('#nr1 div');
            sections[i].content = nr1.innerHTML;
        });
        tasks.push(t);
    });
    Promise.all(tasks).then(function(){
        var html = ['<!DOCTYPE html>',
                    '<html lang="zh-CN">',
                    '<head>',
                    '<meta charset="utf-8">',
                    '<meta name="author" content="' + author + '">',
                    '<meta name="description" content="'+ description +'">',
                    '<title>' + title + '</title>',
                    '</head>',
                    '<body>',
                    '<article>',
                    '<h1>' + title + ' <small> '+ author +'</small></h1>'];
        sections.forEach(function(section){
            html.push('<section>');
            html.push('<h2>' + section.title + '</h2>');
            html.push(section.content);
            html.push('</section>');
        });
        html.push('</article>');
        html.push('</body>');
        html.push('</html>');
        contentList.querySelector('.book-describe').innerHTML = '<div class="scrolltobt"><a id="download">下载电子书</a><br /></br /><a id="singlePage">单页浏览</a></div>' + contentList.querySelector('.book-describe').innerHTML;
        let a = document.getElementById('download');
        a.href = 'data:text/plain;charset=utf-8,'+html.join('\n').replaceAll('#','@');
        a.download = title + '.html';
        let b=document.getElementById('singlePage');
        b.href='javascript:void(0);';
        b.addEventListener("click",function(){
            let win=window.open("about:blank");
            win.document.write(html.join('\n'));
        });

    });})();
sundog2008 发表于 2024-1-31 10:59
jjoobb123 发表于 2024-1-31 11:07
谢谢分享!
schoolclub 发表于 2024-1-31 11:10
能给点提示说下是哪家的吗?
sai609 发表于 2024-1-31 11:22
哪家呢,我很像知道,否则没有办法下载
风中 发表于 2024-1-31 11:24
有没有提示,我也想知道
user1997 发表于 2024-1-31 12:10
感谢分享
DouhiroKwok 发表于 2024-1-31 12:32
对于电子书一党确实是一个福利脚本
fuming2023 发表于 2024-1-31 12:57
试过了,确实挺厉害的,可以下载使用。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-27 15:01

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表