Trích xuất toàn bộ chương truyện

Tải tất cả các chương từ trang danh sách chương truyen.tangthuvien.vn

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Trích xuất toàn bộ chương truyện
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Tải tất cả các chương từ trang danh sách chương truyen.tangthuvien.vn
// @match        https://truyen.tangthuvien.vn/doc-truyen/*
// @grant        GM_xmlhttpRequest
// @connect      truyen.tangthuvien.vn
// ==/UserScript==

(function () {
    'use strict';

    // Giao diện
    const panel = document.createElement('div');
    panel.style.cssText = 'position:fixed;top:10%;right:10px;width:300px;background:#fff;border:1px solid #ccc;padding:10px;z-index:9999;font-family:sans-serif;';
    panel.innerHTML = `
        <textarea id="outputText" style="width:100%;height:300px;margin-bottom:10px;"></textarea>
        <button id="startBtn" style="width:100%;padding:5px 0;margin-bottom:5px;">Bắt đầu tải</button>
        <button id="copyBtn" style="width:100%;padding:5px 0;">Sao chép</button>
    `;
    document.body.appendChild(panel);

    const textarea = document.getElementById('outputText');
    const startBtn = document.getElementById('startBtn');
    const copyBtn = document.getElementById('copyBtn');

    function sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms));
    }

    async function fetchChapter(url) {
        return new Promise((resolve) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: url,
                onload: function (response) {
                    const html = response.responseText;
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(html, 'text/html');
                    const title = doc.querySelector('h2')?.innerText.trim() || 'Không tiêu đề';
                    const content = doc.querySelector('.box-chap')?.innerText.trim() || 'Không có nội dung';
                    resolve(`\n\n------------\n\n${title}\n\n${content}`);
                }
            });
        });
    }

    async function startDownload() {
        const links = [...document.querySelectorAll('a[href*="/chuong-"]')]
            .filter(a => a.href && !a.href.includes('javascript:void'));

        if (links.length === 0) {
            alert("Không tìm thấy chương nào.");
            return;
        }

        startBtn.disabled = true;
        for (let i = 0; i < links.length; i++) {
            const url = links[i].href;
            textarea.value += `\nĐang tải: ${url}\n`;
            const data = await fetchChapter(url);
            textarea.value += data;
            await sleep(1000); // delay giữa các chương
        }

        alert('Hoàn tất!');
        startBtn.disabled = false;
    }

    startBtn.addEventListener('click', startDownload);
    copyBtn.addEventListener('click', () => {
        textarea.select();
        document.execCommand('copy');
        alert('Đã sao chép nội dung!');
    });
})();