Greasy Fork is available in English.

Luogu Tasks

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

  1. // ==UserScript==
  2. // @name Luogu Tasks
  3. // @namespace http://tampermonkey.net/
  4. // @version 2.4.2
  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. return renderList(
  162. "...",
  163. "...",
  164. `<a id="#list-float-${id}" style="float:right;" data-v-303bbf52="" data-v-3fb75f36="" href="javascript:void 0" class="problem-helper-train-remove color-default">删除</a>`,
  165. id
  166. );
  167. };
  168. async function loadContent(id) {
  169. let task;
  170. let getList = (x, is_local) => {
  171. let id = task.problems[x].problem.pid;
  172. if (configs.hide_ac && task.userScore.status[id] && id != configs.local)
  173. return "";
  174. return `
  175. <div>
  176. <div>
  177. <span data-v-3a151854="">
  178. <a
  179. data-v-303bbf52=""
  180. data-v-3fb75f36=""
  181. href="/record/list?pid=${id}&amp;user=${task.userScore.user.uid}"
  182. class="color-default"
  183. style="text-decoration: none;"
  184. >
  185. ${geticon(
  186. id,
  187. task.userScore.user.uid,
  188. task.userScore.score[id],
  189. task.problems[x].problem.fullScore,
  190. task.userScore.status[id]
  191. )}
  192. </a>
  193. ${
  194. is_local
  195. ? `
  196. <a
  197. data-v-3fb75f36=""
  198. style="float: right;font-weight: light;"
  199. class="problem-helper-delete-from-list"
  200. data="${id}"
  201. >
  202. <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>
  203. </a>
  204. `
  205. : ""
  206. }
  207. </span>
  208. <a
  209. class="colored problem-helper-text"
  210. style="padding-left: 3px"
  211. href="/problemnew/show/${id}"
  212. target="_blank"
  213. >
  214. <b>${id}</b> ${task.problems[x].problem.title}
  215. </a>
  216. </div>
  217. </div>`;
  218. };
  219. task = await getTask(id);
  220. let content = "";
  221. if (id == configs.local) {
  222. $(`#list-float-${id}`).text("");
  223. local_problems = task.problems.map((u) => u.problem.pid);
  224. }
  225. for (let i in task.problems) content += getList(i, id == configs.local);
  226. $(`#list-content-${id}`).html(content);
  227. $(`#list-${id} > h4`).text(task.title);
  228. $(`#list-float-${id}`).attr("data", task.id);
  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. function renderItem(title, id) {
  251. return `
  252. <div>
  253. <div>
  254. <span data-v-3a151854="">
  255. <a
  256. data-v-303bbf52=""
  257. data-v-3fb75f36=""
  258. href="/record/list?pid=${id}&amp;user=${task.userScore.user.uid}"
  259. class="color-default"
  260. style="text-decoration: none;"
  261. >
  262. ${geticon(
  263. id,
  264. task.userScore.user.uid,
  265. task.userScore.score[id],
  266. task.problems[x].problem.fullScore,
  267. task.userScore.status[id]
  268. )}
  269. </a>
  270. </span>
  271. <a
  272. class="colored problem-helper-text
  273. style="padding-left: 3px"
  274. href="/problemnew/show/${id}"
  275. target="_blank"
  276. >
  277. <b>${id}</b> ${task.problems[x].problem.title}
  278. </a>
  279. </div>
  280. </div>
  281. `;
  282. }
  283. function loadListener() {
  284. $("#problem-helper-hide-aced").click((u) => {
  285. u = $(u.target);
  286. configs.hide_ac = u[0].checked;
  287. unsafeWindow._feInstance.$swalToastSuccess("修改成功");
  288. loaderProblemEntry();
  289. });
  290. $("#problem-helper-save").click((u) => {
  291. saveConfig(configs);
  292. unsafeWindow._feInstance.$swalToastSuccess("保存成功");
  293. loaderProblemEntry();
  294. });
  295. $("#problem-helper-load").click(async (u) => {
  296. if (await loadConfig())
  297. unsafeWindow._feInstance.$swalToastSuccess("加载成功");
  298. else unsafeWindow._feInstance.$swalToastError("加载失败");
  299. loaderProblemEntry();
  300. });
  301. $("#problem-helper-set-local-list").keydown(function (e) {
  302. if (e.keyCode == 13) {
  303. configs.local = $("#problem-helper-set-local-list").val();
  304. unsafeWindow._feInstance.$swalToastSuccess("修改成功");
  305. loaderProblemEntry();
  306. }
  307. });
  308. $("#problem-helper-import").keydown(async (e) => {
  309. if (e.keyCode == 13) {
  310. let u = $("#problem-helper-import").val().trim().split(",");
  311. local_problems = (await getTask(configs.local)).problems.map(
  312. (u) => u.problem.pid
  313. );
  314. let t = local_problems;
  315. for (let i in u) if (t.indexOf(u[i]) == -1) t.push(u[i]);
  316. saveTrain(t);
  317. unsafeWindow._feInstance.$swalToastSuccess("导入成功");
  318. loaderProblemEntry();
  319. }
  320. });
  321. $("#problem-helper-add-to-list").click(async (u) => {
  322. let r = unsafeWindow.location.href.split("/");
  323. r = r[r.length - 1];
  324. while (r[r.length - 1] == "#") r = r.slice(0, r.length - 1);
  325. if (!unsafeWindow._feInjection.currentData.problem.title) return;
  326. local_problems = (await getTask(configs.local)).problems.map(
  327. (u) => u.problem.pid
  328. );
  329. if (local_problems.indexOf(r) == -1) local_problems.push(r);
  330. saveTrain(local_problems);
  331. unsafeWindow._feInstance.$swalToastSuccess("添加成功");
  332. loaderProblemEntry();
  333. });
  334. $(".problem-helper-delete-from-list").click(async (u) => {
  335. u = $(u.target).parents(".problem-helper-delete-from-list");
  336. let r = u.attr("data");
  337. local_problems = (await getTask(configs.local)).problems.map(
  338. (u) => u.problem.pid
  339. );
  340. let now = local_problems;
  341. let newone = [];
  342. for (let i in now) if (now[i] != r) newone.push(now[i]);
  343. saveTrain(newone);
  344. unsafeWindow._feInstance.$swalToastSuccess("删除成功");
  345. loaderProblemEntry();
  346. });
  347. $(".problem-helper-train-remove").click((u) => {
  348. u = $(u.target);
  349. let r = u.attr("data");
  350. let now = configs.train;
  351. let newone = {};
  352. for (let i in now) if (i != r) newone[i] = now[i];
  353. configs.train = now;
  354. unsafeWindow._feInstance.$swalToastSuccess("删除成功");
  355. loaderProblemEntry();
  356. });
  357. }
  358. async function loaderProblemEntry() {
  359. let lists = "";
  360. let urls = configs.train;
  361. // console.log(urls);
  362. if (configs.local && !urls[configs.local]) urls[configs.local] = "本地列表";
  363. for (let i in urls) lists += await loadProblemList(i);
  364. let content_promise = [];
  365. for (let i in urls) content_promise.push(loadContent(i));
  366. Promise.all(content_promise).then(loadListener);
  367. lists += renderList(
  368. "设置",
  369. `<div data-v-59a1d633="" data-v-83961efe="" class="row">
  370. <div data-v-72d91c56="" data-v-59a1d633="" class="checkbox" data-v-83961efe=""><input id="problem-helper-hide-aced" type="checkbox" value="65560" ${
  371. configs.hide_ac ? 'checked=""' : ""
  372. }"> <lable>
  373. 隐藏已通过题目
  374. </label> </div>
  375. <div>
  376. <label>本地题单</label>
  377. <input data-v-a7f7c968="" type="text" placeholder="题单id" class="lfe-form-sz-middle" style="width: 30%;" value="${
  378. configs.local || ""
  379. }" id="problem-helper-set-local-list">
  380. </div>
  381. <div>
  382. <a id="problem-helper-save">保存到洛谷云剪贴板</a>
  383. </div>
  384. <div>
  385. <a id="problem-helper-load">从洛谷云剪贴板加载</a>
  386. </div>
  387. <div>
  388. <label>批量导入</label>
  389. <input data-v-a7f7c968="" type="text" class="lfe-form-sz-middle" style="width: 30%;" id="problem-helper-import">
  390. </div>
  391. </div>`,
  392. ""
  393. );
  394. $("#problem-helper-entry").html(lists);
  395. $(".problem-helper-fold-on").click((u) => {
  396. u = $(u.target);
  397. u.parent().prev(".problem-helper-inner").removeAttr("style");
  398. u.attr("style", "display:none;");
  399. u.prev(".problem-helper-fold-off").removeAttr("style");
  400. });
  401. $(".problem-helper-fold-off").click((u) => {
  402. u = $(u.target);
  403. u.parent().prev(".problem-helper-inner").attr("style", "display:none;");
  404. u.attr("style", "display:none;");
  405. u.next(".problem-helper-fold-on").removeAttr("style");
  406. });
  407. }
  408. let deferredInjectProblemPage = () => {
  409. $(".problem-helper-container").remove();
  410. $(".side").prepend(
  411. renderList(
  412. "做题助手",
  413. '<div id="problem-helper-entry"></div>',
  414. '<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 >'
  415. )
  416. );
  417. loaderProblemEntry();
  418. };
  419. let loadTrainList = (id) => {
  420. let local = configs.train;
  421. let getList = (id) => {
  422. return `
  423. <div>
  424. <div>
  425. <span data-v-3a151854="">
  426. <a
  427. style="text-decoration: none;float: right;font-weight: light;"
  428. data="${id}"
  429. class="color-default problem-helper-delete-from-list"
  430. data-v-303bbf52=""
  431. data-v-3fb75f36=""
  432. href="javascript:void 0"
  433. >
  434. <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>
  435. </a>
  436. </span>
  437. <a
  438. class="colored problem-helper-text"
  439. style="padding-left: 3px"
  440. href="https://www.luogu.com.cn/training/${id}#information"
  441. target="_blank"
  442. >
  443. <b>${id}</b> ${local[id]}
  444. </a>
  445. </div>
  446. </div>`;
  447. };
  448. let lists = "";
  449. for (let i in local) lists += getList(i);
  450. return lists;
  451. };
  452. let loaderTrainEntry = async () => {
  453. let lists = loadTrainList();
  454. $("#problem-helper-entry").html(lists);
  455. $(".problem-helper-fold-on").click((u) => {
  456. u = $(u.target);
  457. u.parent().prev(".problem-helper-inner").removeAttr("style");
  458. u.attr("style", "display:none;");
  459. u.prev(".problem-helper-fold-off").removeAttr("style");
  460. });
  461. $(".problem-helper-fold-off").click((u) => {
  462. u = $(u.target);
  463. u.parent().prev(".problem-helper-inner").attr("style", "display:none;");
  464. u.attr("style", "display:none;");
  465. u.next(".problem-helper-fold-on").removeAttr("style");
  466. });
  467. $("#problem-helper-add-to-list").click((u) => {
  468. let r = unsafeWindow.location.href.split("/");
  469. r = r[r.length - 1].split("#")[0];
  470. if (!unsafeWindow._feInjection.currentData.training.title) return;
  471. let now = configs.train;
  472. if (!now) now = {};
  473. now[r] = unsafeWindow._feInjection.currentData.training.title;
  474. configs.train = now;
  475. unsafeWindow._feInstance.$swalToastSuccess("添加成功");
  476. loaderTrainEntry();
  477. });
  478. $(".problem-helper-delete-from-list").click((u) => {
  479. u = $(u.target).parents(".problem-helper-delete-from-list");
  480. let r = u.attr("data");
  481. let now = configs.train;
  482. let newone = {};
  483. for (let i in now) if (i != r) newone[i] = now[i];
  484. configs.train = newone;
  485. unsafeWindow._feInstance.$swalToastSuccess("删除成功");
  486. loaderProblemEntry();
  487. });
  488. };
  489. let deferredInjectTrainPage = () => {
  490. $(".problem-helper-container").remove();
  491. $(".side").prepend(
  492. renderList(
  493. "做题助手",
  494. '<div id="problem-helper-entry"></div>',
  495. '<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>'
  496. )
  497. );
  498. loaderTrainEntry();
  499. };
  500. let deferredInjectProblemlist = async () => {
  501. local_problems = (await getTask(configs.local)).problems.map(
  502. (u) => u.problem.pid
  503. );
  504. $(".problem-helper-inlist-adder").remove();
  505. let pid = "",
  506. name = "";
  507. let h = (pid, name) => `
  508. <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>
  509. 添加
  510. </a>`;
  511. let rows = $(".row");
  512. let trim = (s) => {
  513. return s.replace(/(^\s*)|(\s*$)/g, "");
  514. };
  515. rows.each((u) => {
  516. u = $(rows[u]);
  517. pid = u.children(".pid").text();
  518. name = u.children(".title").children(".title").text();
  519. name = trim(name);
  520. u.children(".title").prepend(h(pid, name));
  521. });
  522. $(".problem-helper-inlist-adder").click(async (u) => {
  523. u = $(u.target);
  524. let r = u.attr("data");
  525. local_problems = (await getTask(configs.local)).problems.map(
  526. (u) => u.problem.pid
  527. );
  528. let now = local_problems;
  529. if (now.indexOf(r) == -1) now.push(r);
  530. saveTrain(now);
  531. unsafeWindow._feInstance.$swalToastSuccess("添加成功");
  532. });
  533. };
  534.  
  535. /* main controller */
  536. function inject() {
  537. if (unsafeWindow.location.href.includes("problem/list"))
  538. deferredInjectProblemlist();
  539. else if (unsafeWindow.location.href.includes("training"))
  540. deferredInjectTrainPage(), deferredInjectProblemlist();
  541. else if (unsafeWindow.location.href.includes("problem"))
  542. deferredInjectProblemPage();
  543. }
  544. window.addEventListener("popstate", function (e) {
  545. setTimeout(inject, inject_lantency);
  546. });
  547. setTimeout(inject, inject_lantency);
  548. $(document.body).append(
  549. `<style>
  550. .problem-helper-text{
  551. -webkit-line-clamp: 1; overflow: hidden; display: -webkit-box;
  552. -webkit-box-orient: vertical; white-space: normal;
  553. }
  554. .expand-tip > span[data-v-e4b7c2ca] {
  555. -webkit-user-select: none;
  556. -moz-user-select: none;
  557. -ms-user-select: none;
  558. cuser-select: none;
  559. cursor: pointer;
  560. color: rgba(0, 0, 0, .3);
  561. }
  562. .expand-tip[data-v-e4b7c2ca] {
  563. text-align: center;
  564. }
  565. .expand-tip > span[data-v-e4b7c2ca]:hover {
  566. color: inherit;
  567. }
  568. </style>`
  569. );
  570. });