HaynesChen / XUJCOJ-CPH

// ==UserScript==
// @name         XUJCOJ-CPH
// @version      0.3
// @description  Competitive Companion For XUJCOJ
// @author       Haynes-ROB21026
// @match        *://xujcoj.com/home/contest/*/problem/*
// @match        *://xujcoj.com/home/problem/detail/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=xujcoj.com
// @license MIT
// ==/UserScript==

(function () {
    "use strict";
    var button = document.createElement("button");
    var li = document.createElement("li");
    var img = document.createElement("img");
    img.src =
        "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAC9FJREFUeF7tnWGS3CgMRvFJdnOS3ZwkyUmSnGSTk+zcJMlJvMWUO+uZaTcgBAjx+tdUjcHmk54RIMMW+KEAClwqsKENCqDAtQIAgnegwAMFAAT3QAEAwQdQQKYAPYhMN0otogCALGJomilTAEBkulFqEQUAZBFD00yZAgAi041SiygAIIsYmmbKFAAQmW6UWkQBAFnE0DRTpgCAyHSj1CIKAMgihqaZMgUARKYbpRZRAEAWMTTNlCkAIDLdKLWIAgCyiKFppkwBAJHpRqlFFACQRQxNM2UKAIhMNxel9n3/M4Tw4WjMrxDCz23bnlw0TqkRAKIk5EzV7Pv+OYTw5eKZf4YQIiRft22Lfy/9A5CFzH/0GP+EEP7OaDaghBAAJMNTPFxSCMe5yTdQvq8YfgGIB+9PtOGA44dCU7+FEJYCBUAUvMZyFfu+x3DqX+VnXAYUAFH2HEvVNYLjdfj1yXPoBSCWPFrxWTrAsQQoAKLolFaq2vf9Ywghzlb1/sUBfZwejiGYix+AuDDj/41IrHH0aq0bUACkl8t0uM++77HXiL2Hld/0oACIFVeqfI593+NMVc4CYOWdRMUjKN+2bfsqKj2wEIAMFF/r1sbheD2YnwoUANHy0kH1TATHlKAAyCDHrr1tRepI7a01y5sPvQBE09yd6jrgiGOOmK7u4WcWFACZzL0U86osttwcKABi0U0unqnz6vhIZZ5BORIjh36TAiAj3aDg3gvBYWowDyAFTjrq0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sw0UqoAgEiVo9wSCgDIEmamkVIFAESqHOWWUOA/onS7FGS+ahIAAAAASUVORK5CYII=";
    img.style.width = "20px";
    img.style.height = "20px";
    img.style.marginTop = "-2px";

    button.textContent = " CPH";
    button.style.width = "75px";
    button.style.height = "28px";
    button.style.align = "center";
    button.style.color = "white";
    button.style.background = "#5bc0de";
    button.style.border = "1px solid #46b8da";
    button.style.borderRadius = "4px";
    button.style.marginTop = "11px";
    button.style.fontFamily = "sans-serif";

    var elements = document.getElementsByClassName("title");
    var titleContent = elements[0].innerText;
    var p_input = document.getElementById("copyTarget").innerText;
    var p_output = document.getElementById("copyTarget2").innerText;
    p_input = makeInput(p_input);
    p_output = makeInput(p_output);

    var panel = document.querySelector(".panel");
    var h5 = panel.querySelector("h5");
    var time_mem = h5.innerText;
    var currentUrl = window.location.href;
    var time = time_mem.match(/\d+/g)[0];
    var mem = time_mem.match(/\d+/g)[1];

    console.log(currentUrl);
    console.log(titleContent);
    console.log(time);
    console.log(mem);
    console.log(p_input);
    console.log(p_output);

    button.addEventListener("click", clickBotton);
    function clickBotton() {
        setTimeout(function () {
            var url = "http://127.0.0.1:27121";
            var data = JSON.stringify({
                name: titleContent,
                url: currentUrl,
                interactive: false,
                memoryLimit: mem / 1024,
                timeLimit: time * 1000,
                tests: [
                    {
                        input: p_input,
                        output: p_output,
                    },
                ],
                testType: "single",
                input: {
                    type: "stdin",
                },
                output: {
                    type: "stdout",
                },
            });

            fetch(url, {
                method: "POST",
                body: data,
            }).then(function () {
                console.log(["Send CPH Success"]);
            });
        }, 100);
    }

    function makeInput(inputStr) {
        if (inputStr[inputStr.length - 1] != "\n") {
            inputStr += "\n";
        }
        inputStr = inputStr.replace(/\xA0/g, " "); //题号2355的输入中有特殊空格,要替换掉
        return inputStr.replace(/ /g, " ").replace(/\n{1,3}/g, "\n");
    }

    var navbar = document.getElementsByClassName("navbar-right")[0];
    li.appendChild(button);
    navbar.insertBefore(li, navbar.childNodes[0]);
    button.insertBefore(img, button.childNodes[0]);
})();