Chapter Downloader Auto

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

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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
    }
})();