Greasy Fork

Greasy Fork is available in English.

中少快乐阅读平台中少报刊资源下载器

自动下载中少快乐阅读平台下的中少报刊资源,支持提取报刊分类、子分类、年份、月份和编号信息,批量下载报刊内容并打包成 ZIP 文件

当前为 2025-05-08 提交的版本,查看 最新版本

// ==UserScript==
// @name         中少快乐阅读平台中少报刊资源下载器
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  自动下载中少快乐阅读平台下的中少报刊资源,支持提取报刊分类、子分类、年份、月份和编号信息,批量下载报刊内容并打包成 ZIP 文件
// @author       野原新之布
// @license      GPL-3.0-only
// @match        http://202.96.31.36:8888/Reading/Show/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=31.36
// @grant        GM_xmlhttpRequest
// @grant        GM_download
// @grant        GM_log
// @run-at       document-idle
// ==/UserScript==

// 加载 JSZip 库
(function() {
    'use strict';

    // 引入 JSZip 库
    const script = document.createElement('script');
    script.src = 'https://cdnjs.cloudflare.com/ajax/libs/jszip/3.7.1/jszip.min.js';
    document.head.appendChild(script);

    script.onload = function() {
        GM_log("JSZip loaded successfully");

        // 从页面源码中提取window.location.href的值
        let pageLocation = '';

        // 查找包含window.location.href的<script>标签
        const scripts = document.getElementsByTagName('script');
        Array.from(scripts).forEach(function(script) {
            if (script.innerHTML.includes("window.location.href =")) {
                const regex = /window\.location\.href = '(\/fliphtml5\/[^']+)'/;
                const match = script.innerHTML.match(regex);
                if (match) {
                    pageLocation = match[1];  // 提取出URL
                }
            }
        });

        if (pageLocation) {
            GM_log("从页面源码中提取到 URL: " + pageLocation);

            // 更新的正则表达式,提取期刊分类、子分类、年份、月份和编号
            const locationPattern = /\/fliphtml5\/password\/main\/([^\/]+)\/([^\/]+)\/(\d{4})\/(\d{2})\/(\d+)\//;
            const match = pageLocation.match(locationPattern);

            if (match) {
                const categoryPrefix = match[1];  // qikan
                const subCategory = match[2];     // yehb
                const year = match[3];            // 2021
                const month = match[4];           // 12
                const number = match[5];          // 880

                GM_log("categoryPrefix: ", categoryPrefix);
                GM_log("subCategory: ", subCategory);
                GM_log("year: ", year);
                GM_log("month: ", month);
                GM_log("number: ", number);

                // 根据提取的参数构建 URL
                const xmlUrl = `http://202.96.31.36:8888/fliphtml5/password/main/${categoryPrefix}/${subCategory}/${year}/${month}/${number}/web/html5/tablet/${subCategory}${number}SL.xml`;
                GM_log("xmlUrl: ", xmlUrl);

                // 页面加载后直接进行 XML 下载
                window.addEventListener('load', function() {
                    fetchXmlData(xmlUrl, number, categoryPrefix, subCategory, year, month);
                });

                // 获取 XML 数据并解析
                function fetchXmlData(url, number, categoryPrefix, subCategory, year, month) {
                    console.log('开始请求 XML');
                    GM_xmlhttpRequest({
                        method: 'GET',
                        url: url,
                        onload: function(response) {
                            const parser = new DOMParser();
                            const xmlDoc = parser.parseFromString(response.responseText, "text/xml");

                            // 获取所有的 item 元素
                            const items = xmlDoc.getElementsByTagName('item');

                            // 创建 ZIP 文件
                            const zip = new JSZip();
                            const downloadedFiles = [];
                            // 计数器,确保所有文件都下载完
                            let downloadedCount = 0;

                            Array.from(items).forEach(function(item) {
                                const href = item.getAttribute('href');

                                // 如果 href 存在,进行下载
                                if (href) {
                                    const downloadUrl = `http://202.96.31.36:8888/fliphtml5/password/main/${categoryPrefix}/${subCategory}/${year}/${month}/${number}/web/html5/tablet/${href}`;

                                    // 使用 GM_xmlhttpRequest 下载文件,并将其添加到 ZIP 文件中
                                    GM_xmlhttpRequest({
                                        method: 'GET',
                                        url: downloadUrl,
                                        responseType: 'arraybuffer', // 获取文件内容
                                        onload: function(downloadResponse) {
                                            if (downloadResponse.status === 200) {
                                                const fileName = href.split('/').pop();
                                                zip.file(fileName, downloadResponse.response); // 将文件添加到 zip
                                                downloadedFiles.push(fileName); // 保存文件名
                                                downloadedCount++;

                                                console.log(`添加文件: ${fileName}`);

                                                // 如果所有文件都下载完,生成并下载 ZIP 文件
                                                if (downloadedCount === items.length) {
                                                    const zipName = `${year}${month}-${categoryPrefix}.zip`;

                                                    zip.generateAsync({ type: "blob" }).then(function(content) {
                                                        GM_download({
                                                            url: URL.createObjectURL(content),
                                                            name: zipName,
                                                            onload: function() {
                                                                console.log(`ZIP 文件生成成功: ${zipName}`);
                                                            },
                                                            onerror: function(error) {
                                                                console.error('ZIP 文件下载失败:', error);
                                                            }
                                                        });
                                                    });
                                                }
                                            }
                                        },
                                        onerror: function(error) {
                                            console.error('下载失败:', error);
                                        }
                                    });
                                }
                            });
                        },
                        onerror: function(error) {
                            console.error('XML 请求失败:', error);
                        }
                    });
                }
            } else {
                GM_log("未从页面 URL 中提取到有效的期刊、分类、年份、月份、编号信息");
            }
        } else {
            GM_log("未从页面源码中提取到 window.location.href 的值");
        }
    };
})();