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 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Trích xuất toàn bộ chương truyện
// @namespace    http://tampermonkey.net/
// @version      4.0
// @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';

    if (!location.href.includes('/doc-truyen/') || location.href.includes('/doc-truyen/*/*')) return;

    // UI
    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('.list-chapter a')];
        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); // Chờ 1 giây giữa mỗi chương để tránh bị chặn
        }

        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!');
    });
})();