Shit Journal Download

Download as PDF.

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Shit Journal Download
// @description  Download as PDF.
// @match        https://shitjournal.org/*
// @icon         https://shitjournal.org/LOGO2.png
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/jspdf.min.js#mds=cf869e54b0b882862e545346a3d82649
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/html2canvas.min.js
// @require      https://cdn.jsdelivr.net/npm/[email protected]/dist/dom-to-image.min.js
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_download
// @license      MIT
// @version 0.0.1.20260328114635
// @namespace https://greatest.deepsurf.us/users/1571222
// ==/UserScript==

(function() {
    'use strict';

    // Your code here...
    const milliseconds = 250;

    let idInterval = 0;
    function funcDownload(watermark) {
        if (idInterval != 0) {
            return;
        }

        let watermark_domtoimage;
        if (watermark) {
            const absolute = document.querySelector("div.absolute");
            if (absolute) {
                const canvas = document.querySelector("canvas");
                if (!canvas) {
                    return;
                }

                html2canvas(absolute, {
                    backgroundColor: null,
                    scale: canvas.width / absolute.clientWidth
                }).then(canvas => { watermark = canvas; });

                domtoimage.toPng(absolute.childNodes[0], {
                    width: absolute.clientWidth,
                    height: absolute.clientHeight
                }).then(dataURL => { watermark_domtoimage = dataURL; });
            }
        }

        const url = location.href;
        idInterval = setInterval(() => {
            if (url != location.href) {
                clearInterval(idInterval);
                idInterval = 0;
                return;
            }

            const buttons = document.querySelector("div.flex.items-center.justify-center");
            if (!buttons) {
                clearInterval(idInterval);
                idInterval = 0;
                return;
            }

            const button = buttons.children[0];
            if (button.getAttribute("disabled") == null) {
                button.click();
                return;
            }

            clearInterval(idInterval);

            let pdf = null;
            idInterval = setInterval(() => {
                if (url != location.href) {
                    clearInterval(idInterval);
                    idInterval = 0;
                    return;
                }

                const buttons = document.querySelector("div.flex.items-center.justify-center");
                if (!buttons) {
                    clearInterval(idInterval);
                    idInterval = 0;
                    return;
                }

                const canvas = document.querySelector("canvas");
                if (!canvas) {
                    return;
                }

                const width = canvas.style.getPropertyValue("width");
                const height = canvas.style.getPropertyValue("height");
                const visibility = canvas.style.getPropertyValue("visibility");
                if (!width || !height || visibility) {
                    return;
                }

                if (watermark) {
                    if (watermark == true || !watermark_domtoimage) {
                        return;
                    }
                }

                if (!pdf) {
                    pdf = new jsPDF({
                        unit: "pt",
                        format: [parseInt(width), parseInt(height)]
                    });
                } else {
                    pdf.addPage();
                }
                pdf.addImage(canvas.toDataURL("image/png", 1.0), "PNG", 0, 0, parseInt(width), parseInt(height));
                if (watermark) {
                    const absolute = document.querySelector("div.absolute");
                    pdf.addImage(watermark.toDataURL("image/png", 1.0), "PNG", 0, 0, parseInt(width), watermark.height / watermark.width * parseInt(width));
                    pdf.addImage(watermark_domtoimage, "PNG", 0, 0, parseInt(width), absolute.clientHeight / absolute.clientWidth * parseInt(width));
                }

                const button = buttons.children[2];
                if (button.getAttribute("disabled") == null) {
                    button.click();
                    return;
                }

                clearInterval(idInterval);
                idInterval = 0;

                const node = document.querySelector("main")?.querySelector("h2");
                pdf.save(`${node ? node.innerText : location.pathname.split("/")[2]}.pdf`);
            }, milliseconds);
        }, milliseconds);
    }

    let url;
    let arrMenu = [];
    setInterval(() => {
        if (url != location.href) {
            url = location.href;

            for (const i in arrMenu) {
                GM_unregisterMenuCommand(arrMenu[i]);
            }
            arrMenu = [];

            if (location.pathname.startsWith("/preprints/")) {
                arrMenu.push(GM_registerMenuCommand("打开PDF", () => {
                    open(`https://files.shitjournal.org/${location.pathname.split("/")[2]}.pdf`);
                }));
                arrMenu.push(GM_registerMenuCommand("下载PDF", () => {
                    const node = document.querySelector("main")?.querySelector("h2");
                    GM_download({
                        url: `https://files.shitjournal.org/${location.pathname.split("/")[2]}.pdf`,
                        name: `${node ? node.innerText : location.pathname.split("/")[2]}.pdf`
                    });
                }));
                arrMenu.push(GM_registerMenuCommand("生成PDF", () => funcDownload(false)));
                arrMenu.push(GM_registerMenuCommand("生成PDF(水印)", () => funcDownload(true)));
            }
        }
    }, milliseconds);
})();