PDF Finder and Downloader

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

目前為 2024-03-07 提交的版本,檢視 最新版本

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

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

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

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

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