// ==UserScript==
// @name Luogu Tasks
// @namespace http://tampermonkey.net/
// @version 2.4
// @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==
// == define config ==
const inject_lantency = 500; // in ms
// configs store in browser
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") || null;
},
set hide_ac(v) {
GM_setValue("problem-helper-hide-aced", v);
},
get hide_ac() {
return GM_getValue("problem-helper-hide-aced") || false;
},
};
// == end config ==
// == lib functions ==
// get task info
function getTask(id) {
return new Promise((r) => {
$.get(`https://www.luogu.com.cn/training/${id}?_contentOnly=any`).then(
(u) => r(u.currentData.training)
);
});
}
// save train list
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(),
});
});
};
// save config
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(),
});
});
};
// load config
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.hide_ac = nc.hide_ac;
r(1);
});
});
};
// == end lib functions ==
$(document).ready(async () => {
// == hook pushState ==
var _wr = (type) => {
var orig = history[type];
return function () {
var e = new Event(type);
e.arguments = arguments;
window.dispatchEvent(e);
var rv = orig.apply(this, arguments);
return rv;
};
};
history.pushState = _wr("pushState");
history.replaceState = _wr("replaceState");
let local_problems;
// == get verdict icon ==
let geticon = (pid, uid, your_score, full_score, is_aced = null) => {
let res;
let ua = (uid, pid) =>
``;
let ac = (uid, pid) =>
``;
let nt = (uid, pid) =>
``;
if (your_score == full_score || is_aced) res = ac(uid, pid);
else if (your_score == null) res = nt(uid, pid);
else res = ua(uid, pid);
return `${res}`;
};
// == render one list ==
let renderList = (title, content, float, id = "") =>
`
${float}
${title}
${content}
隐藏列表 查看列表
`;
let loadProblemList = async (id) => {
let task;
let getList = (x, is_local) => {
let id = task.problems[x].problem.pid;
if (configs.hide_ac && task.userScore.status[id] && id != configs.local)
return "";
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 = async () => {
local_problems = (await getTask(configs.local)).problems.map(
(u) => u.problem.pid
);
$(".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");
let now = local_problems;
if (now.indexOf(r) == -1) now.push(r);
saveTrain(now);
unsafeWindow._feInstance.$swalToastSuccess("添加成功");
});
};
/* main controller */
function 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();
}
window.addEventListener("pushState", function (e) {
setTimeout(inject, inject_lantency);
});
window.addEventListener("replaceState", function (e) {
setTimeout(inject, inject_lantency);
});
setTimeout(inject, 100);
$(document.body).append(
``
);
});