有章PDF下载

下载有章文档

目前为 2024-06-26 提交的版本。查看 最新版本

// ==UserScript==
// @name         有章PDF下载
// @namespace    http://tampermonkey.net/
// @version      0.0.3
// @description  下载有章文档
// @author       JoyofFire
// @match        https://www.ilawpress.com/assets/plugin/pdfviewer/web/viewer.html
// @icon         https://www.google.com/s2/favicons?sz=64&domain=ilawpress.com
// @grant        none
// @run-at       document-start
// @license      GPL-3.0-only
// ==/UserScript==


(function() {
    'use strict';


    // 全局常量
    const DL_PDF_API = "download_pdf";
    const DL_BTN = `<button id="dl-pdf" class="extend-navbar-btn fa fa-download" title="下载PDF" style="margin-right:1.1em;color:red" onclick="${DL_PDF_API}()"><span>下载PDF</span></button>`; //`<button id="dl-pdf" class="extend-navbar-btn fa fa-download" title="下载PDF" style="position:fixed;right: 2em;color:red" onclick="${DL_PDF_API}()"><span>下载PDF</span></button>`
    const { createObjectURL } = URL;
    const { insertAdjacentHTML } = Element.prototype;

    const fn_list = [createObjectURL, insertAdjacentHTML];
    for (const fn of fn_list) {
        reject_modify(fn, "call", fn.call);
    }

    function print(...args) {
        const time = new Date().toTimeString().slice(0, 8);
        console.info(`[wk ${time}]`, ...args);
    }

    function reject_modify(obj, prop, value) {
        // debugger;
        Object.defineProperty(obj, prop, {
            enumerable: true,
            configurable: false,
            writable: false,
            value,
        });
    }

    /**
     * @param {string} selectors 
     * @returns {HTMLElement}
     */
    function $(selectors) {
        return document.querySelector(selectors);
    }


    function add_dl_btn() {
        // document.body.insertAdjacentHTML("beforeend", DL_BTN);
        insertAdjacentHTML.call($("#toolbarViewerRight"), "afterbegin", DL_BTN);
        print("btn added");
    }

    function on_data(data) {
        print("data:", data);
        window.data = data;
        const blob = new Blob([data], { type: "application/pdf" });
        const url = createObjectURL.call(URL, blob);
        window.url = url;
        print("url:", window.url);
        window[DL_PDF_API] = () => open(url);
    }

    function hook_then() {
        print("entered hook_then");

        const then = Promise.prototype.then;
        Promise.prototype.then = function(...args) {
            for (const [i, arg] of args.entries()) {
                if (String(arg).includes("(pdfDocument) {")) {
                    print(...args);

                    args[i] = function(doc) {
                        //debugger;
                        // window.mydoc = doc;
                        print("doc:", doc);
                        doc._transport.getData().then(on_data);
                        return arg.call(this, doc);
                    };
                    break;
                }
            }
            return then.apply(this, args);
        };
    }

    function on_dom_loaded() {
        print("dom_loaded");
        add_dl_btn();
        // debugger;
    }

    function main() {
        document.addEventListener("DOMContentLoaded", on_dom_loaded);
        print("entered main");
        hook_then();
    }

    main();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址