// ==UserScript== // @name Luogu Tasks // @namespace http://tampermonkey.net/ // @version 1.0 // @run-at document-start // @description qwq // @author __OwO__ // @match https://www.luogu.com.cn/* // @grant GM_setValue // @grant GM_getValue // @grant unsafeWindow // @require https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js // @require https://cdn.staticfile.org/sweetalert/2.1.2/sweetalert.min.js // @downloadURL none // ==/UserScript== window.onload = async () => { const inject_lantency = 500; let local_list = GM_getValue("problem-helper-local-id"); let local_problems; let geticon = (uid, pid, scr, fscr, stat = null) => { let res; let ua = (uid, pid) => ``; let ac = (uid, pid) => ``; let nt = (uid, pid) => ``; if (scr == fscr || stat) res = ac(uid, pid); else if (scr == null) res = nt(uid, pid); else res = ua(uid, pid); return ( `` + res + `` ); }; let renderList = (title, content, float) => `
${float}

${title}

查看列表
`; let loadProblemList = async (id) => { return new Promise((res, rej) => { $.get(`https://www.luogu.com.cn/training/${id}?_contentOnly=any`).then( (task) => { task = task.currentData.training; let getList = (x, is_local) => { let id = task.problems[x].problem.pid; if ( GM_getValue("problem-helper-hide-aced") == 1 && task.userScore.status[id] && id != local_list ) return ""; return `
${geticon( id, task.userScore.user.uid, task.userScore.score[id], task.problems[x].problem.fullScore, task.userScore.status[id] )} ${ is_local ? ` ` : "" } ${id} ${task.problems[x].problem.title}
`; }; let content = "", float = '删除'; if (id == local_list) (float = ""), (local_problems = task.problems.map((u) => u.problem.pid)); for (let i in task.problems) content += getList(i, id == local_list); res(renderList(task.title, content, float)); } ); }); }; let saveTrain = async (problems) => { return new Promise((r) => { $.ajax({ type: "POST", url: `https://www.luogu.com.cn/api/training/editProblems/${local_list}`, beforeSend: function (request) { request.setRequestHeader( "x-csrf-token", $("meta[name='csrf-token']")[0].content ); }, contentType: "application/json;charset=UTF-8", data: JSON.stringify({ pids: problems }), success: () => r(), }); }); }; let getScore = async (sett) => { if (sett.method == "train") return { uid: sett.task.userScore.user.uid, scr: sett.task.userScore.score[sett.id], fscr: sett.task.problems[sett.id].problem.fullScore, }; else if (sett.method == "local") return new Promise((res, rej) => { $.get(`https://www.luogu.com.cn/problem/${sett.id}?_contentOnly`).then( (u) => { res({ uid: u.currentUser.uid, scr: u.currentData.problem.score, fscr: u.currentData.problem.fullScore, }); } ); }); }; // let loadLocalProblem = async () => { // let local = GM_getValue("problem-helper-local-list"); // let getList = async (id, opt = 1) => { // let h = ""; // if (opt) { // let info = await getScore({ method: "local", id: id }); // h = geticon(info.uid, id, info.scr, info.fscr); // } // return ` //
//
// ${h} // // // // 删除 // // // // // ${id} ${local[id]} // //
//
`; // }; // let content = ""; // for (let i in local) content += await getList(i); // return renderList("本地列表", content, ""); // }; function renderItem(title, id) { return `
${geticon( id, task.userScore.user.uid, task.userScore.score[id], task.problems[x].problem.fullScore, task.userScore.status[id] )} ${id} ${task.problems[x].problem.title}
`; } async function loaderProblemEntry() { let lists = ""; let urls = GM_getValue("problem-helper-local-train-list") || []; // console.log(urls); if (local_list && !urls[local_list]) urls[local_list] = "本地列表"; for (let i in urls) lists += await loadProblemList(i); lists += renderList( "设置", `
隐藏已通过题目
`, "" ); $("#problem-helper-entry").html(lists); $(".problem-helper-fold-on").click((u) => { u = $(u.target); u.parent().prev(".problem-helper-inner").removeAttr("style"); u.attr("style", "display:none;"); u.prev(".problem-helper-fold-off").removeAttr("style"); }); $(".problem-helper-fold-off").click((u) => { u = $(u.target); u.parent().prev(".problem-helper-inner").attr("style", "display:none;"); u.attr("style", "display:none;"); u.next(".problem-helper-fold-on").removeAttr("style"); }); $("#problem-helper-hide-aced").click((u) => { u = $(u.target); GM_setValue("problem-helper-hide-aced", u[0].checked); unsafeWindow._feInstance.$swalToastSuccess("修改成功"); loaderProblemEntry(); }); $("#problem-helper-set-local-list").keydown(function (e) { if (e.keyCode == 13) { let v = $("#problem-helper-set-local-list").val(); GM_setValue("problem-helper-local-id", v.trim()); local_list = GM_getValue("problem-helper-local-id"); unsafeWindow._feInstance.$swalToastSuccess("修改成功"); loaderProblemEntry(); } }); $("#problem-helper-add-to-list").click((u) => { let r = unsafeWindow.location.href.split("/"); r = r[r.length - 1]; while (r[r.length - 1] == "#") r = r.slice(0, r.length - 1); if (!unsafeWindow._feInjection.currentData.problem.title) return; if (local_problems.indexOf(r) == -1) local_problems.push(r); saveTrain(local_problems); unsafeWindow._feInstance.$swalToastSuccess("添加成功"); loaderProblemEntry(); }); $(".problem-helper-delete-from-list").click((u) => { u = $(u.target).parents(".problem-helper-delete-from-list"); let r = u.attr("data"); let now = local_problems; let newone = []; for (let i in now) if (now[i] != r) newone[i] = now[i]; saveTrain(newone); unsafeWindow._feInstance.$swalToastSuccess("删除成功"); loaderProblemEntry(); }); $(".problem-helper-train-remove").click((u) => { u = $(u.target); let r = u.attr("data"); let now = GM_getValue("problem-helper-local-train-list"); let newone = {}; for (let i in now) if (i != r) newone[i] = now[i]; GM_setValue("problem-helper-local-train-list", newone); unsafeWindow._feInstance.$swalToastSuccess("删除成功"); loaderProblemEntry(); }); } let deferredInjectProblemPage = () => { $("#problem-helper-container").remove(); $(".side").prepend( renderList( "做题助手", '
', '添加至列表' ) ); loaderProblemEntry(); }; let loadTrainList = (id) => { let local = GM_getValue("problem-helper-local-train-list"); let getList = (id) => { return `
${id} ${local[id]}
`; }; let lists = ""; for (let i in local) lists += getList(i); return lists; }; let loaderTrainEntry = async () => { let lists = loadTrainList(); $("#problem-helper-entry").html(lists); $(".problem-helper-fold-on").click((u) => { u = $(u.target); u.parent().prev(".problem-helper-inner").removeAttr("style"); u.attr("style", "display:none;"); u.prev(".problem-helper-fold-off").removeAttr("style"); }); $(".problem-helper-fold-off").click((u) => { u = $(u.target); u.parent().prev(".problem-helper-inner").attr("style", "display:none;"); u.attr("style", "display:none;"); u.next(".problem-helper-fold-on").removeAttr("style"); }); $("#problem-helper-add-to-list").click((u) => { let r = unsafeWindow.location.href.split("/"); r = r[r.length - 1].split("#")[0]; if (!unsafeWindow._feInjection.currentData.training.title) return; let now = GM_getValue("problem-helper-local-train-list"); if (!now) now = {}; now[r] = unsafeWindow._feInjection.currentData.training.title; GM_setValue("problem-helper-local-train-list", now); unsafeWindow._feInstance.$swalToastSuccess("添加成功"); loaderTrainEntry(); }); $(".problem-helper-delete-from-list").click((u) => { u = $(u.target).parents(".problem-helper-delete-from-list"); let r = u.attr("data"); let now = GM_getValue("problem-helper-local-train-list"); let newone = {}; for (let i in now) if (i != r) newone[i] = now[i]; GM_setValue("problem-helper-local-train-list", newone); unsafeWindow._feInstance.$swalToastSuccess("删除成功"); loaderProblemEntry(); }); }; let deferredInjectTrainPage = () => { $("#problem-helper-container").remove(); $(".side").prepend( renderList( "做题助手", '
', '添加至列表' ) ); loaderTrainEntry(); }; let deferredInjectProblemlist = () => { $(".problem-helper-inlist-adder").remove(); let pid = "", name = ""; let h = (pid, name) => ` 添加 `; let rows = $(".row"); let trim = (s) => { return s.replace(/(^\s*)|(\s*$)/g, ""); }; rows.each((u) => { u = $(rows[u]); pid = u.children(".pid").text(); name = u.children(".title").children(".title").text(); name = trim(name); u.children(".title").prepend(h(pid, name)); }); $(".problem-helper-inlist-adder").click((u) => { u = $(u.target); let r = u.attr("data"), s = u.attr("name"); let now = GM_getValue("problem-helper-local-list"); if (!now) now = {}; now[r] = s; GM_setValue("problem-helper-local-list", now); unsafeWindow._feInstance.$swalToastSuccess("添加成功"); }); }; /* main controller */ let inject = () => { if ( unsafeWindow.location.href.includes( "problem/list" ) /*deferredInjectProblemlist();*/ ); else if (unsafeWindow.location.href.includes("training")) deferredInjectTrainPage(), deferredInjectProblemlist(); else if (unsafeWindow.location.href.includes("problem")) deferredInjectProblemPage(); }; $(".entry").click(() => setTimeout(inject, inject_lantency)); $("a").click(() => setTimeout(inject, inject_lantency)); $("button").click(() => setTimeout(inject, inject_lantency)); setTimeout(inject, 100); $(document.body).append( `` ); };