Chapter DownloaderChapter Downloader

Extract content from truyen.tangthuvien.net

目前为 2025-03-09 提交的版本。查看 最新版本

// ==UserScript==
// @name         Chapter DownloaderChapter Downloader
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Extract content from truyen.tangthuvien.net
// @author       You
// @match        https://truyen.tangthuvien.net/doc-truyen/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Create container for extracted content
    function createContainer() {
        // Check if container already exists
        if (document.getElementById('content-extractor')) {
            return;
        }

        const container = document.createElement('div');
        container.id = 'content-extractor';
        container.style.position = 'fixed';
        container.style.bottom = '0';
        container.style.left = '0';
        container.style.right = '0';
        container.style.backgroundColor = '#f5f5f5';
        container.style.padding = '15px';
        container.style.zIndex = '9999';
        container.style.boxShadow = '0 -2px 10px rgba(0,0,0,0.1)';
        container.style.maxHeight = '50vh';
        container.style.overflow = 'auto';

        const title = document.createElement('h3');
        title.textContent = 'Nội dung truyện đã trích xuất';
        title.style.marginTop = '0';
        container.appendChild(title);

        const textarea = document.createElement('textarea');
        textarea.id = 'extracted-content';
        textarea.style.width = '100%';
        textarea.style.height = '150px';
        textarea.style.marginBottom = '10px';
        textarea.style.padding = '10px';
        textarea.style.boxSizing = 'border-box';
        container.appendChild(textarea);

        const buttonContainer = document.createElement('div');
        buttonContainer.style.display = 'flex';
        buttonContainer.style.gap = '10px';

        const extractButton = document.createElement('button');
        extractButton.textContent = 'Trích xuất nội dung trang hiện tại';
        extractButton.style.padding = '8px 15px';
        extractButton.style.backgroundColor = '#4caf50';
        extractButton.style.color = 'white';
        extractButton.style.border = 'none';
        extractButton.style.borderRadius = '4px';
        extractButton.style.cursor = 'pointer';
        extractButton.addEventListener('click', extractContent);
        buttonContainer.appendChild(extractButton);

        const nextChapterButton = document.createElement('button');
        nextChapterButton.textContent = 'Chuyển đến chương tiếp theo';
        nextChapterButton.style.padding = '8px 15px';
        nextChapterButton.style.backgroundColor = '#2196f3';
        nextChapterButton.style.color = 'white';
        nextChapterButton.style.border = 'none';
        nextChapterButton.style.borderRadius = '4px';
        nextChapterButton.style.cursor = 'pointer';
        nextChapterButton.addEventListener('click', goToNextChapter);
        buttonContainer.appendChild(nextChapterButton);

        const clearButton = document.createElement('button');
        clearButton.textContent = 'Xóa nội dung';
        clearButton.style.padding = '8px 15px';
        clearButton.style.backgroundColor = '#f44336';
        clearButton.style.color = 'white';
        clearButton.style.border = 'none';
        clearButton.style.borderRadius = '4px';
        clearButton.style.cursor = 'pointer';
        clearButton.addEventListener('click', clearContent);
        buttonContainer.appendChild(clearButton);

        const copyButton = document.createElement('button');
        copyButton.textContent = 'Sao chép nội dung';
        copyButton.style.padding = '8px 15px';
        copyButton.style.backgroundColor = '#ff9800';
        copyButton.style.color = 'white';
        copyButton.style.border = 'none';
        copyButton.style.borderRadius = '4px';
        copyButton.style.cursor = 'pointer';
        copyButton.addEventListener('click', copyContent);
        buttonContainer.appendChild(copyButton);

        container.appendChild(buttonContainer);
        document.body.appendChild(container);
    }

    // Extract content from current page
    function extractContent() {
        const textarea = document.getElementById('extracted-content');
        
        // Get chapter title
        const chapterTitle = document.querySelector('h2');
        let titleText = chapterTitle ? chapterTitle.textContent.trim() : 'Không tìm thấy tiêu đề';
        
        // Get chapter content
        const chapterContentBox = document.querySelector('.box-chap');
        let contentText = '';
        
        if (chapterContentBox) {
            // Remove unwanted elements like scripts, styles, ads
            const contentClone = chapterContentBox.cloneNode(true);
            const scripts = contentClone.querySelectorAll('script, style, .ads, .banner, .comment');
            scripts.forEach(script => script.remove());
            
            // Get the clean text
            contentText = contentClone.innerText.trim();
        } else {
            contentText = 'Không tìm thấy nội dung chương';
        }
        
        // Append to existing content
        textarea.value += `\n\n${titleText}\n\n${contentText}`;
        
        // Scroll to bottom of textarea
        textarea.scrollTop = textarea.scrollHeight;
    }

    // Go to next chapter
    function goToNextChapter() {
        const nextButton = document.querySelector('.bot-next_chap.bot-control');
        if (nextButton) {
            // Extract current content before moving to next chapter
            extractContent();
            
            // Navigate to next chapter
            nextButton.click();
            
            // Add a delay to let the next page load before extracting content
            setTimeout(() => {
                extractContent();
            }, 2000);
        } else {
            alert('Không tìm thấy nút chuyển đến chương tiếp theo');
        }
    }

    // Clear content in textarea
    function clearContent() {
        const textarea = document.getElementById('extracted-content');
        textarea.value = '';
    }

    // Copy content to clipboard
    function copyContent() {
        const textarea = document.getElementById('extracted-content');
        textarea.select();
        document.execCommand('copy');
        
        // Visual feedback
        const originalColor = textarea.style.backgroundColor;
        textarea.style.backgroundColor = '#e6ffe6';
        setTimeout(() => {
            textarea.style.backgroundColor = originalColor;
        }, 500);
    }

    // Initialize when page is loaded
    function init() {
        createContainer();
    }

    // Run when page is fully loaded
    window.addEventListener('load', init);
})();