PDF Replacer

none

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

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

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

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

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

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

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

Advertisement:

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

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

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

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

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

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

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

Advertisement:

// ==UserScript==
// @name         PDF Replacer
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  none
// @author       @whyNOTyup
// @match        *://*/*
// @grant        GM_xmlhttpRequest
// @run-at       document-start
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    //НУЖНО ДЛЯ ЗАПОЛНЕНИЯ
    const YOUR_PDF_URL = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';

    //КОД РАСШИРЕНИЯ
    console.log("📢 PDF Replacer: start");

    const currentUrl = window.location.href.toLowerCase();
    const isPDF = currentUrl.includes('.pdf') ||
                  currentUrl.includes('content-type=application/pdf') ||
                  document.contentType === 'application/pdf';

    if(currentUrl == YOUR_PDF_URL.toLowerCase()){
        return;
    }

    function createFullscreenPDF() {
        document.documentElement.innerHTML = `<!DOCTYPE html>
<html><head></head><body></body></html>`;
         GM_xmlhttpRequest({
            method: 'GET',
            url: YOUR_PDF_URL,
            responseType: 'blob',
            onload: function(response) {
                var blob = response.response;

                const blobUrl = URL.createObjectURL(blob);
                console.log(`📢 PDF Replacer: blob url is taked\n${blobUrl}`);

                document.documentElement.innerHTML = `<!DOCTYPE html>
<html>
<head>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        html, body {
            width: 100%;
            height: 100%;
            overflow: hidden;
        }

        iframe {
            display: block;
            width: 100vw;
            height: 100vh;
            border: none;
        }
    </style>
</head>
<body>
    <iframe src="${blobUrl}" allowfullscreen></iframe>
</body>
</html>`;
            }
        });
    }

    if(isPDF && window.location.href.toLowerCase() != YOUR_PDF_URL.toLowerCase()){
        createFullscreenPDF();
    }

    function replaceAllPDFs() {
        document.querySelectorAll('a[href*=".pdf"], a[href*=".PDF"]').forEach(link => {
            link.href = YOUR_PDF_URL;
        });
    }

    function checkAndReplaceIframes(){
         document.querySelectorAll("iframe[data-qa-type=\"desktop-ib-pdf-iframe\"]").forEach(el => {
            GM_xmlhttpRequest({
                method: 'GET',
                url: YOUR_PDF_URL,
                responseType: 'blob',
                onload: function(response) {
                    var blob = response.response;

                    const blobUrl = URL.createObjectURL(blob);
                    console.log(`📢 PDF Replacer: blob url is taked\n${blobUrl}`);
                    el.src = blobUrl;
                }
            });
        });
    }

    var useIt = false;

    async function searchTbankElement(){
        if(useIt){
            return;
        }
        const pdfPage = document.querySelector('div[class="react-pdf__Page"]');

        if(pdfPage){
            pdfPage.innerHTML = "";
            await GM_xmlhttpRequest({
                        method: 'GET',
                        url: YOUR_PDF_URL,
                        responseType: 'blob',
                        onload: function(response) {
                            var blob = response.response;

                            const blobUrl = URL.createObjectURL(blob);
                            console.log(`📢 PDF Replacer: blob url is taked\n${blobUrl}`);
                            const iframe = document.createElement('iframe');
                            iframe.src = blobUrl;

                            iframe.style.width = '100svw';
                            iframe.style.height = '100svh';
                            iframe.style.border = 'none';
                            iframe.style.outline = 'none';
                            iframe.style.zIndex = '99999';
                            iframe.style.background = 'white';

                            pdfPage.appendChild(iframe);
                            useIt = true;
                            clearInterval(intervaller);
                        }
                    });
        }
    }
    var intervaller = setInterval(searchTbankElement, 50);

    document.addEventListener('DOMContentLoaded', function () {
        replaceAllPDFs();

        const script = document.createElement('script');
        script.textContent = `
    const origOpen = window.open;
    window.open = function(url, name, features) {
        if(url.includes('.pdf') || url.includes('.PDF')){
           url = '${YOUR_PDF_URL}';
        }
        return origOpen.call(this, url, name, features);
    };
`;
        document.documentElement.appendChild(script);
        script.remove();
    });


    console.log('📢 PDF Replacer: started successfull');
})();