Dreame Novel Downloader

Download complete novels from Dreame.com as a text file (placeholder for EPUB)

// ==UserScript==
// @name         Dreame Novel Downloader
// @namespace    http://tampermonkey.net/
// @version      1.5
// @description  Download complete novels from Dreame.com as a text file (placeholder for EPUB)
// @author       Manna Huizar
// @match        https://www.dreame.com/book/*
// @grant        GM_xmlhttpRequest
// @connect      dreame.com
// @license      MIT 
// ==/UserScript==

(function() {
    'use strict';

    // Utility function to fetch chapter content
    function fetchChapter(url) {
        return new Promise((resolve, reject) => {
            GM_xmlhttpRequest({
                method: "GET",
                url: url,
                onload: function(response) {
                    // Parse the chapter content from response
                    const parser = new DOMParser();
                    const doc = parser.parseFromString(response.responseText, 'text/html');
                    // Adjust the selector based on Dreame's chapter content structure
                    const content = doc.querySelector('.chapter-content'); // placeholder selector
                    if (content) {
                        resolve(content.innerText);
                    } else {
                        reject('Content not found');
                    }
                },
                onerror: function() {
                    reject('Request failed');
                }
            });
        });
    }

    // Function to get list of chapter URLs
    function getChapterUrls() {
        // Adjust the selector based on Dreame's chapter list structure
        const chapterLinks = document.querySelectorAll('.chapter-list a'); // placeholder selector
        const urls = Array.from(chapterLinks).map(link => link.href);
        return urls;
    }

    // Main function to download the novel
    async function downloadNovel() {
        const urls = getChapterUrls();
        let novelText = '';

        for (let i = 0; i < urls.length; i++) {
            const url = urls[i];
            try {
                const chapterText = await fetchChapter(url);
                novelText += `\n\nChapter ${i + 1}\n${chapterText}`;
                console.log(`Downloaded chapter ${i + 1}`);
            } catch (e) {
                console.error(`Failed to fetch chapter at ${url}: ${e}`);
            }
        }

        // Create a downloadable file
        const blob = new Blob([novelText], {type: 'text/plain'});
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = 'Complete_Novel.txt';
        a.click();
        URL.revokeObjectURL(url);
    }

    // Add a button to trigger download
    function addDownloadButton() {
        const btn = document.createElement('button');
        btn.innerText = 'Download Complete Novel';
        btn.style.position = 'fixed';
        btn.style.top = '10px';
        btn.style.right = '10px';
        btn.style.zIndex = 1000;
        btn.onclick = downloadNovel;
        document.body.appendChild(btn);
    }

    // Wait for page to load
    window.addEventListener('load', () => {
        addDownloadButton();
    });
})();