• 注册
  • 转载分享 转载分享 关注:171 内容:12813

    全国学生“学宪法 讲宪法”活动,自动答题脚本

  • 查看作者
  • 打赏作者
  • 当前位置: 七七博客 > 转载分享 > 正文
    • 转载分享
    • 需要安装有游猴浏览器插件~

      // ==UserScript==
      // @name         趣味我吧课后练习、考试自动答题
      // @namespace    moliangqw
      // @version      1.0
      // @description  第六届全国学生“学宪法 讲宪法”活动
      // @author       来自(Ne-21)微信烟雨天青色
      // @match        *://static.qspfw.moe.gov.cn/xf2021/learn-practice.html*
      // @match        *://static.qspfw.moe.gov.cn/xf2021/learn_exam.html*
      // @icon         https://bbs.qw9v.com/favicon.ico
      // @connect      api.gocos.cn
      // @run-at       document-end
      // @grant        unsafeWindow
      // @license      MIT
      // @require      http://libs.baidu.com/jquery/2.0.0/jquery.min.js
      // ==/UserScript==
       
      var _self = unsafeWindow,
          $ = _self.jQuery || top.jQuery,
          columnId = getQueryVariable("columnId"),
          answer_list = [],
          exam_list = [],
          time = 3000, // 答题间隔时间,最好为3000mss
          num = {"A": 1,"B": 2, "C": 3, "D": 4};
       
      (function() {
          if (window.location.pathname == '/xf2021/learn_exam.html') {
              alert('点击确定开始考试(使用方法视频请关注‘趣味我吧’公众号)')
              getExam();
              let t = setInterval( function() {
                  doExam(t)
              },time);
          } else if (window.location.pathname == '/xf2021/learn-practice.html') {
              alert('点击确定开始练习(使用方法视频请关注‘趣味我吧’公众号)')
              getAnswer(columnId);
              let t = setInterval( function() {
                  doQuestion(t)
              },time);
          }
      })();
       
      // 解析url参数
      function getQueryVariable(variable) {
             var query = window.location.search.substring(1);
             var vars = query.split("&");
             for (var i=0;i<vars.length;i++) {
                     var pair = vars[i].split("=");
                     if(pair[0] == variable){return pair[1];}
             }
             return(false);
      };
       
      // 正则匹配
      function getStr(str, start, end) {
          let res = str.match(new RegExp(`${start}(.*?)${end}`))
          return res ? res[1] : null
      }
       
      // 获取答案
      function getAnswer(columnId) {
          var html = $("html").html(),
              taskId = getStr(html,'&taskId=','`,')
       
          $.ajax({
              url: _self.config.practice.host + _self.config.practice.practice + "?columnId="+ columnId + "&taskId=" + taskId,
              headers: _self.config.apiConfig.header,
              async: false,
              success: function (res) {
                  const { data, status } = res;
                  if (status === "0") {
                      var question_data = res.data
                      var questionBankList = data.questionBankList
                      answer_list = questionBankList;
                      upload(question_data)
                  } else if (status === "1") {
                      //无效的columnId(下个接口是chapterId)
                      alert("请先学习当前模块");
                      window.history.go(-1);
                  } else if (status === "-2") {
                      alert("请重新登陆");
                  } else {
       
                  }
              },
              error: function (err) {
              }
          });
      }
       
      // 答题操作
      function doQuestion(t) {
          var cur_topic = $('#currentTopic').text(),
              tol_topic = $('#totalTopic').text(),
              answer = answer_list[cur_topic - 1].answer;
          $('#exam_answer > div:nth-child(' + num[answer] + ')').click();
          if (cur_topic == tol_topic) {
              // 清除Interval的定时器
              clearInterval(t);
              setTimeout(function(){alert('答题完成')},time / 2)
          } else{
              setTimeout(function(){$('#next_question').click()},time / 2);
          };
      }
       
      // 获取考试题目
      function getExam(){
          var html = $("html").html(),
              taskId = getStr(html,'taskId=','`,');
          $.ajax({
              url: _self.config.wexam.host + _self.config.wexam.getPaper + "?taskId=" + taskId,
              headers: _self.config.apiConfig.header,
              async: false,
              success: function (res) {
                  const { data, status, message } = res;
                  if (status === "0") {
                      var question_data = res.data;
                      var paper = question_data.paper;
                      var paperInfo = paper.paperInfo;
                      exam_list = paperInfo;
                  } else {
                      alert('获取考试题目失败!')
                  }
              },
              error: function (err) {
              }
          });
      }
      // 考试答题操作
      function doExam(t){
          var cur_topic = $('#currentTopic').text(),
              tol_topic = $('#totalTopic').text(),
              questionInfo = exam_list[cur_topic - 1];
          $.ajax({
              url: 'https://api.gocos.cn/index.php/cxapi/xf/getAnswer?v=2',
              type: 'POST',
              data: {
                  'question': questionInfo.content,
                  'answerops':questionInfo.answerOptions,
                  'topicId': questionInfo.id
              },
              async: false,
              success: function (res) {
                  if (res.code == 1) {
                      var data = res.data;
                      var answer = data[0].answer
                      $('#exam_answer > div:nth-child(' + num[answer] + ')').click();
                  } else {
                      var msg = res.msg;
                      alert(msg)
                  }
              },
              error: function (err) {
              }
          });
          if (cur_topic == tol_topic) {
               // 清除Interval的定时器
               clearInterval(t);
               setTimeout(function(){alert('答题完成')},time / 2);
          } else{
               setTimeout(function(){$('#next_question').click()},time / 2);
          };
       
      }
       
      function upload(question_data) {
          $.ajax({
              url: 'https://api.gocos.cn/index.php/cxapi/xf/upload',
              type: 'POST',
              data: {'data': question_data},
              async: true,
              success: function (res) {
              },
              error: function (err) {
              }
          });
      }

      隐藏内容需要登录才可以看见

      登录

      请登录之后再进行评论

      登录
    • 任务系统
    • 帖子间隔 侧栏位置: