Luogu Tasks

在洛谷侧边栏显示题单与自己存的题

As of 2021-05-31. See the latest version.

  1. // ==UserScript==
  2. // @name Luogu Tasks
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.4.1
  5. // @run-at document-start
  6. // @description 在洛谷侧边栏显示题单与自己存的题
  7. // @author __OwO__
  8. // @match https://www.luogu.com.cn/*
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @grant unsafeWindow
  12. // @require https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js
  13. // @require https://cdn.staticfile.org/sweetalert/2.1.2/sweetalert.min.js
  14. // ==/UserScript==
  15.  
  16. // == define config ==
  17.  
  18. const inject_lantency = 300; // in ms
  19.  
  20. // configs store in browser
  21. let configs = {
  22. set train(v) {
  23. GM_setValue("problem-helper-local-train-list", v);
  24. },
  25. get train() {
  26. return GM_getValue("problem-helper-local-train-list") || [];
  27. },
  28. set local(v) {
  29. GM_setValue("problem-helper-local-id", v);
  30. },
  31. get local() {
  32. return GM_getValue("problem-helper-local-id") || null;
  33. },
  34. set hide_ac(v) {
  35. GM_setValue("problem-helper-hide-aced", v);
  36. },
  37. get hide_ac() {
  38. return GM_getValue("problem-helper-hide-aced") || false;
  39. },
  40. };
  41.  
  42. // == end config ==
  43.  
  44. // == lib functions ==
  45.  
  46. // get task info
  47. function getTask(id) {
  48. return new Promise((r) => {
  49. $.get(`https://www.luogu.com.cn/training/${id}?_contentOnly=any`).then(
  50. (u) => r(u.currentData.training)
  51. );
  52. });
  53. }
  54.  
  55. // save train list
  56. let saveTrain = async (problems) => {
  57. return new Promise((r) => {
  58. $.ajax({
  59. type: "POST",
  60. url: `https://www.luogu.com.cn/api/training/editProblems/${configs.local}`,
  61. beforeSend: function (request) {
  62. request.setRequestHeader(
  63. "x-csrf-token",
  64. $("meta[name='csrf-token']")[0].content
  65. );
  66. },
  67. contentType: "application/json;charset=UTF-8",
  68. data: JSON.stringify({ pids: problems }),
  69. success: () => r(),
  70. });
  71. });
  72. };
  73.  
  74. // save config
  75. let saveConfig = async (config) => {
  76. return new Promise((r) => {
  77. $.ajax({
  78. type: "POST",
  79. url: `https://www.luogu.com.cn/paste/new`,
  80. beforeSend: function (request) {
  81. request.setRequestHeader(
  82. "x-csrf-token",
  83. $("meta[name='csrf-token']")[0].content
  84. );
  85. },
  86. contentType: "application/json;charset=UTF-8",
  87. data: JSON.stringify({
  88. public: false,
  89. data: "#lgtsk" + JSON.stringify(config),
  90. }),
  91. success: () => r(),
  92. });
  93. });
  94. };
  95.  
  96. // load config
  97. let loadConfig = () => {
  98. return new Promise((r) => {
  99. $.get("https://www.luogu.com.cn/paste?_contentOnly").then((u) => {
  100. u = u.currentData.pastes.result;
  101. let nc = null;
  102. for (let i in u) {
  103. try {
  104. if (u[i].data.substr(0, 6) !== "#lgtsk") continue;
  105. let k = u[i].data;
  106. nc = JSON.parse(k.substr(6, k.lentgh));
  107. break;
  108. } catch (e) {}
  109. }
  110. if (!nc) return r(0);
  111. configs.train = nc.train;
  112. configs.local = nc.local;
  113. configs.hide_ac = nc.hide_ac;
  114. r(1);
  115. });
  116. });
  117. };
  118.  
  119. // == end lib functions ==
  120.  
  121. $(document).ready(async () => {
  122. let local_problems;
  123.  
  124. // == get verdict icon ==
  125. let geticon = (pid, uid, your_score, full_score, is_aced = null) => {
  126. let res;
  127. let ua = (uid, pid) =>
  128. `<a data-v-303bbf52="" data-v-357e29e4="" href="/record/list?pid=${pid}&amp;user=${uid}" target="_blank" colorscheme="default" class="color-default" data-v-83961efe="" style="color: inherit; float:left; padding-right: 0.5em;"><svg data-v-1b44b3e6="" data-v-357e29e4="" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="times" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 352 512" class="icon svg-inline--fa fa-times fa-w-11" data-v-303bbf52="" style="transform: scale(1.2); color: rgb(231, 76, 60);"><path data-v-1b44b3e6="" fill="currentColor" d="M242.72 256l100.07-100.07c12.28-12.28 12.28-32.19 0-44.48l-22.24-22.24c-12.28-12.28-32.19-12.28-44.48 0L176 189.28 75.93 89.21c-12.28-12.28-32.19-12.28-44.48 0L9.21 111.45c-12.28 12.28-12.28 32.19 0 44.48L109.28 256 9.21 356.07c-12.28 12.28-12.28 32.19 0 44.48l22.24 22.24c12.28 12.28 32.2 12.28 44.48 0L176 322.72l100.07 100.07c12.28 12.28 32.2 12.28 44.48 0l22.24-22.24c12.28-12.28 12.28-32.19 0-44.48L242.72 256z" class=""></path></svg></a>`;
  129. let ac = (uid, pid) =>
  130. `<a data-v-303bbf52="" data-v-357e29e4="" href="/record/list?pid=${pid}&amp;user=${uid}" target="_blank" colorscheme="default" class="color-default" data-v-83961efe="" style="color: inherit; float:left; padding-right: 0.5em;"><svg data-v-1b44b3e6="" data-v-357e29e4="" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="check" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512" class="icon svg-inline--fa fa-check fa-w-16" data-v-303bbf52="" style="color: rgb(82, 196, 26);"><path data-v-1b44b3e6="" fill="currentColor" d="M173.898 439.404l-166.4-166.4c-9.997-9.997-9.997-26.206 0-36.204l36.203-36.204c9.997-9.998 26.207-9.998 36.204 0L192 312.69 432.095 72.596c9.997-9.997 26.207-9.997 36.204 0l36.203 36.204c9.997 9.997 9.997 26.206 0 36.204l-294.4 294.401c-9.998 9.997-26.207 9.997-36.204-.001z" class=""></path></svg></a>`;
  131. let nt = (uid, pid) =>
  132. `<a data-v-303bbf52="" data-v-357e29e4="" href="/record/list?pid=${pid}&amp;user=${uid}" target="_blank" colorscheme="default" class="color-default" data-v-83961efe="" style="color: inherit; float:left; padding-right: 0.5em;"><svg data-v-1b44b3e6="" data-v-357e29e4="" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="minus" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="icon svg-inline--fa fa-minus fa-w-14" data-v-303bbf52="" style="opacity: 0.7;"><path data-v-1b44b3e6="" fill="currentColor" d="M416 208H32c-17.67 0-32 14.33-32 32v32c0 17.67 14.33 32 32 32h384c17.67 0 32-14.33 32-32v-32c0-17.67-14.33-32-32-32z" class=""></path></svg></a>`;
  133. if (your_score == full_score || is_aced) res = ac(uid, pid);
  134. else if (your_score == null) res = nt(uid, pid);
  135. else res = ua(uid, pid);
  136. return `<span data-v-3fb75f36="" style="font-weight: bold;">${res}</span>`;
  137. };
  138.  
  139. // == render one list ==
  140. let renderList = (title, content, float, id = "") =>
  141. `
  142. <div
  143. data-v-796309f8=""
  144. data-v-3fb75f36=""
  145. class="card padding-default problem-helper-container"
  146. data-v-6febb0e8=""
  147. id="list-${id}"
  148. >
  149. <div id="list-float-${id}">${float}</div>
  150. <h4 data-v-3fb75f36="" data-v-796309f8="" class="lfe-h4" >${title}</h4>
  151. <div class="problem-helper-inner" style="display:none;">
  152. <div id="list-content-${id}">${content}</div>
  153. </div>
  154. <div data-v-e4b7c2ca="" data-v-3fb75f36="" class="expand-tip lfe-caption" data-v-796309f8="" >
  155. <span class="problem-helper-fold-off" style="display:none;" data-v-e4b7c2ca=""><svg data-v-e4b7c2ca="" aria-hidden="true" focusable="false" data-prefix="fal" data-icon="chevron-up" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="svg-inline--fa fa-chevron-up fa-w-14" ><path data-v-e4b7c2ca="" fill="currentColor" d="M4.465 366.475l7.07 7.071c4.686 4.686 12.284 4.686 16.971 0L224 178.053l195.494 195.493c4.686 4.686 12.284 4.686 16.971 0l7.07-7.071c4.686-4.686 4.686-12.284 0-16.97l-211.05-211.051c-4.686-4.686-12.284-4.686-16.971 0L4.465 349.505c-4.687 4.686-4.687 12.284 0 16.97z" class=""></path></svg> 隐藏列表</span>
  156. <span class="problem-helper-fold-on" data-v-e4b7c2ca=""><svg data-v-e4b7c2ca="" aria-hidden="true" focusable="false" data-prefix="fal" data-icon="chevron-down" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="svg-inline--fa fa-chevron-down fa-w-14"><path data-v-e4b7c2ca="" fill="currentColor" d="M443.5 162.6l-7.1-7.1c-4.7-4.7-12.3-4.7-17 0L224 351 28.5 155.5c-4.7-4.7-12.3-4.7-17 0l-7.1 7.1c-4.7 4.7-4.7 12.3 0 17l211 211.1c4.7 4.7 12.3 4.7 17 0l211-211.1c4.8-4.7 4.8-12.3.1-17z" class=""></path></svg> 查看列表</span>
  157. </div>
  158. </div>
  159. `;
  160. let loadProblemList = async (id) => {
  161. let task;
  162. let getList = (x, is_local) => {
  163. let id = task.problems[x].problem.pid;
  164. if (configs.hide_ac && task.userScore.status[id] && id != configs.local)
  165. return "";
  166. return `
  167. <div>
  168. <div>
  169. <span data-v-3a151854="">
  170. <a
  171. data-v-303bbf52=""
  172. data-v-3fb75f36=""
  173. href="/record/list?pid=${id}&amp;user=${task.userScore.user.uid}"
  174. class="color-default"
  175. style="text-decoration: none;"
  176. >
  177. ${geticon(
  178. id,
  179. task.userScore.user.uid,
  180. task.userScore.score[id],
  181. task.problems[x].problem.fullScore,
  182. task.userScore.status[id]
  183. )}
  184. </a>
  185. ${
  186. is_local
  187. ? `
  188. <a
  189. data-v-3fb75f36=""
  190. style="float: right;font-weight: light;"
  191. class="problem-helper-delete-from-list"
  192. data="${id}"
  193. >
  194. <svg data-v-4121e124="" aria-hidden="true" focusable="false" data-prefix="fal" data-icon="times" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" class="svg-inline--fa fa-times fa-w-10"><path data-v-4121e124="" fill="currentColor" d="M193.94 256L296.5 153.44l21.15-21.15c3.12-3.12 3.12-8.19 0-11.31l-22.63-22.63c-3.12-3.12-8.19-3.12-11.31 0L160 222.06 36.29 98.34c-3.12-3.12-8.19-3.12-11.31 0L2.34 120.97c-3.12 3.12-3.12 8.19 0 11.31L126.06 256 2.34 379.71c-3.12 3.12-3.12 8.19 0 11.31l22.63 22.63c3.12 3.12 8.19 3.12 11.31 0L160 289.94 262.56 392.5l21.15 21.15c3.12 3.12 8.19 3.12 11.31 0l22.63-22.63c3.12-3.12 3.12-8.19 0-11.31L193.94 256z" class=""></path></svg>
  195. </a>
  196. `
  197. : ""
  198. }
  199. </span>
  200. <a
  201. class="colored problem-helper-text"
  202. style="padding-left: 3px"
  203. href="/problemnew/show/${id}"
  204. target="_blank"
  205. >
  206. <b>${id}</b> ${task.problems[x].problem.title}
  207. </a>
  208. </div>
  209. </div>`;
  210. };
  211. async function loadContent(id) {
  212. task = await getTask(id);
  213. let content = "";
  214. if (id == configs.local) {
  215. $(`#list-float-${id}`).text("");
  216. local_problems = task.problems.map((u) => u.problem.pid);
  217. }
  218. for (let i in task.problems) content += getList(i, id == configs.local);
  219. $(`#list-content-${id}`).html(content);
  220. $(`#list-${id} > h4`).text(task.title);
  221. }
  222. loadContent(id);
  223. return renderList(
  224. "...",
  225. "...",
  226. '<a style="float:right;" data="${task.id}" data-v-303bbf52="" data-v-3fb75f36="" href="javascript:void 0" class="problem-helper-train-remove color-default">删除</a>',
  227. id
  228. );
  229. };
  230. let getScore = async (sett) => {
  231. if (sett.method == "train")
  232. return {
  233. uid: sett.task.userScore.user.uid,
  234. scr: sett.task.userScore.score[sett.id],
  235. fscr: sett.task.problems[sett.id].problem.fullScore,
  236. };
  237. else if (sett.method == "local")
  238. return new Promise((res, rej) => {
  239. $.get(`https://www.luogu.com.cn/problem/${sett.id}?_contentOnly`).then(
  240. (u) => {
  241. res({
  242. uid: u.currentUser.uid,
  243. scr: u.currentData.problem.score,
  244. fscr: u.currentData.problem.fullScore,
  245. });
  246. }
  247. );
  248. });
  249. };
  250. // let loadLocalProblem = async () => {
  251. // let local = GM_getValue("problem-helper-local-list");
  252. // let getList = async (id, opt = 1) => {
  253. // let h = "";
  254. // if (opt) {
  255. // let info = await getScore({ method: "local", id: id });
  256. // h = geticon(info.uid, id, info.scr, info.fscr);
  257. // }
  258. // return `
  259. // <div>
  260. // <div>
  261. // ${h}
  262. // <span data-v-3a151854="">
  263. // <a
  264. // data-v-303bbf52=""
  265. // data-v-3fb75f36=""
  266. // href="javascript:void 0"
  267. // class="color-default"
  268. // style="text-decoration: none;"
  269. // >
  270. // <span
  271. // data-v-3fb75f36=""
  272. // style="float: right;font-weight: light;"
  273. // data="${id}"
  274. // class="problem-helper-delete-from-list"
  275. // >
  276. // 删除
  277. // </span>
  278. // </a>
  279. // </span>
  280. // <a
  281. // class="colored problem-helper-text
  282. // style="padding-left: 3px"
  283. // href="/problemnew/show/${id}"
  284. // target="_blank"
  285. // >
  286. // <b>${id}</b> ${local[id]}
  287. // </a>
  288. // </div>
  289. // </div>`;
  290. // };
  291. // let content = "";
  292. // for (let i in local) content += await getList(i);
  293. // return renderList("本地列表", content, "");
  294. // };
  295. function renderItem(title, id) {
  296. return `
  297. <div>
  298. <div>
  299. <span data-v-3a151854="">
  300. <a
  301. data-v-303bbf52=""
  302. data-v-3fb75f36=""
  303. href="/record/list?pid=${id}&amp;user=${task.userScore.user.uid}"
  304. class="color-default"
  305. style="text-decoration: none;"
  306. >
  307. ${geticon(
  308. id,
  309. task.userScore.user.uid,
  310. task.userScore.score[id],
  311. task.problems[x].problem.fullScore,
  312. task.userScore.status[id]
  313. )}
  314. </a>
  315. </span>
  316. <a
  317. class="colored problem-helper-text
  318. style="padding-left: 3px"
  319. href="/problemnew/show/${id}"
  320. target="_blank"
  321. >
  322. <b>${id}</b> ${task.problems[x].problem.title}
  323. </a>
  324. </div>
  325. </div>
  326. `;
  327. }
  328. async function loaderProblemEntry() {
  329. let lists = "";
  330. let urls = configs.train;
  331. // console.log(urls);
  332. if (configs.local && !urls[configs.local]) urls[configs.local] = "本地列表";
  333. for (let i in urls) lists += await loadProblemList(i);
  334. lists += renderList(
  335. "设置",
  336. `<div data-v-59a1d633="" data-v-83961efe="" class="row">
  337. <div data-v-72d91c56="" data-v-59a1d633="" class="checkbox" data-v-83961efe=""><input id="problem-helper-hide-aced" type="checkbox" value="65560" ${
  338. configs.hide_ac ? 'checked=""' : ""
  339. }"> <lable>
  340. 隐藏已通过题目
  341. </label> </div>
  342. <div>
  343. <label>本地题单</label>
  344. <input data-v-a7f7c968="" type="text" placeholder="题单id" class="lfe-form-sz-middle" style="width: 30%;" value="${
  345. configs.local || ""
  346. }" id="problem-helper-set-local-list">
  347. </div>
  348. <div>
  349. <a id="problem-helper-save">保存到洛谷云剪贴板</a>
  350. </div>
  351. <div>
  352. <a id="problem-helper-load">从洛谷云剪贴板加载</a>
  353. </div>
  354. <div>
  355. <label>批量导入</label>
  356. <input data-v-a7f7c968="" type="text" class="lfe-form-sz-middle" style="width: 30%;" id="problem-helper-import">
  357. </div>
  358. </div>`,
  359. ""
  360. );
  361. $("#problem-helper-entry").html(lists);
  362. $(".problem-helper-fold-on").click((u) => {
  363. u = $(u.target);
  364. u.parent().prev(".problem-helper-inner").removeAttr("style");
  365. u.attr("style", "display:none;");
  366. u.prev(".problem-helper-fold-off").removeAttr("style");
  367. });
  368. $(".problem-helper-fold-off").click((u) => {
  369. u = $(u.target);
  370. u.parent().prev(".problem-helper-inner").attr("style", "display:none;");
  371. u.attr("style", "display:none;");
  372. u.next(".problem-helper-fold-on").removeAttr("style");
  373. });
  374. $("#problem-helper-hide-aced").click((u) => {
  375. u = $(u.target);
  376. configs.hide_ac = u[0].checked;
  377. unsafeWindow._feInstance.$swalToastSuccess("修改成功");
  378. loaderProblemEntry();
  379. });
  380. $("#problem-helper-save").click((u) => {
  381. saveConfig(configs);
  382. unsafeWindow._feInstance.$swalToastSuccess("保存成功");
  383. loaderProblemEntry();
  384. });
  385. $("#problem-helper-load").click(async (u) => {
  386. if (await loadConfig())
  387. unsafeWindow._feInstance.$swalToastSuccess("加载成功");
  388. else unsafeWindow._feInstance.$swalToastError("加载失败");
  389. loaderProblemEntry();
  390. });
  391. $("#problem-helper-set-local-list").keydown(function (e) {
  392. if (e.keyCode == 13) {
  393. configs.local = $("#problem-helper-set-local-list").val();
  394. unsafeWindow._feInstance.$swalToastSuccess("修改成功");
  395. loaderProblemEntry();
  396. }
  397. });
  398. $("#problem-helper-import").keydown(function (e) {
  399. if (e.keyCode == 13) {
  400. let u = $("#problem-helper-import").val().trim().split(",");
  401. let t = local_problems;
  402. for (let i in u) if (t.indexOf(u[i]) == -1) t.push(u[i]);
  403. saveTrain(t);
  404. unsafeWindow._feInstance.$swalToastSuccess("导入成功");
  405. loaderProblemEntry();
  406. }
  407. });
  408. $("#problem-helper-add-to-list").click(async (u) => {
  409. let r = unsafeWindow.location.href.split("/");
  410. r = r[r.length - 1];
  411. while (r[r.length - 1] == "#") r = r.slice(0, r.length - 1);
  412. if (!unsafeWindow._feInjection.currentData.problem.title) return;
  413. local_problems = (await getTask(configs.local)).problems.map(
  414. (u) => u.problem.pid
  415. );
  416. if (local_problems.indexOf(r) == -1) local_problems.push(r);
  417. saveTrain(local_problems);
  418. unsafeWindow._feInstance.$swalToastSuccess("添加成功");
  419. loaderProblemEntry();
  420. });
  421. $(".problem-helper-delete-from-list").click((u) => {
  422. u = $(u.target).parents(".problem-helper-delete-from-list");
  423. let r = u.attr("data");
  424. let now = local_problems;
  425. let newone = [];
  426. for (let i in now) if (now[i] != r) newone.push(now[i]);
  427. saveTrain(newone);
  428. unsafeWindow._feInstance.$swalToastSuccess("删除成功");
  429. loaderProblemEntry();
  430. });
  431. $(".problem-helper-train-remove").click((u) => {
  432. u = $(u.target);
  433. let r = u.attr("data");
  434. let now = configs.train;
  435. let newone = {};
  436. for (let i in now) if (i != r) newone[i] = now[i];
  437. configs.train = now;
  438. unsafeWindow._feInstance.$swalToastSuccess("删除成功");
  439. loaderProblemEntry();
  440. });
  441. }
  442. let deferredInjectProblemPage = () => {
  443. $(".problem-helper-container").remove();
  444. $(".side").prepend(
  445. renderList(
  446. "做题助手",
  447. '<div id="problem-helper-entry"></div>',
  448. '<a style="float:right;" data-v-303bbf52="" data-v-3fb75f36="" href="javascript:void 0" id="problem-helper-add-to-list" class="color-default"><svg data-v-b35188f4="" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="plus-square" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="svg-inline--fa fa-plus-square fa-w-14"><path data-v-b35188f4="" fill="currentColor" d="M400 32H48C21.5 32 0 53.5 0 80v352c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48zm-32 252c0 6.6-5.4 12-12 12h-92v92c0 6.6-5.4 12-12 12h-56c-6.6 0-12-5.4-12-12v-92H92c-6.6 0-12-5.4-12-12v-56c0-6.6 5.4-12 12-12h92v-92c0-6.6 5.4-12 12-12h56c6.6 0 12 5.4 12 12v92h92c6.6 0 12 5.4 12 12v56z" class=""></path></svg>添加至列表</a >'
  449. )
  450. );
  451. loaderProblemEntry();
  452. };
  453. let loadTrainList = (id) => {
  454. let local = configs.train;
  455. let getList = (id) => {
  456. return `
  457. <div>
  458. <div>
  459. <span data-v-3a151854="">
  460. <a
  461. style="text-decoration: none;float: right;font-weight: light;"
  462. data="${id}"
  463. class="color-default problem-helper-delete-from-list"
  464. data-v-303bbf52=""
  465. data-v-3fb75f36=""
  466. href="javascript:void 0"
  467. >
  468. <svg data-v-4121e124="" aria-hidden="true" focusable="false" data-prefix="fal" data-icon="times" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 320 512" class="svg-inline--fa fa-times fa-w-10"><path data-v-4121e124="" fill="currentColor" d="M193.94 256L296.5 153.44l21.15-21.15c3.12-3.12 3.12-8.19 0-11.31l-22.63-22.63c-3.12-3.12-8.19-3.12-11.31 0L160 222.06 36.29 98.34c-3.12-3.12-8.19-3.12-11.31 0L2.34 120.97c-3.12 3.12-3.12 8.19 0 11.31L126.06 256 2.34 379.71c-3.12 3.12-3.12 8.19 0 11.31l22.63 22.63c3.12 3.12 8.19 3.12 11.31 0L160 289.94 262.56 392.5l21.15 21.15c3.12 3.12 8.19 3.12 11.31 0l22.63-22.63c3.12-3.12 3.12-8.19 0-11.31L193.94 256z" class=""></path></svg>
  469. </a>
  470. </span>
  471. <a
  472. class="colored problem-helper-text"
  473. style="padding-left: 3px"
  474. href="https://www.luogu.com.cn/training/${id}#information"
  475. target="_blank"
  476. >
  477. <b>${id}</b> ${local[id]}
  478. </a>
  479. </div>
  480. </div>`;
  481. };
  482. let lists = "";
  483. for (let i in local) lists += getList(i);
  484. return lists;
  485. };
  486. let loaderTrainEntry = async () => {
  487. let lists = loadTrainList();
  488. $("#problem-helper-entry").html(lists);
  489. $(".problem-helper-fold-on").click((u) => {
  490. u = $(u.target);
  491. u.parent().prev(".problem-helper-inner").removeAttr("style");
  492. u.attr("style", "display:none;");
  493. u.prev(".problem-helper-fold-off").removeAttr("style");
  494. });
  495. $(".problem-helper-fold-off").click((u) => {
  496. u = $(u.target);
  497. u.parent().prev(".problem-helper-inner").attr("style", "display:none;");
  498. u.attr("style", "display:none;");
  499. u.next(".problem-helper-fold-on").removeAttr("style");
  500. });
  501. $("#problem-helper-add-to-list").click((u) => {
  502. let r = unsafeWindow.location.href.split("/");
  503. r = r[r.length - 1].split("#")[0];
  504. if (!unsafeWindow._feInjection.currentData.training.title) return;
  505. let now = configs.train;
  506. if (!now) now = {};
  507. now[r] = unsafeWindow._feInjection.currentData.training.title;
  508. configs.train = now;
  509. unsafeWindow._feInstance.$swalToastSuccess("添加成功");
  510. loaderTrainEntry();
  511. });
  512. $(".problem-helper-delete-from-list").click((u) => {
  513. u = $(u.target).parents(".problem-helper-delete-from-list");
  514. let r = u.attr("data");
  515. let now = configs.train;
  516. let newone = {};
  517. for (let i in now) if (i != r) newone[i] = now[i];
  518. configs.train = newone;
  519. unsafeWindow._feInstance.$swalToastSuccess("删除成功");
  520. loaderProblemEntry();
  521. });
  522. };
  523. let deferredInjectTrainPage = () => {
  524. $(".problem-helper-container").remove();
  525. $(".side").prepend(
  526. renderList(
  527. "做题助手",
  528. '<div id="problem-helper-entry"></div>',
  529. '<a style="float:right;" data-v-303bbf52="" data-v-3fb75f36="" href="javascript:void 0" id="problem-helper-add-to-list" class="color-default"><svg data-v-b35188f4="" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="plus-square" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="svg-inline--fa fa-plus-square fa-w-14"><path data-v-b35188f4="" fill="currentColor" d="M400 32H48C21.5 32 0 53.5 0 80v352c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48zm-32 252c0 6.6-5.4 12-12 12h-92v92c0 6.6-5.4 12-12 12h-56c-6.6 0-12-5.4-12-12v-92H92c-6.6 0-12-5.4-12-12v-56c0-6.6 5.4-12 12-12h92v-92c0-6.6 5.4-12 12-12h56c6.6 0 12 5.4 12 12v92h92c6.6 0 12 5.4 12 12v56z" class=""></path></svg>添加至列表</a>'
  530. )
  531. );
  532. loaderTrainEntry();
  533. };
  534. let deferredInjectProblemlist = async () => {
  535. local_problems = (await getTask(configs.local)).problems.map(
  536. (u) => u.problem.pid
  537. );
  538. $(".problem-helper-inlist-adder").remove();
  539. let pid = "",
  540. name = "";
  541. let h = (pid, name) => `
  542. <a style="float:right; padding-right: 2em;" data="${pid}" data-v-303bbf52="" data-v-3fb75f36="" href="javascript:void 0" class="problem-helper-inlist-adder color-default"><svg data-v-b35188f4="" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="plus-square" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 448 512" class="svg-inline--fa fa-plus-square fa-w-14"><path data-v-b35188f4="" fill="currentColor" d="M400 32H48C21.5 32 0 53.5 0 80v352c0 26.5 21.5 48 48 48h352c26.5 0 48-21.5 48-48V80c0-26.5-21.5-48-48-48zm-32 252c0 6.6-5.4 12-12 12h-92v92c0 6.6-5.4 12-12 12h-56c-6.6 0-12-5.4-12-12v-92H92c-6.6 0-12-5.4-12-12v-56c0-6.6 5.4-12 12-12h92v-92c0-6.6 5.4-12 12-12h56c6.6 0 12 5.4 12 12v92h92c6.6 0 12 5.4 12 12v56z" class=""></path></svg>
  543. 添加
  544. </a>`;
  545. let rows = $(".row");
  546. let trim = (s) => {
  547. return s.replace(/(^\s*)|(\s*$)/g, "");
  548. };
  549. rows.each((u) => {
  550. u = $(rows[u]);
  551. pid = u.children(".pid").text();
  552. name = u.children(".title").children(".title").text();
  553. name = trim(name);
  554. u.children(".title").prepend(h(pid, name));
  555. });
  556. $(".problem-helper-inlist-adder").click((u) => {
  557. u = $(u.target);
  558. let r = u.attr("data");
  559. let now = local_problems;
  560. if (now.indexOf(r) == -1) now.push(r);
  561. saveTrain(now);
  562. unsafeWindow._feInstance.$swalToastSuccess("添加成功");
  563. });
  564. };
  565.  
  566. /* main controller */
  567. function inject() {
  568. if (unsafeWindow.location.href.includes("problem/list"))
  569. deferredInjectProblemlist();
  570. else if (unsafeWindow.location.href.includes("training"))
  571. deferredInjectTrainPage(), deferredInjectProblemlist();
  572. else if (unsafeWindow.location.href.includes("problem"))
  573. deferredInjectProblemPage();
  574. }
  575. window.addEventListener("popstate", function (e) {
  576. setTimeout(inject, inject_lantency);
  577. });
  578. setTimeout(inject, inject_lantency);
  579. $(document.body).append(
  580. `<style>
  581. .problem-helper-text{
  582. -webkit-line-clamp: 1; overflow: hidden; display: -webkit-box;
  583. -webkit-box-orient: vertical; white-space: normal;
  584. }
  585. .expand-tip > span[data-v-e4b7c2ca] {
  586. -webkit-user-select: none;
  587. -moz-user-select: none;
  588. -ms-user-select: none;
  589. cuser-select: none;
  590. cursor: pointer;
  591. color: rgba(0, 0, 0, .3);
  592. }
  593. .expand-tip[data-v-e4b7c2ca] {
  594. text-align: center;
  595. }
  596. .expand-tip > span[data-v-e4b7c2ca]:hover {
  597. color: inherit;
  598. }
  599. </style>`
  600. );
  601. });