// ==UserScript==
// @name Luogu Tasks
// @namespace http://tampermonkey.net/
// @version 1.2
// @run-at document-start
// @description 在洛谷侧边栏显示题单与自己存的题
// @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 configs = {
set train(v) {
GM_setValue("problem-helper-local-train-list", v);
},
get train() {
return GM_getValue("problem-helper-local-train-list");
},
set local(v) {
GM_setValue("problem-helper-local-id", v);
},
get local() {
return GM_getValue("problem-helper-local-id");
},
set hideac(v) {
GM_setValue("problem-helper-hide-aced", v);
},
get hideac() {
return GM_getValue("problem-helper-hide-aced");
},
};
let local_problems;
let geticon = (pid, uid, 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}
${content}
`;
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 (
configs.hideac &&
task.userScore.status[id] &&
id != configs.local
)
return "";
return `
`;
};
let content = "",
float =
'删除';
if (id == configs.local)
(float = ""),
(local_problems = task.problems.map((u) => u.problem.pid));
for (let i in task.problems)
content += getList(i, id == configs.local);
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/${configs.local}`,
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 saveConfig = async (config) => {
return new Promise((r) => {
$.ajax({
type: "POST",
url: `https://www.luogu.com.cn/paste/new`,
beforeSend: function (request) {
request.setRequestHeader(
"x-csrf-token",
$("meta[name='csrf-token']")[0].content
);
},
contentType: "application/json;charset=UTF-8",
data: JSON.stringify({
public: false,
data: "#lgtsk" + JSON.stringify(config),
}),
success: () => r(),
});
});
};
let loadConfig = () => {
return new Promise((r) => {
$.get("https://www.luogu.com.cn/paste?_contentOnly").then((u) => {
u = u.currentData.pastes.result;
let nc = null;
for (let i in u) {
try {
if (u[i].data.substr(0, 6) !== "#lgtsk") continue;
let k = u[i].data;
nc = JSON.parse(k.substr(6, k.lentgh));
break;
} catch (e) {}
}
if (!nc) return r(0);
configs.train = nc.train;
configs.local = nc.local;
configs.hideac = nc.hideac;
r(1);
});
});
};
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 `
// `;
// };
// let content = "";
// for (let i in local) content += await getList(i);
// return renderList("本地列表", content, "");
// };
function renderItem(title, id) {
return `
`;
}
async function loaderProblemEntry() {
let lists = "";
let urls = configs.train || [];
// console.log(urls);
if (configs.local && !urls[configs.local]) urls[configs.local] = "本地列表";
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);
configs.hideac = u[0].checked;
unsafeWindow._feInstance.$swalToastSuccess("修改成功");
loaderProblemEntry();
});
$("#problem-helper-save").click((u) => {
saveConfig(configs);
unsafeWindow._feInstance.$swalToastSuccess("保存成功");
loaderProblemEntry();
});
$("#problem-helper-load").click(async (u) => {
if (await loadConfig())
unsafeWindow._feInstance.$swalToastSuccess("加载成功");
else unsafeWindow._feInstance.$swalToastError("加载失败");
loaderProblemEntry();
});
$("#problem-helper-set-local-list").keydown(function (e) {
if (e.keyCode == 13) {
configs.local = $("#problem-helper-set-local-list").val();
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 = configs.train;
let newone = {};
for (let i in now) if (i != r) newone[i] = now[i];
configs.train = now;
unsafeWindow._feInstance.$swalToastSuccess("删除成功");
loaderProblemEntry();
});
}
let deferredInjectProblemPage = () => {
$("#problem-helper-container").remove();
$(".side").prepend(
renderList(
"做题助手",
'',
'添加至列表'
)
);
loaderProblemEntry();
};
let loadTrainList = (id) => {
let local = configs.train;
let getList = (id) => {
return `
`;
};
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 = configs.train;
if (!now) now = {};
now[r] = unsafeWindow._feInjection.currentData.training.title;
configs.train = 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 = configs.train;
let newone = {};
for (let i in now) if (i != r) newone[i] = now[i];
configs.train = 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(
``
);
};