Shit Journal Download

Download as PDF.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램을 설치해야 합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 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://greasyfork.org/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);
})();