PDF Replacer

none

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey, Greasemonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Violentmonkey.

Voor het installeren van scripts heb je een extensie nodig, zoals Tampermonkey of Userscripts.

Voor het installeren van scripts heb je een extensie nodig, zoals {tampermonkey_link:Tampermonkey}.

Voor het installeren van scripts heb je een gebruikersscriptbeheerder nodig.

(Ik heb al een user script manager, laat me het downloaden!)

Advertisement:

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een extensie nodig, zoals {stylus_link:Stylus}.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

Voor het installeren van gebruikersstijlen heb je een gebruikersstijlbeheerder nodig.

(Ik heb al een beheerder - laat me doorgaan met de installatie!)

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