PDF Viewer Embedder

Embeds PDFs directly in the page instead of downloading

2025-04-29 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         PDF Viewer Embedder
// @namespace    *
// @version      1.5
// @author       zinchaiku
// @description  Embeds PDFs directly in the page instead of downloading
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    function showPDF(srcUrl) {
        const existing = document.getElementById('tampermonkey-pdf-viewer');
        if (existing) existing.remove();

        const overlay = document.createElement('div');
        overlay.id = 'tampermonkey-pdf-viewer';
        overlay.style.position = 'fixed';
        overlay.style.top = 0;
        overlay.style.left = 0;
        overlay.style.width = '100vw';
        overlay.style.height = '100vh';
        overlay.style.background = 'rgba(0,0,0,0.8)';
        overlay.style.zIndex = 99999;
        overlay.style.display = 'flex';
        overlay.style.alignItems = 'center';
        overlay.style.justifyContent = 'center';

        const iframe = document.createElement('iframe');
        iframe.src = srcUrl;
        iframe.style.width = '80vw';
        iframe.style.height = '90vh';
        iframe.style.border = 'none';
        iframe.style.background = '#fff';
        overlay.appendChild(iframe);

        const closeBtn = document.createElement('button');
        closeBtn.textContent = 'Close PDF';
        closeBtn.style.position = 'absolute';
        closeBtn.style.top = '20px';
        closeBtn.style.right = '40px';
        closeBtn.style.zIndex = 100000;
        closeBtn.style.padding = '2px 6px';
        closeBtn.style.fontSize = '1.2em';
        closeBtn.onclick = () => overlay.remove();
        overlay.appendChild(closeBtn);

        document.body.appendChild(overlay);
    }

    function isPDFLink(href) {
        try {
            const url = new URL(href, window.location.href);
            return url.pathname.toLowerCase().endsWith('.pdf');
        } catch {
            return false;
        }
    }

    function attachPDFInterceptors() {
        document.querySelectorAll('a[href]').forEach(link => {
            const href = link.getAttribute('href');
            if (!href || link.dataset.tmPdfBound) return;

            const isWrapper = href.includes('_download.html');
            const isDirectPDF = isPDFLink(href);

            if (isWrapper || isDirectPDF) {
                link.dataset.tmPdfBound = "1";

                link.addEventListener('click', function (e) {
                    e.preventDefault();

                    if (isDirectPDF) {
                        // Just embed the link directly
                        showPDF(link.href);
                    } else {
                        // Fetch and convert to blob (for download wrappers)
                        fetch(link.href, { credentials: 'include' })
                            .then(res => {
                                const contentType = res.headers.get('Content-Type') || '';
                                if (contentType.includes('pdf')) {
                                    return res.blob().then(blob => {
                                        const blobUrl = URL.createObjectURL(blob);
                                        showPDF(blobUrl);
                                    });
                                } else {
                                    window.location.href = link.href;
                                }
                            })
                            .catch(err => {
                                alert("Could not open PDF: " + err);
                                window.location.href = link.href;
                            });
                    }
                });
            }
        });
    }

    window.addEventListener('keydown', (e) => {
        if (e.key === 'Escape') {
            const viewer = document.getElementById('tampermonkey-pdf-viewer');
            if (viewer) viewer.remove();
        }
    });

    attachPDFInterceptors();
    const observer = new MutationObserver(attachPDFInterceptors);
    observer.observe(document.body, { childList: true, subtree: true });
})();