驾考宝典,模拟考试自动做题

驾考宝典模拟考试自动做题

目前为 2024-04-11 提交的版本。查看 最新版本

// ==UserScript==
// @name         驾考宝典,模拟考试自动做题
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  驾考宝典模拟考试自动做题
// @author       ZouYS
// @match        https://www.jiakaobaodian.com/mnks/exam/*.html
// @icon         https://www.jiakaobaodian.com/favicon.ico
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    var quesList=[];
    //劫持函数
        function addXMLRequestCallback(callback) {
          // oldSend 旧函数 i 循环
          var oldSend, i;
          //判断是否有callbacks变量
          if (XMLHttpRequest.callbacks) {
            //判断XMLHttpRequest对象下是否存在回调列表,存在就push一个回调的函数
            XMLHttpRequest.callbacks.push(callback);
          } else {
            //如果不存在则在xmlhttprequest函数下创建一个回调列表/callback数组
            XMLHttpRequest.callbacks = [callback];
            // 保存 XMLHttpRequest 的send函数
            oldSend = XMLHttpRequest.prototype.send;
            //获取旧xml的send函数,并对其进行劫持(替换)  function()则为替换的函数
            //以下function函数是一个替换的例子
            XMLHttpRequest.prototype.send = function () {
              // 把callback列表上的所有函数取出来
              for (i = 0; i < XMLHttpRequest.callbacks.length; i++) {
                // 把this传入进去
                XMLHttpRequest.callbacks[i](this);
              }
              //循环回调xml内的回调函数
              // 调用旧的send函数 并传入this 和 参数
              oldSend.apply(this, arguments);
              //由于我们获取了send函数的引用,并且复写了send函数,这样我们在调用原send的函数的时候,需要对其传入引用,而arguments是传入的参数
            };
          }
        }
        //传入回调 接收xhr变量
        addXMLRequestCallback(function (xhr) {
          //调用劫持函数,填入一个function的回调函数
          //回调函数监听了对xhr调用了监听load状态,并且在触发的时候再次调用一个function,进行一些数据的劫持以及修改
          xhr.addEventListener("load", function () {
            // 输入xhr所有相关信息
            //console.log(xhr);
            if (xhr.readyState == 4 && xhr.status == 200) {
              //  如果xhr请求成功 则返回请求路径
              //console.log("函数1", xhr.responseURL);
               //console.log(xhr.response)
                if(xhr.responseURL.includes('https://api2.jiakaobaodian.com/api/open/question/question-list.htm')){
                    //console.log(JSON.parse(xhr.response))
                    let list=JSON.parse(xhr.response);
                    quesList=[...quesList,...(list.data)]
                    console.log(quesList)
                }
            }
          });

        });

   async function domain(){
        let index=0;
        for(index;index<quesList.length;index++)
        {
            await sleep(500);
            //点击并查询正确答案
            let questionId=document.querySelector('[data-questionid]').getAttribute('data-questionid')
            let result=quesList.filter(item => item.questionId==questionId)
            result=result[0]
            let answer=result.answer.toString()
            console.log(answer)
            switch(answer)
            {
                    case '16':
                    answer=result.optionA
                    break;
                    case '32':
                    answer=result.optionB
                    break;
                    case '64':
                    answer=result.optionC
                    break;
                    case '128':
                    answer=result.optionD
                    break;
                default:
                    answer='error get answer'
            }
            console.log(answer)
            let letter='A'
            var container = await document.querySelector('.detail-content#ComQuestionInfo_qundefined');
            if (container) {
                var paragraphs =await container.querySelectorAll('p'); // 获取父元素内部的所有 <p> 元素
              await paragraphs.forEach(async function(paragraph) {
                    if(paragraph.textContent.includes(answer)){
                       //console.log(paragraph.textContent.split('、')[0]); // 输出每个 <p> 元素的文本内容
                        letter=paragraph.textContent.split('、')[0]
                         var selector = '.select-w.right .select-lable[data-key="' + letter + '"]';
                        // 使用构建好的选择器字符串来获取相应的按钮元素
                        var button = await document.querySelector(selector);
                        button.click();
                    }
                });
            }

        }

    }

     let showWindow = document.createElement('button')
    showWindow.style.width = '60px'
    showWindow.style.height = '40px'
    showWindow.innerText = '开始!'
    showWindow.style.position = 'fixed'
    showWindow.style.left = '0px'
    showWindow.style.top = '150px'
    showWindow.style.zIndex = '111'
    showWindow.addEventListener('click', fuc_show)
    function fuc_show() {
      // let is_show=document.querySelector('#myWindow')
      if (quesList.length==100) {
        domain();

      }
      else {
       alert('wrong init!Please refresh again!')
      }
    }
    document.body.appendChild(showWindow)
    async function sleep(time) {
      console.log('睡眠' + time / 1000 + 's')
      //if(time < 1000)time=1000
      return await new Promise((resolve) => setTimeout(resolve, time));
    }
})();

QingJ © 2025

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