Chapter Downloader Auto

Tự động tải tối đa 200 chương trên truyen.tangthuvien.vn với giao diện hiển thị rõ tiến trình

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Chapter Downloader Auto
// @namespace    http://tampermonkey.net/
// @version      3.1
// @description  Tự động tải tối đa 200 chương trên truyen.tangthuvien.vn với giao diện hiển thị rõ tiến trình
// @match        https://truyen.tangthuvien.vn/doc-truyen/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    const MAX_CHAPTERS = 200;
    let downloadedCount = parseInt(localStorage.getItem('dl_count') || '0');
    let isAutoDownloading = localStorage.getItem('dl_active') === 'true';

    // UI Elements
    const panel = document.createElement('div');
    const textarea = document.createElement('textarea');
    const startBtn = document.createElement('button');
    const statusDiv = document.createElement('div');

    // Setup UI
    panel.style.cssText = `
        position: fixed; top: 10%; right: 10px; z-index: 9999;
        width: 320px; background: #fff; border: 1px solid #ccc; padding: 10px;
        font-family: Arial, sans-serif; font-size: 14px;
    `;
    textarea.style.cssText = `width: 100%; height: 250px; margin-bottom: 10px;`;
    startBtn.style.cssText = `width: 100%; margin-bottom: 10px; padding: 10px;`;
    statusDiv.style.cssText = `margin-bottom: 10px; color: #333; font-weight: bold;`;

    startBtn.textContent = isAutoDownloading ? `⏸ Dừng (${downloadedCount}/${MAX_CHAPTERS})` : '▶️ Bắt đầu';

    panel.appendChild(textarea);
    panel.appendChild(startBtn);
    panel.appendChild(statusDiv);
    document.body.appendChild(panel);

    // Load previous content
    textarea.value = localStorage.getItem('dl_content') || '';

    function saveContent() {
        localStorage.setItem('dl_content', textarea.value);
        localStorage.setItem('dl_count', downloadedCount.toString());
    }

    function setStatus(msg) {
        statusDiv.textContent = msg;
    }

    function extractChapterContent() {
        const title = document.querySelector('h2')?.innerText.trim();
        const content = document.querySelector('.box-chap')?.innerText.trim();
        if (title && content) {
            textarea.value += `\n\n------------\n\n${title}\n\n${content}`;
            downloadedCount++;
            saveContent();
            setStatus(`✅ Đã lấy: ${title}`);
            return true;
        } else {
            setStatus('❌ Không thể lấy nội dung chương.');
            return false;
        }
    }

    function goToNextChapter() {
        const next = document.querySelector('.bot-next_chap.bot-control') ||
                     [...document.querySelectorAll('a')].find(a => /tiếp|next/i.test(a.textContent));
        if (next && next.href) {
            localStorage.setItem('dl_active', 'true');
            localStorage.setItem('dl_count', downloadedCount.toString());
            setStatus('➡️ Đang chuyển sang chương tiếp...');
            setTimeout(() => location.href = next.href, 1500);
            return true;
        } else {
            setStatus('❌ Không tìm thấy nút chương tiếp theo.');
            return false;
        }
    }

    async function autoDownloadLoop() {
        if (downloadedCount >= MAX_CHAPTERS) {
            isAutoDownloading = false;
            localStorage.setItem('dl_active', 'false');
            startBtn.textContent = '▶️ Bắt đầu';
            setStatus(`🏁 Đã tải xong ${MAX_CHAPTERS} chương.`);
            return;
        }

        if (extractChapterContent()) {
            startBtn.textContent = `⏸ Dừng (${downloadedCount}/${MAX_CHAPTERS})`;
            goToNextChapter(); // Sẽ reload lại trang và tiếp tục
        } else {
            isAutoDownloading = false;
            localStorage.setItem('dl_active', 'false');
            startBtn.textContent = '▶️ Bắt đầu';
        }
    }

    startBtn.addEventListener('click', () => {
        isAutoDownloading = !isAutoDownloading;
        localStorage.setItem('dl_active', isAutoDownloading ? 'true' : 'false');
        startBtn.textContent = isAutoDownloading ? `⏸ Dừng (${downloadedCount}/${MAX_CHAPTERS})` : '▶️ Bắt đầu';
        if (isAutoDownloading) autoDownloadLoop();
    });

    if (isAutoDownloading) {
        setStatus('🔁 Tiếp tục tải chương...');
        setTimeout(autoDownloadLoop, 1500);
    } else {
        setStatus('💤 Đang chờ bắt đầu...');
    }
})();