Luogu Tasks

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

As of 2021-04-02. See the latest version.

  1. // ==UserScript==
  2. // @name Luogu Tasks
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.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. window.onload = async () => {
  17. const inject_lantency = 500;
  18. var _wr = (type) => {
  19. var orig = history[type];
  20. return function () {
  21. var e = new Event(type);
  22. e.arguments = arguments;
  23. window.dispatchEvent(e);
  24. var rv = orig.apply(this, arguments);
  25. return rv;
  26. };
  27. };
  28. history.pushState = _wr("pushState");
  29. history.replaceState = _wr("replaceState");
  30.  
  31. let configs = {
  32. set train(v) {
  33. GM_setValue("problem-helper-local-train-list", v);
  34. },
  35. get train() {
  36. return GM_getValue("problem-helper-local-train-list") || [];
  37. },
  38. set local(v) {
  39. GM_setValue("problem-helper-local-id", v);
  40. },
  41. get local() {
  42. return GM_getValue("problem-helper-local-id") || null;
  43. },
  44. set hideac(v) {
  45. GM_setValue("problem-helper-hide-aced", v);
  46. },
  47. get hideac() {
  48. return GM_getValue("problem-helper-hide-aced") || false;
  49. },
  50. };
  51.  
  52. let local_problems;
  53. let geticon = (pid, uid, scr, fscr, stat = null) => {
  54. let res;
  55. let ua = (uid, pid) =>
  56. `<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>`;
  57. let ac = (uid, pid) =>
  58. `<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>`;
  59. let nt = (uid, pid) =>
  60. `<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>`;
  61. if (scr == fscr || stat) res = ac(uid, pid);
  62. else if (scr == null) res = nt(uid, pid);
  63. else res = ua(uid, pid);
  64. return (
  65. `<span data-v-3fb75f36="" style="font-weight: bold;">` + res + `</span>`
  66. );
  67. };
  68. let renderList = (title, content, float) =>
  69. `
  70. <div
  71. data-v-796309f8=""
  72. data-v-3fb75f36=""
  73. class="card padding-default"
  74. data-v-6febb0e8=""
  75. id="problem-helper-container"
  76. >
  77. ${float}
  78. <h4 data-v-3fb75f36="" data-v-796309f8="" class="lfe-h4">${title}</h4>
  79. <div class="problem-helper-inner" style="display:none;">
  80. ${content}
  81. </div>
  82. <div data-v-e4b7c2ca="" data-v-3fb75f36="" class="expand-tip lfe-caption" data-v-796309f8="" >
  83. <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>
  84. <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>
  85. </div>
  86. </div>
  87. `;
  88. function getTask(id) {
  89. return new Promise((r) => {
  90. $.get(
  91. `https://www.luogu.com.cn/training/${id}?_contentOnly=any`
  92. ).then((u) => r(u.currentData.training));
  93. });
  94. }
  95. let loadProblemList = async (id) => {
  96. task = await getTask(id);
  97. let getList = (x, is_local) => {
  98. let id = task.problems[x].problem.pid;
  99. if (configs.hideac && task.userScore.status[id] && id != configs.local)
  100. return "";
  101. return `
  102. <div>
  103. <div>
  104. <span data-v-3a151854="">
  105. <a
  106. data-v-303bbf52=""
  107. data-v-3fb75f36=""
  108. href="/record/list?pid=${id}&amp;user=${task.userScore.user.uid}"
  109. class="color-default"
  110. style="text-decoration: none;"
  111. >
  112. ${geticon(
  113. id,
  114. task.userScore.user.uid,
  115. task.userScore.score[id],
  116. task.problems[x].problem.fullScore,
  117. task.userScore.status[id]
  118. )}
  119. </a>
  120. ${
  121. is_local
  122. ? `
  123. <a
  124. data-v-3fb75f36=""
  125. style="float: right;font-weight: light;"
  126. class="problem-helper-delete-from-list"
  127. data="${id}"
  128. >
  129. <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>
  130. </a>
  131. `
  132. : ""
  133. }
  134. </span>
  135. <a
  136. class="colored problem-helper-text"
  137. style="padding-left: 3px"
  138. href="/problemnew/show/${id}"
  139. target="_blank"
  140. >
  141. <b>${id}</b> ${task.problems[x].problem.title}
  142. </a>
  143. </div>
  144. </div>`;
  145. };
  146. let content = "",
  147. float =
  148. '<a style="float:right;" data="${task.id}" data-v-303bbf52="" data-v-3fb75f36="" href="#" class="problem-helper-train-remove color-default">删除</a>';
  149. if (id == configs.local)
  150. (float = ""), (local_problems = task.problems.map((u) => u.problem.pid));
  151. for (let i in task.problems) content += getList(i, id == configs.local);
  152. return renderList(task.title, content, float);
  153. };
  154. let saveTrain = async (problems) => {
  155. return new Promise((r) => {
  156. $.ajax({
  157. type: "POST",
  158. url: `https://www.luogu.com.cn/api/training/editProblems/${configs.local}`,
  159. beforeSend: function (request) {
  160. request.setRequestHeader(
  161. "x-csrf-token",
  162. $("meta[name='csrf-token']")[0].content
  163. );
  164. },
  165. contentType: "application/json;charset=UTF-8",
  166. data: JSON.stringify({ pids: problems }),
  167. success: () => r(),
  168. });
  169. });
  170. };
  171. let saveConfig = async (config) => {
  172. return new Promise((r) => {
  173. $.ajax({
  174. type: "POST",
  175. url: `https://www.luogu.com.cn/paste/new`,
  176. beforeSend: function (request) {
  177. request.setRequestHeader(
  178. "x-csrf-token",
  179. $("meta[name='csrf-token']")[0].content
  180. );
  181. },
  182. contentType: "application/json;charset=UTF-8",
  183. data: JSON.stringify({
  184. public: false,
  185. data: "#lgtsk" + JSON.stringify(config),
  186. }),
  187. success: () => r(),
  188. });
  189. });
  190. };
  191. let loadConfig = () => {
  192. return new Promise((r) => {
  193. $.get("https://www.luogu.com.cn/paste?_contentOnly").then((u) => {
  194. u = u.currentData.pastes.result;
  195. let nc = null;
  196. for (let i in u) {
  197. try {
  198. if (u[i].data.substr(0, 6) !== "#lgtsk") continue;
  199. let k = u[i].data;
  200. nc = JSON.parse(k.substr(6, k.lentgh));
  201. break;
  202. } catch (e) {}
  203. }
  204. if (!nc) return r(0);
  205. configs.train = nc.train;
  206. configs.local = nc.local;
  207. configs.hideac = nc.hideac;
  208. r(1);
  209. });
  210. });
  211. };
  212. let getScore = async (sett) => {
  213. if (sett.method == "train")
  214. return {
  215. uid: sett.task.userScore.user.uid,
  216. scr: sett.task.userScore.score[sett.id],
  217. fscr: sett.task.problems[sett.id].problem.fullScore,
  218. };
  219. else if (sett.method == "local")
  220. return new Promise((res, rej) => {
  221. $.get(`https://www.luogu.com.cn/problem/${sett.id}?_contentOnly`).then(
  222. (u) => {
  223. res({
  224. uid: u.currentUser.uid,
  225. scr: u.currentData.problem.score,
  226. fscr: u.currentData.problem.fullScore,
  227. });
  228. }
  229. );
  230. });
  231. };
  232. // let loadLocalProblem = async () => {
  233. // let local = GM_getValue("problem-helper-local-list");
  234. // let getList = async (id, opt = 1) => {
  235. // let h = "";
  236. // if (opt) {
  237. // let info = await getScore({ method: "local", id: id });
  238. // h = geticon(info.uid, id, info.scr, info.fscr);
  239. // }
  240. // return `
  241. // <div>
  242. // <div>
  243. // ${h}
  244. // <span data-v-3a151854="">
  245. // <a
  246. // data-v-303bbf52=""
  247. // data-v-3fb75f36=""
  248. // href="#"
  249. // class="color-default"
  250. // style="text-decoration: none;"
  251. // >
  252. // <span
  253. // data-v-3fb75f36=""
  254. // style="float: right;font-weight: light;"
  255. // data="${id}"
  256. // class="problem-helper-delete-from-list"
  257. // >
  258. // 删除
  259. // </span>
  260. // </a>
  261. // </span>
  262. // <a
  263. // class="colored problem-helper-text
  264. // style="padding-left: 3px"
  265. // href="/problemnew/show/${id}"
  266. // target="_blank"
  267. // >
  268. // <b>${id}</b> ${local[id]}
  269. // </a>
  270. // </div>
  271. // </div>`;
  272. // };
  273. // let content = "";
  274. // for (let i in local) content += await getList(i);
  275. // return renderList("本地列表", content, "");
  276. // };
  277. function renderItem(title, id) {
  278. return `
  279. <div>
  280. <div>
  281. <span data-v-3a151854="">
  282. <a
  283. data-v-303bbf52=""
  284. data-v-3fb75f36=""
  285. href="/record/list?pid=${id}&amp;user=${task.userScore.user.uid}"
  286. class="color-default"
  287. style="text-decoration: none;"
  288. >
  289. ${geticon(
  290. id,
  291. task.userScore.user.uid,
  292. task.userScore.score[id],
  293. task.problems[x].problem.fullScore,
  294. task.userScore.status[id]
  295. )}
  296. </a>
  297. </span>
  298. <a
  299. class="colored problem-helper-text
  300. style="padding-left: 3px"
  301. href="/problemnew/show/${id}"
  302. target="_blank"
  303. >
  304. <b>${id}</b> ${task.problems[x].problem.title}
  305. </a>
  306. </div>
  307. </div>
  308. `;
  309. }
  310. async function loaderProblemEntry() {
  311. let lists = "";
  312. let urls = configs.train;
  313. // console.log(urls);
  314. if (configs.local && !urls[configs.local]) urls[configs.local] = "本地列表";
  315. for (let i in urls) lists += await loadProblemList(i);
  316. lists += renderList(
  317. "设置",
  318. `<div data-v-59a1d633="" data-v-83961efe="" class="row">
  319. <div data-v-72d91c56="" data-v-59a1d633="" class="checkbox" data-v-83961efe=""><input id="problem-helper-hide-aced" type="checkbox" value="65560" ${
  320. configs.hideac ? 'checked=""' : ""
  321. }"> <lable>
  322. 隐藏已通过题目
  323. </label> </div>
  324. <div>
  325. <label>本地题单</label>
  326. <input data-v-a7f7c968="" type="text" placeholder="题单id" class="lfe-form-sz-middle" style+"width: 30%;" value="${
  327. configs.local || ""
  328. }" id="problem-helper-set-local-list">
  329. </div>
  330. <div>
  331. <a id="problem-helper-save">保存到洛谷云剪贴板</a>
  332. <a id="problem-helper-load">从洛谷云剪贴板加载</a>
  333. </div>
  334. </div>`,
  335. ""
  336. );
  337. $("#problem-helper-entry").html(lists);
  338. $(".problem-helper-fold-on").click((u) => {
  339. u = $(u.target);
  340. u.parent().prev(".problem-helper-inner").removeAttr("style");
  341. u.attr("style", "display:none;");
  342. u.prev(".problem-helper-fold-off").removeAttr("style");
  343. });
  344. $(".problem-helper-fold-off").click((u) => {
  345. u = $(u.target);
  346. u.parent().prev(".problem-helper-inner").attr("style", "display:none;");
  347. u.attr("style", "display:none;");
  348. u.next(".problem-helper-fold-on").removeAttr("style");
  349. });
  350. $("#problem-helper-hide-aced").click((u) => {
  351. u = $(u.target);
  352. configs.hideac = u[0].checked;
  353. unsafeWindow._feInstance.$swalToastSuccess("修改成功");
  354. loaderProblemEntry();
  355. });
  356. $("#problem-helper-save").click((u) => {
  357. saveConfig(configs);
  358. unsafeWindow._feInstance.$swalToastSuccess("保存成功");
  359. loaderProblemEntry();
  360. });
  361. $("#problem-helper-load").click(async (u) => {
  362. if (await loadConfig())
  363. unsafeWindow._feInstance.$swalToastSuccess("加载成功");
  364. else unsafeWindow._feInstance.$swalToastError("加载失败");
  365. loaderProblemEntry();
  366. });
  367. $("#problem-helper-set-local-list").keydown(function (e) {
  368. if (e.keyCode == 13) {
  369. configs.local = $("#problem-helper-set-local-list").val();
  370. unsafeWindow._feInstance.$swalToastSuccess("修改成功");
  371. loaderProblemEntry();
  372. }
  373. });
  374. $("#problem-helper-add-to-list").click((u) => {
  375. let r = unsafeWindow.location.href.split("/");
  376. r = r[r.length - 1];
  377. while (r[r.length - 1] == "#") r = r.slice(0, r.length - 1);
  378. if (!unsafeWindow._feInjection.currentData.problem.title) return;
  379. if (local_problems.indexOf(r) == -1) local_problems.push(r);
  380. saveTrain(local_problems);
  381. unsafeWindow._feInstance.$swalToastSuccess("添加成功");
  382. loaderProblemEntry();
  383. });
  384. $(".problem-helper-delete-from-list").click((u) => {
  385. u = $(u.target).parents(".problem-helper-delete-from-list");
  386. let r = u.attr("data");
  387. let now = local_problems;
  388. let newone = [];
  389. for (let i in now) if (now[i] != r) newone[i] = now[i];
  390. saveTrain(newone);
  391. unsafeWindow._feInstance.$swalToastSuccess("删除成功");
  392. loaderProblemEntry();
  393. });
  394. $(".problem-helper-train-remove").click((u) => {
  395. u = $(u.target);
  396. let r = u.attr("data");
  397. let now = configs.train;
  398. let newone = {};
  399. for (let i in now) if (i != r) newone[i] = now[i];
  400. configs.train = now;
  401. unsafeWindow._feInstance.$swalToastSuccess("删除成功");
  402. loaderProblemEntry();
  403. });
  404. }
  405. let deferredInjectProblemPage = () => {
  406. $("#problem-helper-container").remove();
  407. $(".side").prepend(
  408. renderList(
  409. "做题助手",
  410. '<div id="problem-helper-entry"></div>',
  411. '<a style="float:right;" data-v-303bbf52="" data-v-3fb75f36="" href="#" 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 >'
  412. )
  413. );
  414. loaderProblemEntry();
  415. };
  416. let loadTrainList = (id) => {
  417. let local = configs.train;
  418. let getList = (id) => {
  419. return `
  420. <div>
  421. <div>
  422. <span data-v-3a151854="">
  423. <a
  424. style="text-decoration: none;float: right;font-weight: light;"
  425. data="${id}"
  426. class="color-default problem-helper-delete-from-list"
  427. data-v-303bbf52=""
  428. data-v-3fb75f36=""
  429. href="#"
  430. >
  431. <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>
  432. </a>
  433. </span>
  434. <a
  435. class="colored problem-helper-text"
  436. style="padding-left: 3px"
  437. href="https://www.luogu.com.cn/training/${id}#information"
  438. target="_blank"
  439. >
  440. <b>${id}</b> ${local[id]}
  441. </a>
  442. </div>
  443. </div>`;
  444. };
  445. let lists = "";
  446. for (let i in local) lists += getList(i);
  447. return lists;
  448. };
  449. let loaderTrainEntry = async () => {
  450. let lists = loadTrainList();
  451. $("#problem-helper-entry").html(lists);
  452. $(".problem-helper-fold-on").click((u) => {
  453. u = $(u.target);
  454. u.parent().prev(".problem-helper-inner").removeAttr("style");
  455. u.attr("style", "display:none;");
  456. u.prev(".problem-helper-fold-off").removeAttr("style");
  457. });
  458. $(".problem-helper-fold-off").click((u) => {
  459. u = $(u.target);
  460. u.parent().prev(".problem-helper-inner").attr("style", "display:none;");
  461. u.attr("style", "display:none;");
  462. u.next(".problem-helper-fold-on").removeAttr("style");
  463. });
  464. $("#problem-helper-add-to-list").click((u) => {
  465. let r = unsafeWindow.location.href.split("/");
  466. r = r[r.length - 1].split("#")[0];
  467. if (!unsafeWindow._feInjection.currentData.training.title) return;
  468. let now = configs.train;
  469. if (!now) now = {};
  470. now[r] = unsafeWindow._feInjection.currentData.training.title;
  471. configs.train = now;
  472. unsafeWindow._feInstance.$swalToastSuccess("添加成功");
  473. loaderTrainEntry();
  474. });
  475. $(".problem-helper-delete-from-list").click((u) => {
  476. u = $(u.target).parents(".problem-helper-delete-from-list");
  477. let r = u.attr("data");
  478. let now = configs.train;
  479. let newone = {};
  480. for (let i in now) if (i != r) newone[i] = now[i];
  481. configs.train = newone;
  482. unsafeWindow._feInstance.$swalToastSuccess("删除成功");
  483. loaderProblemEntry();
  484. });
  485. };
  486. let deferredInjectTrainPage = () => {
  487. $("#problem-helper-container").remove();
  488. $(".side").prepend(
  489. renderList(
  490. "做题助手",
  491. '<div id="problem-helper-entry"></div>',
  492. '<a style="float:right;" data-v-303bbf52="" data-v-3fb75f36="" href="#" 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>'
  493. )
  494. );
  495. loaderTrainEntry();
  496. };
  497. let deferredInjectProblemlist = async () => {
  498. local_problems = (await getTask(configs.local)).problems.map(
  499. (u) => u.problem.pid
  500. );
  501. $(".problem-helper-inlist-adder").remove();
  502. let pid = "",
  503. name = "";
  504. let h = (pid, name) => `
  505. <a style="float:right; padding-right: 2em;" data="${pid}" data-v-303bbf52="" data-v-3fb75f36="" href="#" 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>
  506. 添加
  507. </a>`;
  508. let rows = $(".row");
  509. let trim = (s) => {
  510. return s.replace(/(^\s*)|(\s*$)/g, "");
  511. };
  512. rows.each((u) => {
  513. u = $(rows[u]);
  514. pid = u.children(".pid").text();
  515. name = u.children(".title").children(".title").text();
  516. name = trim(name);
  517. u.children(".title").prepend(h(pid, name));
  518. });
  519. $(".problem-helper-inlist-adder").click((u) => {
  520. u = $(u.target);
  521. let r = u.attr("data");
  522. let now = local_problems;
  523. if (now.indexOf(r) == -1) now.push(r);
  524. saveTrain(now);
  525. unsafeWindow._feInstance.$swalToastSuccess("添加成功");
  526. });
  527. };
  528.  
  529. /* main controller */
  530. let inject = () => {
  531. if (unsafeWindow.location.href.includes("problem/list"))
  532. deferredInjectProblemlist();
  533. else if (unsafeWindow.location.href.includes("training"))
  534. deferredInjectTrainPage(), deferredInjectProblemlist();
  535. else if (unsafeWindow.location.href.includes("problem"))
  536. deferredInjectProblemPage();
  537. };
  538. window.addEventListener("pushState", function (e) {
  539. setTimeout(inject, inject_lantency);
  540. });
  541. window.addEventListener("replaceState", function (e) {
  542. setTimeout(inject, inject_lantency);
  543. });
  544. setTimeout(inject, 100);
  545. $(document.body).append(
  546. `<style>
  547. .problem-helper-text{
  548. -webkit-line-clamp: 1; overflow: hidden; display: -webkit-box;
  549. -webkit-box-orient: vertical; white-space: normal;
  550. }
  551. .expand-tip > span[data-v-e4b7c2ca] {
  552. -webkit-user-select: none;
  553. -moz-user-select: none;
  554. -ms-user-select: none;
  555. cuser-select: none;
  556. cursor: pointer;
  557. color: rgba(0, 0, 0, .3);
  558. }
  559. .expand-tip[data-v-e4b7c2ca] {
  560. text-align: center;
  561. }
  562. .expand-tip > span[data-v-e4b7c2ca]:hover {
  563. color: inherit;
  564. }
  565. </style>`
  566. );
  567. };