Chapter Downloader

Tải tối đa 200 chương từ 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
// @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!');
    });

})();