Shit Journal Download

Download as PDF.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, Greasemonkey alebo Violentmonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey, % alebo Violentmonkey.

Na nainštalovanie skriptu si budete musieť nainštalovať rozšírenie, ako napríklad Tampermonkey alebo Userscripts.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie, ako napríklad Tampermonkey.

Na inštaláciu tohto skriptu je potrebné nainštalovať rozšírenie správcu používateľských skriptov.

(Už mám správcu používateľských skriptov, nechajte ma ho nainštalovať!)

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie, ako napríklad Stylus.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

Na inštaláciu tohto štýlu je potrebné nainštalovať rozšírenie správcu používateľských štýlov.

(Už mám správcu používateľských štýlov, nechajte ma ho nainštalovať!)

// ==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);
})();