Chapter Downloader Auto

Tự động tải tối đa 200 chương trên truyen.tangthuvien.vn

当前为 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.0
// @description  Tự động tải tối đa 200 chương trên truyen.tangthuvien.vn
// @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';

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

    const panel = document.createElement('div');
    panel.style.cssText = `
        position: fixed; top: 10%; right: 10px; z-index: 9999;
        width: 300px; background: white; border: 1px solid #ccc; padding: 10px;
    `;
    const startBtn = document.createElement('button');
    startBtn.textContent = isAutoDownloading ? 'Dừng' : 'Bắt đầu';
    startBtn.style.cssText = `width: 100%; margin-bottom: 10px;`;

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

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

    textarea.value = localStorage.getItem('dl_content') || '';

    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();
            return true;
        }
        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'); // Ghi lại trạng thái đang chạy
            localStorage.setItem('dl_count', downloadedCount.toString());
            setTimeout(() => location.href = next.href, 1000); // chuyển trang
            return true;
        }
        return false;
    }

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

        if (extractChapterContent()) {
            goToNextChapter(); // Tự reload trang
        } 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' : 'Bắt đầu';
        if (isAutoDownloading) autoDownloadLoop();
    });

    // Nếu reload trang và vẫn đang tải thì tiếp tục
    if (isAutoDownloading) {
        setTimeout(autoDownloadLoop, 1500); // đợi trang load xong
    }
})();