🏠 Home 

Greasy Fork is available in English.

HDUOJ增强

自动登录HDUOJ,提供历史contests选项

  1. // ==UserScript==
  2. // @name HDUOJ增强
  3. // @namespace YinTianliang_i
  4. // @version 0.2.7
  5. // @description 自动登录HDUOJ,提供历史contests选项
  6. // @author Yin Tianliang
  7. // @include *//hdu.hustoj.com/*
  8. // @include *//acm.hdu.edu.cn/*
  9. // @include *//acm.split.hdu.edu.cn/*
  10. // @require https://code.jquery.com/jquery-3.2.1.min.js
  11. // @grant none
  12. // @run-at document-start
  13. // ==/UserScript==
  14. // TODO:判断当前登录状态
  15. /*jshint esversion: 6 */
  16. let getKey = (key) => { return localStorage[key]; };
  17. let setKey = (key, value) => { localStorage[key] = value; };
  18. function getInput(mes) {
  19. let input;
  20. do {
  21. input = prompt(mes);
  22. } while (input === null);
  23. return input;
  24. }
  25. function setUserInfo() {
  26. let username = getInput("请输入用户名:");
  27. let userpass = getInput("请输入密码:");
  28. setKey("username", username);
  29. setKey("userpass", userpass);
  30. return [username, userpass];
  31. }
  32. function contestLogin(contestID, contestPSW) {
  33. $.post(`http://${location.host}/diy/contest_login.php?cid=${contestID}&action=login`,
  34. { password: contestPSW });
  35. }
  36. // 如果跳转到了登录界面,则开始执行逻辑
  37. // 提取url中的contestID 判断是否在storage中
  38. let reg = new RegExp("userloginex|contest_login.php.cid=(\\d+)");
  39. let matches = location.toString().match(reg);
  40. if (matches) {
  41. // 用户登录
  42. let username = getKey("username");
  43. let userpass = getKey("userpass");
  44. let contestID = getKey("contestID");
  45. let contestPSW = getKey("contestPSW");
  46. if (username === undefined || userpass === undefined) {
  47. [username, userpass] = setUserInfo();
  48. }
  49. $.post(`http://${location.host}/userloginex.php?action=login`,
  50. { username: username, userpass: userpass, login: "Sign In" });
  51. // 单项测试登录
  52. if (matches[1]) {
  53. if (!new RegExp(matches[1]).test(contestID)) {
  54. let psw = getInput("该测试的口令未被记录,请输入该测试的口令");
  55. if (contestID === undefined) {
  56. contestID = matches[1];
  57. contestPSW = psw;
  58. } else {
  59. contestID += '|' + matches[1];
  60. contestPSW += '|' + psw;
  61. }
  62. setKey("contestID", contestID);
  63. setKey("contestPSW", contestPSW);
  64. }
  65. contestID = contestID.split('|');
  66. contestPSW = contestPSW.split('|');
  67. for (let i = 0; i < contestID.length; i++) {
  68. contestLogin(contestID[i], contestPSW[i]);
  69. }
  70. // 跳转到题目页面
  71. location.href = `http://${location.host}/diy/contest_show.php?cid=${matches[1]}`;
  72. } else {
  73. history.back();
  74. // TODO:返回上一层后用户又要重新点击进入 重点是判断当前状态
  75. }
  76. }
  77. // 在页面顶端增加历史contests
  78. let contestID = getKey("contestID").split('|');
  79. let divObj = document.createElement("div");
  80. divObj.style = 'text-align:center';
  81. divObj.innerHTML = '历史contests:';
  82. for (let i in contestID) {
  83. divObj.innerHTML += `<a href="/diy/contest_show.php?cid=${contestID[i]}">${contestID[i]} </a>`;
  84. }
  85. divObj.innerHTML += `<a href="javascript:confirm('确定要清空吗?')&&localStorage.clear()&&location.reload()">清空数据</a>`;
  86. document.body.insertBefore(divObj, document.body.firstChild);