wsxy_Learn

网上学院函数库:自动报名、学习、考试等

目前为 2020-02-06 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/396002/770450/wsxy_Learn.js

  1. // ==UserScript==
  2. // @name wsxy_Learn
  3. // @namespace Vionlentmonkey
  4. // @version 0.9.2
  5. // @description 网上学院函数库:自动报名、学习、考试等
  6. // ==/UserScript==
  7.  
  8. // 自动报名高学分课程。2020 年初,高于 1 学分的有且仅有 20 门 3 学分课程。
  9. const autoSignupMaxCredit = async () => {
  10. // 需要 iframe 提升才会执行
  11. if (window.top !== window.self) return;
  12. let courses = document.querySelectorAll('#requiredCourseTable .course');
  13. const waitCourseInfo = JSON.parse(localStorage.getItem('waitCourseInfo'));
  14. for (let w of waitCourseInfo) {
  15. // 学分高且未报名。
  16. // 取消报名的也有 apply_pk,但没有进度点数 jdpoint 。
  17. if (w.courseCredit > 1 && !w.jdpoint) {
  18. console.log(w.course_name);
  19. for (let c of courses) {
  20. let coursePk = Number(c.getElementsByClassName('coursePk')[0].textContent);
  21. // 不能作为判断依据。
  22. if (coursePk === w.course_pk) {
  23. c.click();
  24. const btn = document.getElementsByClassName('layui-layer-btn0');
  25. if (btn.length === 1) {
  26. btn[0].click();
  27. }
  28. }
  29. }
  30. }
  31. }
  32. };
  33.  
  34. // 自动报名高学时课程
  35. const autoSignupMaxTime = async () => {
  36. // 需要 iframe 提升才会执行
  37. if (window.top !== window.self) return;
  38. // 存储所有未报名课程的课时和对应编号
  39. let timesMap = new Map();
  40. const waitCourseInfo = JSON.parse(localStorage.getItem('waitCourseInfo'));
  41. for (let w of waitCourseInfo) {
  42. // 报名后等于零,undefined 代表未报名
  43. if (w.jdpoint === undefined) {
  44. timesMap.set(w.courseTime, w.course_pk);
  45. }
  46. }
  47. let timesArray = [...timesMap.keys()];
  48. let longest = Math.max(...timesArray);
  49. console.log(`+${longest}h`);
  50. let maxTimeCourse_pk = timesMap.get(longest);
  51. let courses = document.querySelectorAll('#requiredCourseTable .course');
  52. for (let c of courses) {
  53. let coursePk = Number(c.getElementsByClassName('coursePk')[0].textContent);
  54. if (coursePk === maxTimeCourse_pk) {
  55. c.click();
  56. const btn = document.getElementsByClassName('layui-layer-btn0');
  57. if (btn.length === 1) {
  58. btn[0].click();
  59. }
  60. }
  61. }
  62. };
  63.  
  64. const autoOpenExam = async exams => {
  65. for await (let exam of exams) {
  66. const examURL = location.origin + '/sfxzwsxy/' + exam.getAttribute('onclick').split("'")[1];
  67. // 魔法打开的考卷确认交卷后不能自动关闭,只得如此暴力处理,1分钟后强行关闭。
  68. let autoExam = GM_openInTab(examURL, true);
  69. setTimeout(autoExam.close, 60000);
  70. }
  71. };
  72.  
  73. const autoLearn = async exams => {
  74. if (window.top !== window.self) return;
  75. // 30 分钟刷新一次
  76. setInterval(() => {
  77. location.reload(true);
  78. }, 1800000);
  79. const total_hour = Number(localStorage.getItem('total_hour')); //规定需达到的总学时
  80. const required_hour = Number(localStorage.getItem('required_hour')); //规定需达到的必修学时
  81. const required_credit = Number(localStorage.getItem('required_credit')); //规定需达到的总学分
  82. const user_total_hour = Number(localStorage.getItem('user_total_hour')); //用户已获得的总学时
  83. const user_required_hour = Number(localStorage.getItem('user_required_hour')); //用户已获得的必修学时
  84. const user_required_credit = Number(localStorage.getItem('user_required_credit')); //用户已获得的总学分
  85.  
  86. console.log(`已获得:必修学时:${user_required_hour},学分:${user_required_credit}`);
  87. // 判断是否已完成。首次在新标签页打开本页显然是未完成,但刷新后可能进入已完成状态。
  88. if (
  89. user_total_hour >= total_hour &&
  90. user_required_hour >= required_hour &&
  91. user_required_credit >= required_credit
  92. ) {
  93. console.log(`本学年任务已经完成`);
  94. return;
  95. } else {
  96. // 全部未完成必修课程。为同时解决学时学分问题,只关注必修课。
  97. const waitCourseInfo = JSON.parse(localStorage.getItem('waitCourseInfo'));
  98. //console.log(waitCourseInfo);
  99. const courses = document.querySelectorAll('#requiredCourseTable .course');
  100. // 初始化预期学时/学分为已得值
  101. let pendingCredit = user_required_credit;
  102. let pendingTime = user_total_hour;
  103. // 向预期学时/学分添加已报名课程数据
  104. for (w of waitCourseInfo) {
  105. // jdpoint 保证已报名,否则无法处理取消报名的问题
  106. if (w.player === 'old' && w.jdpoint >= 0) {
  107. pendingCredit += w.courseCredit;
  108. pendingTime += w.courseTime;
  109. }
  110. }
  111. if (user_required_credit < required_credit && exams.length > 0) {
  112. console.log('学分未满,有待考试课程');
  113. await autoOpenExam(exams);
  114. // 1分钟后考完关闭,2分钟后刷新
  115. setTimeout(() => {
  116. location.reload(true);
  117. }, 120000);
  118. } else if (pendingTime < total_hour) {
  119. console.log(
  120. `已报名:必修学时:${pendingTime.toFixed(1)},学分:${pendingCredit},继续报名。`
  121. );
  122. autoSignupMaxTime();
  123. } else if (pendingTime >= total_hour && pendingCredit < required_credit) {
  124. // 因为全部学习必修课,出现本状况可能很小,暂不处理
  125. console.log(
  126. `已报名:必修学时:${pendingTime.toFixed(1)},学分:${pendingCredit},有待处理。`
  127. );
  128. } else if (pendingTime >= total_hour && pendingCredit >= required_credit) {
  129. console.log(
  130. `已报名:必修学时:${pendingTime.toFixed(1)},学分:${pendingCredit},已达预期。`
  131. );
  132. if (user_required_hour < total_hour) {
  133. console.log('学时未满,自动打开已报名课程,将定时关闭。');
  134. let i = 0;
  135. for await (let c of courses) {
  136. let applyPk = Number(c.getElementsByClassName('applyPk')[0].textContent);
  137. let jdjs = c.getElementsByClassName('jdjs')[0].textContent; // 完成进度定性
  138. // 未报名课程 applyPk === '',取消报名的课程却能直接获取 applyPk,可能不适合使用 for length++ 循环
  139. if (jdjs === '完成进度') {
  140. if (i < GM_config.get('batch')) {
  141. // 部分少见的新型播放器会弹出 confirm,避开为宜
  142. for (w of waitCourseInfo) {
  143. if (w.apply_pk === applyPk && w.player === 'old') {
  144. const trainURL =
  145. location.origin +
  146. '/sfxzwsxy/jypxks/modules/train/ware/course_ware_view.jsp?applyPk=' +
  147. applyPk +
  148. '&courseType=1';
  149. const openClose = GM_openInTab(trainURL, true);
  150. // 25分钟自动关闭
  151. setTimeout(openClose.close, 1500000);
  152. i++;
  153. }
  154. }
  155. } else {
  156. // 跳过的课程一样会计数,可能不准
  157. console.log(`已尝试批量打开${i}个课程`);
  158. return;
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }
  165. };