关于某看视频学习网站前端Javascript代码求助

问题场景

某看视频学习网站,视频播放的途中会弹框,出现答题框,如果不手动选择一个正确选项提交的话,视频无法继续播放。本人菜鸟一枚,尝试使用you猴编写自己的脚本,实现设想的功能如下:
        1.定时检测是否出现答题的弹框--已经实现;
        2.若检测到弹框存在,则选中A选项,提交;
        3.若检测到弹框存在,则选中B选项,提交;
但是,一直弹框,提示“答案错误,请重新作答”。
题目区域的代码如下:

<script>
  function finishTest() {
    var array = $('input[name="response"]:checked');
    var value = [];
    for (let i = 0; i < array.length; i++) {
      value.push($(array[i]).val());
    }
    var answerList;
    if ('Choice0'.includes(',')) {
      answerList = JSON.parse('Choice0');
    } else {
      answerList = ['Choice0'];
    }
    var isRight = true;
    if (value.length != answerList.length){
      isRight = false;
    }else{
      for (let i = 0; i < answerList.length; i++) {
        if (!value.includes(answerList[i])) {
          isRight = false;
        }
      }
    }
    if (!isRight) {
      alert('答案错误,请重新作答');
      // $('input[name="response"]').removeAttr('checked');
      $('#questionDiv').stopTime('C');
      setQustTime();
    } else {
      $('#questionDiv').stopTime('C');
      $('.mylayer-closeico').trigger('click');
      parent.player.videoPlay();
    }
  }

  $(function() {
    //点选按钮模拟
    $('.m-radio-tick input').bindCheckboxRadioSimulate();
    //多选按钮模拟
    $('.m-checkbox-tick input').bindCheckboxRadioSimulate();
    setQustTime();
  });

  function setQustTime() {
    $('#questionDiv').oneTime('900s', 'C', function() {
      confirm('你已超时,将退出学习页', function() {
        window.location.href = '/uc/store/courseRegister';
      }, function() {
        window.location.href = '/uc/store/courseRegister';
      });
    });
  }

</script>


初步的效果如下(选中A,提交提示“答案错误”;选中B提交,也提示“答案错误”,弹框无法关闭,视频就无法继续播放):

自己编写的部分代码如下:


    // 检测弹框问题"questionDiv"是否存在.
    function testQuestionShow() {
        var ff ="";
        var questionDiv = document.getElementById("questionDiv");
        if (!questionDiv) {
            clearInterval(ff);
            console.log("检测到questionDiv不存在");
        } else {
            console.log("检测到questionDiv存在");
            ff = setInterval(function () {
                //每隔1分钟检测一次
                // 生成1到10之间的随机数
                var randomNumber = Math.floor(Math.random() * 10) + 1;
                // 判断随机数是奇数还是偶数,并执行相应的函数
                if (randomNumber % 2 !== 0) {
                    dochoice0();// 如果是奇数,执行dochoice0
                } else {
                    dochoice1();// 否则执行dochoice1
                }
                // 使用CSS选择器找到特定的<a>元素
                var submitBtn = document.querySelector("#questionDiv > div > div > div > div > a");
                // 触发<a>元素的click事件
                submitBtn.click();
                finishTest();
                console.log("自动提交完成!");
            }, 10000);
        }
    }
    //自动提交 A选项
    function dochoice0() {
        // 使用CSS选择器找到特定的<strong>元素
        var cha = document.querySelector("#questionDiv > div > li > div > ol > li:nth-child(1) > label > strong");
        // 给找到的<strong>元素添加类名"on"
        cha.classList.add("on");
        console.log("选中A选项");
        // 使用CSS选择器找到特定的<strong>元素
        var chbb = document.querySelector("#questionDiv > div > li > div > ol > li:nth-child(2) > label > strong");

        // 检查该元素是否具有"on"类,如果有,则移除它
        if (chbb.classList.contains('on')) {
            chbb.classList.remove('on');
            console.log("取消选中B选项");
        }
    }


    //自动提交 B选项
    function dochoice1() {
        // 使用CSS选择器找到特定的<strong>元素
        var chaa = document.querySelector("#questionDiv > div > li > div > ol > li:nth-child(1) > label > strong");
        // 检查该元素是否具有"on"类,如果有,则移除它
        if (chaa.classList.contains('on')) {
            chaa.classList.remove('on');
            console.log("取消选中A选项");
        }
        // 使用CSS选择器找到特定的<strong>元素
        var strongElement = document.querySelector("#questionDiv > div > li > div > ol > li:nth-child(2) > label > strong");
        // 给找到的<strong>元素添加类名"on"
        strongElement.classList.add("on");
        console.log("选中B选项");
    }

添加的依赖js文件:

// @require      http://libs.baidu.com/jquery/2.0.0/jquery.min.js
// @require       https://jsxx.gdedu.gov.cn/common/js/common.base.min.js
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_xmlhttpRequest

主要原因定位猜测:

            1.跟题目下面的嵌入的<script>的函数function finishTest() 有关;

            2.跟依赖的脚本JQuery的定义及拓展有关,附上脚本链接;

https://jsxx.gdedu.gov.cn/common/js/common.base.min.jsicon-default.png?t=N7T8https://jsxx.gdedu.gov.cn/common/js/common.base.min.js

请大神帮忙看看怎么实现,自动化提交。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值