Anonfiles PDF Viewer

Adds a button to view PDF and TXT files without needing to download them.

// ==UserScript==
// @name         Anonfiles PDF Viewer
// @namespace    https://leaked.wiki
// @version      0.21
// @description  Adds a button to view PDF and TXT files without needing to download them.
// @author       Sango
// @match        *://anonfiles.com/*
// @match        *://bayfiles.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=anonfiles.com
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';
    // Change this to true if you want it to always show the PDF or TXT
    const ALWAYS_SHOW = false,
          PDF_HEIGHT = '800px';

    const icon = '<img src="/img/file/filetypes/ext/pdf.png">',
          btnText = 'View as ';
    // Check if file is a PDF
    const downloadUrl = document.getElementById('download-url');
    if (downloadUrl && (downloadUrl.href.toLowerCase().endsWith('pdf') || downloadUrl.href.toLowerCase().endsWith('txt'))) {
        const directURL = downloadUrl.href,
              fileType = directURL.slice(-3).toUpperCase();
        // if (fileType == 'PDF') directURL = 'https://docs.google.com/viewer?url=' + directURL + '&embedded=true';
        // Create a button to show the PDF
        const pdfBtn = document.createElement('a');
        pdfBtn.href = '#Sango';
        pdfBtn.id = 'show-pdf';
        pdfBtn.type = 'button';
        pdfBtn.className = 'btn btn-primary btn-block';
        pdfBtn.style.background = '#245488';
        pdfBtn.innerHTML = icon + ' View as ' + fileType;
        if (!ALWAYS_SHOW) downloadUrl.parentNode.insertBefore(pdfBtn, downloadUrl);

        // Create an iframe element to embed the PDF
        const pdfFrame = Object.assign(document.createElement('iframe'), {
            id: 'pdf-viewer',
            src: 'https://docs.google.com/viewer?url=' + directURL + '&embedded=true',
            style: 'width: 100%; height: 100%; min-height: ' + PDF_HEIGHT + "; margin-bottom: 10px; border: 1px solid #245488; background: url(\"data:image/svg+xml,%0A%3Csvg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink' style='width:100px;' preserveAspectRatio='xMidYMid' viewBox='10.67 44 78.67 12'%3E%3Cg transform='translate(16.666666666666668 50)'%3E%3Ccircle cx='0' cy='0' r='6' fill='%234087b6'%3E%3CanimateTransform attributeName='transform' type='scale' begin='-0.4938271604938271s' calcMode='spline' keySplines='0.3 0 0.7 1;0.3 0 0.7 1' values='0;1;0' keyTimes='0;0.5;1' dur='1.2345679012345678s' repeatCount='indefinite'%3E%3C/animateTransform%3E%3C/circle%3E%3C/g%3E%3Cg transform='translate(33.333333333333336 50)'%3E%3Ccircle cx='0' cy='0' r='6' fill='%233886ba'%3E%3CanimateTransform attributeName='transform' type='scale' begin='-0.3703703703703703s' calcMode='spline' keySplines='0.3 0 0.7 1;0.3 0 0.7 1' values='0;1;0' keyTimes='0;0.5;1' dur='1.2345679012345678s' repeatCount='indefinite'%3E%3C/animateTransform%3E%3C/circle%3E%3C/g%3E%3Cg transform='translate(50 50)'%3E%3Ccircle cx='0' cy='0' r='6' fill='%235298c7'%3E%3CanimateTransform attributeName='transform' type='scale' begin='-0.24691358024691354s' calcMode='spline' keySplines='0.3 0 0.7 1;0.3 0 0.7 1' values='0;1;0' keyTimes='0;0.5;1' dur='1.2345679012345678s' repeatCount='indefinite'%3E%3C/animateTransform%3E%3C/circle%3E%3C/g%3E%3Cg transform='translate(66.66666666666667 50)'%3E%3Ccircle cx='0' cy='0' r='6' fill='%234591c2'%3E%3CanimateTransform attributeName='transform' type='scale' begin='-0.12345679012345677s' calcMode='spline' keySplines='0.3 0 0.7 1;0.3 0 0.7 1' values='0;1;0' keyTimes='0;0.5;1' dur='1.2345679012345678s' repeatCount='indefinite'%3E%3C/animateTransform%3E%3C/circle%3E%3C/g%3E%3Cg transform='translate(83.33333333333333 50)'%3E%3Ccircle cx='0' cy='0' r='6' fill='%234087b6'%3E%3CanimateTransform attributeName='transform' type='scale' begin='0s' calcMode='spline' keySplines='0.3 0 0.7 1;0.3 0 0.7 1' values='0;1;0' keyTimes='0;0.5;1' dur='1.2345679012345678s' repeatCount='indefinite'%3E%3C/animateTransform%3E%3C/circle%3E%3C/g%3E%3C/svg%3E\") center / 200px no-repeat;"
        });

        // Add button functionality
        if (ALWAYS_SHOW) document.querySelector('.top-wrapper').appendChild(pdfFrame);
        else {
            document.getElementById('show-pdf').addEventListener('click', (e) => {
                var pdfViewer = document.getElementById('pdf-viewer');
                if (e.target.text == ' View as ' + fileType) {
                    if (pdfViewer) pdfViewer.style.display = ''
                    else document.getElementsByClassName('top-wrapper')[0].appendChild(pdfFrame);
                    e.target.innerHTML = icon + ' Hide ' + fileType;
                } else {
                    e.target.innerHTML = icon + ' View as ' + fileType;
                    pdfViewer.style.display = 'none';
                }
            });
        }
    };
})();