Scribd Material Downloader (Fixed + Auto-Scroll)

View/Download Scribd content as TXT or PDF (fixed selectors, regex, auto-scroll loader)

// ==UserScript==
// @name         Scribd Material Downloader (Fixed + Auto-Scroll)
// @namespace    Violentmonkey Scripts
// @version      1.4
// @description  View/Download Scribd content as TXT or PDF (fixed selectors, regex, auto-scroll loader)
// @author       Original: Angesom12 | Update: fd2013
// @license      MIT
// @match        *://www.scribd.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    // Load jsPDF dynamically
    const jsPDFScript = document.createElement('script');
    jsPDFScript.src = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js';
    jsPDFScript.onload = () => initDownloader();
    document.head.appendChild(jsPDFScript);

    function initDownloader() {
        const { jsPDF } = window.jspdf;

        // --- Button Container ---
        const container = document.createElement('div');
        container.className = 'button-container';
        Object.assign(container.style, {
            position: 'fixed',
            top: '50%',
            right: '40px',
            transform: 'translateY(-50%)',
            display: 'flex',
            flexDirection: 'column',
            gap: '12px',
            zIndex: '999999'
        });

        // --- Styles ---
        const style = document.createElement('style');
        style.textContent = `
            .dl-btn {
                padding: 12px 20px;
                font-size: 14px;
                font-weight: bold;
                border: none;
                border-radius: 8px;
                cursor: pointer;
                color: #fff;
                box-shadow: 0 3px 6px rgba(0,0,0,0.25);
                transition: transform 0.2s, opacity 0.2s;
            }
            .dl-btn:hover { transform: scale(1.1); opacity: 0.9; }
            .btn-view { background-color: #27ae60; }
            .btn-txt  { background-color: #f39c12; }
            .btn-pdf  { background-color: #8e44ad; }
        `;
        document.head.appendChild(style);

        // --- Buttons ---
        const btnView = makeBtn('View Full', 'btn-view', redirectToEmbed);
        const btnTXT  = makeBtn('Download TXT', 'btn-txt', () => autoScroll(downloadTXT));
        const btnPDF  = makeBtn('Download PDF', 'btn-pdf', () => autoScroll(downloadPDF));

        container.append(btnView, btnTXT, btnPDF);
        document.body.appendChild(container);

        function makeBtn(label, cls, action) {
            const btn = document.createElement('button');
            btn.className = `dl-btn ${cls}`;
            btn.textContent = label;
            btn.addEventListener('click', action);
            return btn;
        }

        // --- Actions ---
        function redirectToEmbed() {
            const currentUrl = window.location.href;
            const regex = /scribd\.com\/(?:doc|document|book)\/(\d+)/;
            const match = currentUrl.match(regex);

            if (match) {
                const newUrl = `https://www.scribd.com/embeds/${match[1]}/content`;
                window.location.href = newUrl;
            } else {
                alert("Unable to open embed view. Try refreshing.");
            }
        }

        function extractContent() {
            const elements = document.querySelectorAll('.text_layer div, .text_layer span, .text_layer p');
            let text = '';
            elements.forEach(el => {
                const t = el.textContent.trim();
                if (t) text += t + '\n';
            });
            return text;
        }

        function downloadTXT() {
            const content = extractContent();
            if (!content.trim()) {
                alert("No content found. Try opening embed view.");
                return;
            }
            const blob = new Blob([content], { type: 'text/plain' });
            const url = URL.createObjectURL(blob);
            const a = document.createElement('a');
            a.href = url;
            a.download = 'scribd_material.txt';
            a.click();
            URL.revokeObjectURL(url);
        }

        function downloadPDF() {
            const content = extractContent();
            if (!content.trim()) {
                alert("No content found. Try opening embed view.");
                return;
            }
            const doc = new jsPDF();
            const lines = content.split('\n');
            let y = 10;

            lines.forEach(line => {
                if (y > 280) { doc.addPage(); y = 10; }
                doc.text(line, 10, y);
                y += 7;
            });
            doc.save('scribd_material.pdf');
        }

        // --- Auto Scroll Helper ---
        function autoScroll(callback) {
            let totalHeight = 0;
            const distance = 800;
            const delay = 400; // ms between scrolls

            const timer = setInterval(() => {
                const scrollHeight = document.body.scrollHeight;
                window.scrollBy(0, distance);
                totalHeight += distance;

                if (totalHeight >= scrollHeight - window.innerHeight) {
                    clearInterval(timer);
                    setTimeout(() => {
                        window.scrollTo(0, 0); // return to top
                        callback();
                    }, 1000);
                }
            }, delay);
        }
    }
})();