新建一个收藏夹,url 里面写下面这一坨,在编辑题目页面点收藏夹就会自动下载。
javascript:(async()=>{try{const pid=location.pathname.match(/^\/problem\/([^/]+)\/edit/)?.[1];if(!pid)throw Error("请先打开洛谷题目编辑页面");function extract(o){for(const x of[o,o?.data,o?.currentData,o?.data?.data,o?.currentData?.data]){const c=x?.problemDataSetting?.cases;if(Array.isArray(c)&&(!x.problem?.pid||String(x.problem.pid)===pid))return c;}return null;}let cases=null;const url="/problem/"+pid+"/edit?tab=testcase";for(const headers of[{"x-lentille-request":"content-only"},{"x-luogu-type":"content-only"},{}]){try{const r=await fetch(url,{credentials:"same-origin",cache:"no-store",headers});if(!r.ok)continue;const t=await r.text();let data;if(t.trimStart().startsWith("<")){const doc=new DOMParser().parseFromString(t,"text/html");const raw=doc.getElementById("lentille-context")?.textContent;if(!raw)continue;data=JSON.parse(raw);}else data=JSON.parse(t);cases=extract(data);if(cases)break;}catch(e){console.warn(e);}}if(!cases){const raw=document.getElementById("lentille-context")?.textContent;if(raw)cases=extract(JSON.parse(raw));}if(!cases?.length)throw Error("无法读取测试点,请先保存修改");const yaml=cases.map((c,i)=>{if(!Array.isArray(c)||typeof c[0]!=="string")throw Error("第 "+(i+1)+" 个测试点格式错误");const name=c[0].split("/").pop();const key=/^[A-Za-z0-9_.-]+$/.test(name)?name:JSON.stringify(name);const lines=[key+":"];for(const[field,index]of[["timeLimit",2],["memoryLimit",3],["score",4],["subtaskId",6]])if(c[index]!=null)lines.push(" "+field+": "+c[index]);return lines.join("\n");}).join("\n\n")+"\n";const blob=new Blob([yaml],{type:"text/yaml;charset=utf-8"});const objectUrl=URL.createObjectURL(blob);const a=document.createElement("a");a.href=objectUrl;a.download="config.yml";document.documentElement.appendChild(a);a.click();a.remove();setTimeout(()=>URL.revokeObjectURL(objectUrl),1000);console.log("成功生成 "+cases.length+" 个测试点");}catch(e){console.error(e);alert("生成失败:"+e.message);}})();