Chapter Downloader

Tải tối đa 200 chương từ truyen.tangthuvien.vn

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Chapter Downloader
// @namespace    http://tampermonkey.net/
// @version      3.0
// @description  Tải tối đa 200 chương từ truyen.tangthuvien.vn
// @author       Bạn
// @match        https://truyen.tangthuvien.vn/doc-truyen/*
// @grant        none
// ==/UserScript==

(function () {
    'use strict';

    let maxChapters = 200;
    let downloadedChapters = 0;
    let isAutoDownloading = false;

    // UI Panel setup
    const panel = document.createElement('div');
    panel.innerHTML = `
        <textarea id="content-textarea" style="width:100%; height:300px; margin-bottom:10px;"></textarea>
        <button id="start-btn">Bắt đầu</button>
        <button id="stop-btn">Dừng</button>
        <button id="clear-btn">Xoá</button>
        <button id="copy-btn">Sao chép</button>
        <div style="font-size: 12px; color: gray; margin-top: 10px;">
            Tải tối đa ${maxChapters} chương.
        </div>
    `;
    Object.assign(panel.style, {
        position: 'fixed',
        top: '10%',
        right: '10px',
        width: '300px',
        backgroundColor: '#f8f8f8',
        border: '1px solid #ccc',
        borderRadius: '5px',
        padding: '10px',
        zIndex: 9999,
        boxShadow: '0 0 10px rgba(0,0,0,0.2)',
    });
    document.body.appendChild(panel);

    const textarea = document.getElementById('content-textarea');
    const startBtn = document.getElementById('start-btn');
    const stopBtn = document.getElementById('stop-btn');
    const clearBtn = document.getElementById('clear-btn');
    const copyBtn = document.getElementById('copy-btn');

    // Helper: get current chapter content
    function extractChapterContent() {
        const titleElement = document.querySelector('h2');
        const contentElement = document.querySelector('.box-chap');

        if (!titleElement || !contentElement) return false;

        const title = titleElement.innerText.trim();
        const content = contentElement.innerText.trim();

        if (textarea.value) {
            textarea.value += `\n\n---\n\n${title}\n\n${content}`;
        } else {
            textarea.value = `${title}\n\n${content}`;
        }

        downloadedChapters++;
        return true;
    }

    // Helper: go to next chapter
    function goToNextChapter() {
        const nextBtn = document.querySelector('.bot-next_chap.bot-control');
        if (nextBtn) nextBtn.click();
    }

    // Monitor for new page load
    function waitForNewContent(callback) {
        const oldTitle = document.querySelector('h2')?.innerText;
        const checkInterval = setInterval(() => {
            const newTitle = document.querySelector('h2')?.innerText;
            if (newTitle && newTitle !== oldTitle) {
                clearInterval(checkInterval);
                callback();
            }
        }, 800);
    }

    function startAutoDownload() {
        isAutoDownloading = true;
        downloadedChapters = 0;
        downloadLoop();
    }

    function downloadLoop() {
        if (!isAutoDownloading || downloadedChapters >= maxChapters) {
            console.log('✅ Dừng tự động.');
            isAutoDownloading = false;
            return;
        }

        const success = extractChapterContent();
        if (!success) {
            console.log('⚠️ Không tìm thấy nội dung.');
            isAutoDownloading = false;
            return;
        }

        goToNextChapter();
        waitForNewContent(() => {
            setTimeout(downloadLoop, 1000);
        });
    }

    // Event listeners
    startBtn.addEventListener('click', startAutoDownload);
    stopBtn.addEventListener('click', () => { isAutoDownloading = false; });
    clearBtn.addEventListener('click', () => { textarea.value = ''; });
    copyBtn.addEventListener('click', () => {
        textarea.select();
        document.execCommand('copy');
        alert('📋 Đã sao chép!');
    });

})();