PDF Finder and Downloader

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

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

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         PDF Finder and Downloader
// @version      1.2
// @description  Find and add a download button for PDFs on webpages, including embedded PDFs in iframes.
// @license      MIT
// @namespace   pdf downloader
// @match        *://*/*
// @grant        none
// ==/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 = 'red';
        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);
    }

    // Function to find PDF links on the page and add download buttons
    function findAndAddPDFDownloadButtons() {
        const elements = document.querySelectorAll('a, iframe');

        elements.forEach(element => {
            let pdfLink;

            // Check if the element is an iframe
            if (element.tagName.toLowerCase() === 'iframe') {
                // Extract PDF link from iframe src attribute
                pdfLink = element.src;
            } else if (isPDFLink(element.href)) {
                pdfLink = element.href;
            }

            // If a PDF link is found, add a download button
            if (pdfLink) {
                addDownloadButton(element, pdfLink);
            }
        });
    }

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