PDF Replacer

none

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

Advertisement:

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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