PDF Replacer

none

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==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');
})();