Shit Journal Download

Download as PDF.

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

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