PDF Finder and Downloader

Find and add a download button for PDFs on webpages, including embedded PDFs in iframes.

当前为 2024-03-07 提交的版本,查看 最新版本

// ==UserScript==
// @name         PDF Finder and Downloader
// @version      1.1
// @description  Find and add a download button for PDFs on webpages, including embedded PDFs in iframes.
// @license      MIT
// @match        *://*/*
// @grant        none
// @namespace pdf downloader
// ==/UserScript==

(function() {
    'use strict';

    // Function to check if a link ends with .pdf
    function isPDFLink(link) {
        return link.toLowerCase().endsWith('.pdf');
    }

    // Function to add a download button next to a PDF link
    function addDownloadButton(element, pdfLink) {
        const downloadButton = document.createElement('button');
        downloadButton.innerText = 'PDF ⬇️';
        downloadButton.style.backgroundColor = 'violet'; // Change color to violet
        downloadButton.style.color = 'white';
        downloadButton.style.border = 'none';
        downloadButton.style.padding = '5px 10px';
        downloadButton.style.marginLeft = '5px';
        downloadButton.style.cursor = 'pointer';

        downloadButton.addEventListener('click', function(event) {
            event.preventDefault();
            window.open(pdfLink, '_blank');
        });

        // Insert the download button next to the PDF link or iframe
        element.parentNode.insertBefore(downloadButton, element.nextSibling);

        // Add circular button beside the PDF ⬇️ button
        addCircularButton(downloadButton);
    }

    // Function to add a circular button beside the PDF ⬇️ button
    function addCircularButton(downloadButton) {
        const circularButton = document.createElement('button');
        circularButton.innerText = '-';
        circularButton.style.backgroundColor = 'green'; // Set color to green
        circularButton.style.color = 'white';
        circularButton.style.border = 'none';
        circularButton.style.borderRadius = '50%'; // Make it circular
        circularButton.style.width = '20px';
        circularButton.style.height = '20px';
        circularButton.style.marginLeft = '5px';
        circularButton.style.cursor = 'pointer';

        // Add click event listener to exclude the website
        circularButton.addEventListener('click', function(event) {
            event.preventDefault();
            excludeWebsiteFromScript();
        });

        // Insert the circular button next to the PDF ⬇️ button
        downloadButton.parentNode.insertBefore(circularButton, downloadButton.nextSibling);
    }

    // Function to exclude the current website from running the script
    function excludeWebsiteFromScript() {
        const currentUrl = window.location.href;
        let excludedSites = JSON.parse(localStorage.getItem('excludedSites')) || [];
        if (!excludedSites.includes(currentUrl)) {
            excludedSites.push(currentUrl);
            localStorage.setItem('excludedSites', JSON.stringify(excludedSites));
        }
    }

    // Function to check if the current website should be excluded from running the script
    function shouldExcludeWebsite() {
        const currentUrl = window.location.href;
        const excludedSites = JSON.parse(localStorage.getItem('excludedSites')) || [];
        return excludedSites.includes(currentUrl);
    }

    // Function to find PDF links on the page and add download buttons
    function findAndAddPDFDownloadButtons() {
        if (!shouldExcludeWebsite()) {
            const links = document.querySelectorAll('a, iframe');
            links.forEach(link => {
                let pdfLink;
                if (link.tagName.toLowerCase() === 'iframe') {
                    const dataSource = link.getAttribute('data-source');
                    if (dataSource) {
                        pdfLink = getPDFLinkFromDataSource(dataSource);
                    }
                } else if (isPDFLink(link.href)) {
                    pdfLink = link.href;
                }
                if (pdfLink) {
                    addDownloadButton(link, pdfLink);
                }
            });
        }
    }

    // Function to extract PDF link from data-source attribute
    function getPDFLinkFromDataSource(dataSource) {
        const match = dataSource.match(/file=([^&]+)/);
        if (match && match[1]) {
            return decodeURIComponent(match[1]);
        }
        return null;
    }

    // Run the function when the page is fully loaded
    window.addEventListener('load', findAndAddPDFDownloadButtons);

})();