PDF Replacer

none

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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');
})();