CPH for 小视野

小视野 CPH 拓展

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         CPH for 小视野
// @namespace    http://lkyo.top/
// @version      2024-08-16
// @description  小视野 CPH 拓展
// @author       xhze
// @match        http://gdgzoi.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=gdgzoi.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    console.log('run');
    const geti=(x,document)=>{
        return document.evaluate(x, document, null, XPathResult.ANY_TYPE, null);
    }
    function get(x,document) {
        return geti(x,document).iterateNext();
    }
    let center = get("/html/body/div/div[3]/section/div/div/div/div/center[1]",document);
    center.innerHTML += ' <a href="javascript:postToCph()" class="btn btn-primary" role="button"> Throw to CPH </a>';

    window.postToCph = async() => {
        if(window.location.toString().indexOf('contest')==-1) {
            sendProblem(parseProblemFromDom(window.location.toString(), document));
        }else {
            let c=geti("//a[starts-with(@href,'problem.php')]", document);
            let d=c.iterateNext();
            let dp = new DOMParser();
            let ls = []
            while(d) {
                ls.push(d);
                d=c.iterateNext();
            }
            ls.reverse();
            for(d of ls) {
                let html = await(await fetch(d.href)).text();
                let g=dp.parseFromString(html, 'text/html');
                console.log('ok');
                sendProblem(parseProblemFromDom(d.href,g));
            }
        }
        function sendProblem(problem) {
            console.log(problem);
            return fetch('http://localhost:27121', {
                method: "POST", mode: "no-cors", body: JSON.stringify(problem)
            });
        }

        function parseProblemFromDom(url, document) {
            let center = get("/html/body/div/div[3]/section/div/div/div/div/center[1]",document);
            let inp = get("/html/body/div/div[3]/section/div/div/div/div/div[4]/div[1]/ul/pre",document).innerText.replaceAll(/【.+】/g, "@(*&)").split("@(*&)").map(e => e.trim()).filter(e => e);
            let ans = get("/html/body/div/div[3]/section/div/div/div/div/div[4]/div[2]/ul/pre",document).innerText.replaceAll(/【.+】/g, "@(*&)").split("@(*&)").map(e => e.trim()).filter(e => e);
            return {
                name:'horizon_P' + url.match(/(?<=cid=)(\d+)|(?<=pid=)(\d+)/g).join('_'),
                url,
                interactive: false,
                memoryLimit: center.innerText.match(/(\d+)(?= MB)/g)[0] * 1024 * 1024,
                timeLimit: center.innerText.match(/(\d+)(?= Sec)/g)[0] * 1000,
                group: "asd",
                tests: inp.map((e, i) => ({
                    input: e,
                    output: ans[i],
                    id: i,
                })),
                srcPath: "",
                local: true
            }
        }
    };
})();