Chapter Downloader Auto

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

目前為 2025-04-21 提交的版本,檢視 最新版本

// ==UserScript==
// @name         Chapter Downloader Auto
// @namespace    http://tampermonkey.net/
// @version      3.2
// @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 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;
        box-shadow: 0 0 8px rgba(0,0,0,0.2); font-family: Arial;
    `;

    const textarea = document.createElement('textarea');
    textarea.style.cssText = `width: 100%; height: 300px; margin-bottom: 10px;`;
    textarea.value = localStorage.getItem('dl_content') || '';
    panel.appendChild(textarea);

    const startBtn = document.createElement('button');
    startBtn.textContent = isAutoDownloading ? 'Dừng' : 'Bắt đầu';
    startBtn.style.cssText = `width: 100%; margin-bottom: 10px; background: ${isAutoDownloading ? '#f44336' : '#4CAF50'}; color: white; border: none; padding: 10px; border-radius: 4px; cursor: pointer;`;
    panel.appendChild(startBtn);

    const statusDiv = document.createElement('div');
    statusDiv.style.cssText = `font-size: 14px; color: #333; padding-top: 6px; text-align: center;`;
    panel.appendChild(statusDiv);

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

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

    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 chương: ${title}`);
            return true;
        }
        setStatus("❌ Không tìm được nội dung chương.");
        return false;
    }

    function goToNextChapter() {
        const nextLink =
            document.querySelector('.bot-next_chap.bot-control')?.href ||
            [...document.querySelectorAll('a')].find(a => /tiếp|next/i.test(a.textContent))?.href ||
            document.querySelector('a[rel="next"]')?.href;

        if (nextLink) {
            console.log("➡️ Chuyển đến:", nextLink);
            localStorage.setItem('dl_active', 'true');
            localStorage.setItem('dl_count', downloadedCount.toString());
            setStatus('➡️ Chuyển chương mới...');
            setTimeout(() => location.href = nextLink, 1500);
            return true;
        } else {
            setStatus('❌ Không tìm thấy link chương tiếp.');
            console.warn("❌ Không tìm thấy link chương tiếp.");
            return false;
        }
    }

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

        if (extractChapterContent()) {
            goToNextChapter();
        } else {
            isAutoDownloading = false;
            localStorage.setItem('dl_active', 'false');
            startBtn.textContent = 'Bắt đầu';
            startBtn.style.backgroundColor = '#4CAF50';
        }
    }

    startBtn.addEventListener('click', () => {
        isAutoDownloading = !isAutoDownloading;
        localStorage.setItem('dl_active', isAutoDownloading ? 'true' : 'false');
        startBtn.textContent = isAutoDownloading ? 'Dừng' : 'Bắt đầu';
        startBtn.style.backgroundColor = isAutoDownloading ? '#f44336' : '#4CAF50';
        if (isAutoDownloading) autoDownloadLoop();
    });

    document.body.appendChild(panel);

    // Nếu reload trang và vẫn đang tải thì tiếp tục
    if (isAutoDownloading) {
        setStatus(`▶️ Tiếp tục tải chương... (${downloadedCount}/${MAX_CHAPTERS})`);
        setTimeout(autoDownloadLoop, 2000); // đợi trang load xong
    } else {
        setStatus("⏸ Chờ bắt đầu...");
    }
})();