普法网(宪法小卫士)课后练习、考试自动答题

第六届全国学生“学宪法 讲宪法”活动

目前为 2021-08-02 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name 普法网(宪法小卫士)课后练习、考试自动答题
  3. // @namespace Ne-21
  4. // @version 1.2
  5. // @description 第六届全国学生“学宪法 讲宪法”活动
  6. // @author Ne-21
  7. // @match https://static.qspfw.moe.gov.cn/xf2021/learn-practice.html*
  8. // @match https://static.qspfw.moe.gov.cn/xf2021/learn_exam.html*
  9. // @icon https://blog.gocos.cn/wp-content/uploads/2021/07/2021-07-2782.ico
  10. // @connect api.gocos.cn
  11. // @run-at document-end
  12. // @grant unsafeWindow
  13. // @license MIT
  14. // @require http://libs.baidu.com/jquery/2.0.0/jquery.min.js
  15. // ==/UserScript==
  16.  
  17. var _self = unsafeWindow,
  18. $ = _self.jQuery || top.jQuery,
  19. columnId = getQueryVariable("columnId"),
  20. answer_list = [],
  21. exam_list = [],
  22. time = 3000, // 答题间隔时间,最好为3000mss
  23. num = {"A": 1,"B": 2, "C": 3, "D": 4};
  24.  
  25. (function() {
  26. if (window.location.pathname == '/xf2021/learn_exam.html') {
  27. getExam();
  28. let t = setInterval( function() {
  29. doExam(t)
  30. },time);
  31. } else if (window.location.pathname == '/xf2021/learn-practice.html') {
  32. getAnswer(columnId);
  33. let t = setInterval( function() {
  34. doQuestion(t)
  35. },time);
  36. }
  37. })();
  38.  
  39. // 解析url参数
  40. function getQueryVariable(variable) {
  41. var query = window.location.search.substring(1);
  42. var vars = query.split("&");
  43. for (var i=0;i<vars.length;i++) {
  44. var pair = vars[i].split("=");
  45. if(pair[0] == variable){return pair[1];}
  46. }
  47. return(false);
  48. };
  49.  
  50. // 正则匹配
  51. function getStr(str, start, end) {
  52. let res = str.match(new RegExp(`${start}(.*?)${end}`))
  53. return res ? res[1] : null
  54. }
  55.  
  56. // 获取答案
  57. function getAnswer(columnId) {
  58. var html = $("html").html(),
  59. taskId = getStr(html,'&taskId=','`,')
  60.  
  61. $.ajax({
  62. url: _self.config.practice.host + _self.config.practice.practice + "?columnId="+ columnId + "&taskId=" + taskId,
  63. headers: _self.config.apiConfig.header,
  64. async: false,
  65. success: function (res) {
  66. const { data, status } = res;
  67. if (status === "0") {
  68. var question_data = res.data
  69. var questionBankList = data.questionBankList
  70. answer_list = questionBankList;
  71. upload(question_data)
  72. } else if (status === "1") {
  73. //无效的columnId(下个接口是chapterId)
  74. alert("请先学习当前模块");
  75. window.history.go(-1);
  76. } else if (status === "-2") {
  77. alert("请重新登陆");
  78. } else {
  79.  
  80. }
  81. },
  82. error: function (err) {
  83. }
  84. });
  85. }
  86.  
  87. // 答题操作
  88. function doQuestion(t) {
  89. var cur_topic = $('#currentTopic').text(),
  90. tol_topic = $('#totalTopic').text(),
  91. answer = answer_list[cur_topic - 1].answer;
  92. $('#exam_answer > div:nth-child(' + num[answer] + ')').click();
  93. if (cur_topic == tol_topic) {
  94. // 清除Interval的定时器
  95. clearInterval(t);
  96. setTimeout(function(){alert('答题完成')},time / 2)
  97. } else{
  98. setTimeout(function(){$('#next_question').click()},time / 2);
  99. };
  100. }
  101.  
  102. // 获取考试题目
  103. function getExam(){
  104. var html = $("html").html(),
  105. taskId = getStr(html,'taskId=','`,');
  106. $.ajax({
  107. url: _self.config.wexam.host + _self.config.wexam.getPaper + "?taskId=" + taskId,
  108. headers: _self.config.apiConfig.header,
  109. async: false,
  110. success: function (res) {
  111. const { data, status, message } = res;
  112. if (status === "0") {
  113. var question_data = res.data;
  114. var paper = question_data.paper;
  115. var paperInfo = paper.paperInfo;
  116. exam_list = paperInfo;
  117. } else {
  118. alert('获取考试题目失败!')
  119. }
  120. },
  121. error: function (err) {
  122. }
  123. });
  124. }
  125. // 考试答题操作
  126. function doExam(t){
  127. var cur_topic = $('#currentTopic').text(),
  128. tol_topic = $('#totalTopic').text(),
  129. questionInfo = exam_list[cur_topic - 1];
  130. $.ajax({
  131. url: 'https://api.gocos.cn/index.php/cxapi/xf/getAnswer?v=2',
  132. type: 'POST',
  133. data: {
  134. 'question': questionInfo.content,
  135. 'answerops':questionInfo.answerOptions,
  136. 'topicId': questionInfo.id
  137. },
  138. async: false,
  139. success: function (res) {
  140. if (res.code == 1) {
  141. var data = res.data;
  142. var answer = data[0].answer
  143. $('#exam_answer > div:nth-child(' + num[answer] + ')').click();
  144. } else {
  145. var msg = res.msg;
  146. alert(msg)
  147. }
  148. },
  149. error: function (err) {
  150. }
  151. });
  152. if (cur_topic == tol_topic) {
  153. // 清除Interval的定时器
  154. clearInterval(t);
  155. setTimeout(function(){alert('答题完成')},time / 2);
  156. } else{
  157. setTimeout(function(){$('#next_question').click()},time / 2);
  158. };
  159.  
  160. }
  161.  
  162. function upload(question_data) {
  163. $.ajax({
  164. url: 'https://api.gocos.cn/index.php/cxapi/xf/upload',
  165. type: 'POST',
  166. data: {'data': question_data},
  167. async: true,
  168. success: function (res) {
  169. },
  170. error: function (err) {
  171. }
  172. });
  173. }

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址