Shit Journal Download

Download as PDF.

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

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