问题场景
某看视频学习网站,视频播放的途中会弹框,出现答题框,如果不手动选择一个正确选项提交的话,视频无法继续播放。本人菜鸟一枚,尝试使用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的定义及拓展有关,附上脚本链接;
请大神帮忙看看怎么实现,自动化提交。