PDF.js Direct Downloader

Adds a download button to PDF.js viewers that saves the original PDF file

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         PDF.js Direct Downloader
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Adds a download button to PDF.js viewers that saves the original PDF file
// @author       Anonymous
// @match        *://*/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function addDownloadButton() {
        // Check if PDF.js is available
        if (typeof PDFViewerApplication === "undefined" ||
            !PDFViewerApplication.pdfDocument) {
            return;
        }

        // Avoid duplicate buttons
        if (document.getElementById("tm-pdf-download-btn")) return;

        // Create button
        let btn = document.createElement("button");
        btn.id = "tm-pdf-download-btn";
        btn.textContent = "Download PDF";
        btn.style.position = "fixed";
        btn.style.top = "10px";
        btn.style.right = "10px";
        btn.style.zIndex = "9999";
        btn.style.padding = "8px 12px";
        btn.style.background = "#007bff";
        btn.style.color = "white";
        btn.style.border = "none";
        btn.style.borderRadius = "5px";
        btn.style.cursor = "pointer";

        btn.onclick = function() {
            PDFViewerApplication.pdfDocument.getData().then(function(data) {
                let blob = new Blob([data], { type: "application/pdf" });
                let url = URL.createObjectURL(blob);

                let a = document.createElement("a");
                a.href = url;
                a.download = "document.pdf";
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                URL.revokeObjectURL(url);
            });
        };

        document.body.appendChild(btn);
    }

    // Try adding button periodically (since PDF.js may load async)
    setInterval(addDownloadButton, 2000);
})();