// ==UserScript== // @name Luogu Plus VScode CPH // @namespace http://tampermonkey.net/ // @version 0.1.0 // @author Andy_hpy // @icon https://www.luogu.com.cn/favicon.ico // @description 将Luogu题目测试用例发送到VS Code的CPH插件 // @match *://www.luogu.com.cn/* // @grant GM_xmlhttpRequest // @grant GM_addStyle // @connect 127.0.0.1 // @connect localhost // @require https://scriptcat.org/lib/5444/0.1.1/CPH%20API%E5%BA%93.js?sha384-jmi4a0QQDOtVuAdlWNOyhgKoJ7g8POo2oUPJX+c+BTBXz0cv2HUlk/1SDkGcti2s // ==/UserScript== cph = new CPH(27121); async function getSample() { let sample = [], sampleIn = [], sampleOut = []; document.querySelectorAll("div.io-sample > div.io-sample-block:nth-child(1) > pre.lfe-code").forEach((pre) => { sampleIn.push(pre.textContent.trim()); }); document.querySelectorAll("div.io-sample > div.io-sample-block:nth-child(2) > pre.lfe-code").forEach((pre) => { sampleOut.push(pre.textContent.trim()); }); for (let i = 0; i < sampleIn.length; i++) { sample.push({ 'input': sampleIn[i], 'output': sampleOut[i] }); } return sample; } async function getData() { const url = location.href; const group = 'Luogu'; const name = 'Luogu - ' + url.match(/\/problem\/([a-zA-Z0-9_]+)/)[1]; const mem = parseFloat(document.querySelector("div.stat.stacked.with-vr.color-inv > div.field:nth-child(4) > span.stat-text.value").textContent); const time = parseFloat(document.querySelector("div.stat.stacked.with-vr.color-inv > div.field:nth-child(3) > span.stat-text.value").textContent) * 1000; const test = await getSample(); const data = { name: name, group: group, tests: test.map((t, idx) => ({ ...t, id: idx })), url: url, memoryLimit: mem, timeLimit: time, testType: "single", input: { type: "stdin" }, output: { type: "stdout" }, languages: { java: { mainClass: "Main", taskClass: "" } }, batch: { id: cph.randomUUID(), size: 1 } }; return data; } async function addbtn() { const btn = document.createElement('button'); btn.href = 'javascript:void(0)'; btn.innerHTML = '传送至CPH'; btn.className = 'solid lform-size-middle'; btn.id = 'cgh-to-cph'; btn.style.paddingLeft = btn.style.paddingRight = '9px'; btn.setAttribute('data-v-505b6a97', ''); btn.setAttribute('data-v-f265fec6-s', ''); btn.addEventListener('click', async () => { const data = await getData(); console.log(data); console.log('Data to send:', JSON.stringify(data, null, 2)); const response = await cph.send(data); if (response.status !== 200) { alert(response.message); } }); document.querySelector('div.columba-content-wrap.header-layout > div > div:nth-child(1)').appendChild(btn); } function main() { if (location.href.includes("problem") && document.querySelectorAll("div.io-sample").length > 0) { addbtn(); } } async function waitHtmlAndWorkMain() { const wait = setInterval(async () => { const place = document.querySelector("html"); if (place) { clearInterval(wait); main(); } }, 500); } waitHtmlAndWorkMain();