Tải toàn bộ chương Tangthuvien

Tự động tải tất cả chương từ trang danh sách chương của truyện trên Tangthuvien

当前为 2025-04-21 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Tải toàn bộ chương Tangthuvien
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Tự động tải tất cả chương từ trang danh sách chương của truyện trên Tangthuvien
// @match        https://truyen.tangthuvien.vn/doc-truyen/*
// @grant        GM_xmlhttpRequest
// @connect      truyen.tangthuvien.vn
// ==/UserScript==

(function () {
    'use strict';

    if (!location.href.includes('/doc-truyen/')) return;

    const textarea = document.createElement('textarea');
    textarea.style.cssText = 'width:100%; height:300px; margin-bottom:10px; white-space: pre-wrap;';

    const container = document.createElement('div');
    container.style.cssText = `
        position: fixed; top: 10%; right: 10px; z-index: 9999;
        width: 400px; background: white; border: 1px solid #ccc;
        padding: 10px; box-shadow: 0 0 10px rgba(0,0,0,0.3);
    `;

    const startBtn = document.createElement('button');
    startBtn.textContent = 'Bắt đầu tải';
    startBtn.style.cssText = 'width: 100%; padding: 8px; margin-bottom: 10px;';

    const copyBtn = document.createElement('button');
    copyBtn.textContent = 'Sao chép';
    copyBtn.style.cssText = 'width: 100%; padding: 8px;';

    container.appendChild(textarea);
    container.appendChild(startBtn);
    container.appendChild(copyBtn);
    document.body.appendChild(container);

    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    function fetchChapter(url) {
        return new Promise((resolve) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: url,
                onload: function (response) {
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(response.responseText, 'text/html');
                    const title = doc.querySelector('h2')?.innerText.trim() || 'Không tiêu đề';
                    const content = doc.querySelector('.box-chap')?.innerText.trim() || 'Không có nội dung';
                    resolve(`\n\n------------\n\n${title}\n\n${content}`);
                },
                onerror: () => resolve(`\n\n[Không tải được chương: ${url}]`)
            });
        });
    }

    startBtn.addEventListener('click', async () => {
        startBtn.disabled = true;
        startBtn.textContent = 'Đang tải...';

        const links = [...document.querySelectorAll('a')]
            .filter(a => a.href.includes('/chuong-') && a.href.includes(location.pathname))
            .sort((a, b) => {
                const n1 = parseInt(a.textContent.match(/\d+/)?.[0] || 0);
                const n2 = parseInt(b.textContent.match(/\d+/)?.[0] || 0);
                return n1 - n2;
            });

        if (links.length === 0) {
            textarea.value = 'Không tìm thấy danh sách chương!';
            startBtn.disabled = false;
            startBtn.textContent = 'Bắt đầu tải';
            return;
        }

        for (const link of links) {
            const url = link.href;
            textarea.value += `\nĐang tải: ${url}`;
            const data = await fetchChapter(url);
            textarea.value += data;
            await sleep(1500); // nghỉ 1.5s giữa mỗi chương để tránh bị chặn
        }

        startBtn.textContent = 'Đã xong!';
        startBtn.disabled = false;
    });

    copyBtn.addEventListener('click', () => {
        textarea.select();
        document.execCommand('copy');
        alert('Đã sao chép nội dung vào clipboard!');
    });
})();