一个自动练习的脚本,写了好几个小时,感觉有所收获,记录如下
1,网站应该是用了vue框架导致input.value无法赋值,vlaue值可以看到,但一提交就显示未输入信息,至今还不知道怎么回事,百度了一下,用一下代码可以解决
usermessage[0].value=userinfo[0]
usermessage[1].value=userinfo[1]
usermessage[2].value=userinfo[2]`
// 创造事件vue创建的文本框,不能通过value值直接赋值
var event = document.createEvent('HTMLEvents');
event.initEvent("input", true, true);
event.eventType = 'message';
// 调度事件
usermessage[0].dispatchEvent(event);
usermessage[1].dispatchEvent(event);
usermessage[2].dispatchEvent(event);
usermessage[3].dispatchEvent(event);
2、网页的alert应该是改写了,网上提供的劫持方法没用,最后只能通过按键精灵解决,汗……
3、油候的GM_getValue和GM_setValue可以存储数组类的数据,我还以为和cookie一样是字符串需要解析
4、GM_getValue和GM_setValue不可以在不同的脚本中共享数据
5、一般不通过window.href()跳转页面,而是直接.click(),这样估计被查到的可能性要小点吧
6、做题时一般时通过问题找正确答案,这样太麻烦了,这次索性将正确的答案变成数组,直接用ABCD的选项去匹配,只要匹配上就选,虽然正确率可能降低,胜在方法比较简单
7、高一、高二的题目居然不一样!!!!
8、自动登录的验证码无解,只能用网上大神的自动识别填充网页验证码脚本
// ==UserScript==
// @name 普法教育02
// @namespace http://tampermonkey.net/
// @version 0.1
// @description try to take over the world!
// @author You
// @match *://qspfw.moe.gov.cn/*
// @match *://static.qspfw.moe.gov.cn/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=moe.gov.cn
// @grant GM_listValues
// @grant GM_getValue
// @grant GM_setValue
// @run-at document-start
// ==/UserScript==
(function() {
'use strict';
// 加载完成后执行的代码
window.alert = function(str){
return ;
}
window.confirm=function(){return true}
var classname='高一'//根据班级选择是否更新数据
if( GM_getValue('classname')!=classname){
GM_setValue('classname',classname)
//学生信息‘学生账号\t学生名\t学生密码@’ 用@分割字符串
var temp='050047790 xxx 244746@050047887 xxxx 294724'
temp=temp.split('@')
GM_setValue('info',temp)
console.log('写入全部学生信息')
}
var info=GM_getValue('info')
// for(var i=0;i<info.length;i++){
// console.log(info[i])
// }
console.log('************'+GM_getValue('classname')+'还剩'+info.length+'人********')
var userinfo=info[0]
userinfo= userinfo.split('\t')
console.log('************当前'+userinfo[1]+'正在练习********')
var login=function(){
let usermessage=document.querySelectorAll('input')//结果是数组[账号,姓名,密码,验证码,同意协议]
// if(document.querySelectorAll('input')[4].checked){
// return
// }
usermessage[0].value=userinfo[0]
usermessage[1].value=userinfo[1]
usermessage[2].value=userinfo[2]
//验证码测试开始
if(document.querySelectorAll('input')[3].value!='' && document.querySelectorAll('input')[4].checked){
var yanzhengma=document.querySelectorAll('input')[3].value
console.log('获取到了验证码'+yanzhengma)
document.querySelectorAll('input')[3].value=yanzhengma
document.querySelectorAll('button')[1].click()
}
//验证码测试结束
usermessage[4].click()
// 创造事件vue创建的文本框,不能通过value值直接赋值
var event = document.createEvent('HTMLEvents');
event.initEvent("input", true, true);
event.eventType = 'message';
// 调度事件
usermessage[0].dispatchEvent(event);
usermessage[1].dispatchEvent(event);
usermessage[2].dispatchEvent(event);
usermessage[3].dispatchEvent(event);
}
// 综合练习函数,直接通过找正确答案的方法实现
var testnew=function(){
var choice=document.querySelectorAll('.item')
var numb=parseInt(document.querySelector('#currentTopic').textContent)
for (var i=0; i<choice.length;i++){
var text2=choice[i].textContent
//console.log(text2)
for (var ii=0;ii<text1.length;ii++){
if(text2.indexOf(text1[ii])!=-1){
choice[i].click();//选择正确答案
document.querySelector('.next_question').click(); //点击下一题
if(document.querySelector('#submit')&& numb==10){
document.querySelector('#submit').click()
// 为防止反复交卷和点击下一题,当交卷后就要将原函数清空
document.querySelector('#submit').onclick=function(){}
document.querySelector('.next_question').onclick=function(){}
}
return
}
}
}
}
// 练习的函数答案无所谓
var test=function(answer){
var x=0
var i=parseInt(document.querySelector('#currentTopic').textContent)-1
if (answer[i]=='a'){x=0};
if (answer[i]=='b'){x=1}
if (answer[i]=='c'){x=2}
if (answer[i]=='d'){x=3}
document.querySelectorAll('.item')[x].click()
//前往下一题
// document.querySelector('.step').children[1].children[1].click()//点击第二个按钮
if(document.querySelector('.next_question') && document.querySelector('.next_question').style.display==''){
document.querySelector('.next_question').click()
}
//前往下一主题
if(document.querySelector('#next_exam') && document.querySelector('#next_exam').style.display=='block'){
document.querySelector('#next_exam').click()
}
//前往综合练习
if(document.querySelector('#next_eval')&& document.querySelector('#next_eval').style.display=='block'){
document.querySelector('#next_eval').click()
}
//前往交卷
if(document.querySelector('#submit')&& document.querySelector('#submit').style.display=='block'){
document.querySelector('#submit').click()
document.querySelector('#submit').style.display='none'
}
}
var answer1=['a','b','c','c','d']//练习一的正确答案
var answer2=['d','a','b','c','b']
var answer3=['c','b','c','d','a']
//综合练习的答案,也就是其他练习的答案综合
var text1=['序言','遵守劳动纪律','工人阶级;工农联盟','人民代表大会制度','宪法规定了国家的根本制度,但没有规定国家的根本任务','实施;实施','五','国家建立健全同经济发展水平相适应的社会保障制度','1982年宪法公布施行后,全国人大对现行宪法的所有条款都作了必要的修正','宪法和法律委员会','设区的市的人大及其常委会可以依照法律规定制定地方性法','劳动者有随时休假的权利','按劳分配;同工同酬','八小时','遵守劳动纪 ','劳动是公民的权利不是义务','十一个坚持','1999','党的十四大提出依法治国、建设社会主义法治国家','科学;严格;公正','全面推进依法治国,要坚持法治国家、法治政府、法治社会各自建设','人民代表大会制度','全国人民代表大会和地方各级人民代表大会','民主集中制','平等团结互助和谐','民主选举','宪法','全国人大及其常委会','民法典','乡规民约','改革开放']
//如果是登录页则登录
var time= window.setInterval(function(){
if(window.location.href=='https://static.qspfw.moe.gov.cn/user/#/user/login'){
window.setTimeout(login,500)
}
//如果是首页则跳到登录
else if(window.location.href=='https://static.qspfw.moe.gov.cn/xf2023/index.html'){
console.log('这是首页')
window.location.href ='https://static.qspfw.moe.gov.cn/user/#/user/login'
// window.setTimeout(function(){
// document.querySelector('#login').click();
// },500);
}
//如果是开始学习,则跳到第一练习
else if(window.location.href=='https://static.qspfw.moe.gov.cn/user/#/activity'){
console.log('这是开始学习')
window.setTimeout(function(){
document.querySelector('button').click();
},500);
}
//如果是学习列表则跳到学习
else if(window.location.href=='https://static.qspfw.moe.gov.cn/xf2023/learn_practice_list.html'){
window.setTimeout(function(){
document.querySelector('.button').children[0].click()
},500);
}
//如果是学习,则跳到练习
else if(window.location.href.indexOf('https://static.qspfw.moe.gov.cn/xf2023/learning-page.html')!=-1){
window.setTimeout(function(){
document.querySelector('#afterClassPractice').click()
},500);
}
//开始练习
else if(window.location.href=='https://static.qspfw.moe.gov.cn/xf2023/learn-practice.html?columnId=332'||window.location.href=='https://static.qspfw.moe.gov.cn/xf2023/learn-practice.html?columnId=335'){
window.setTimeout(function(){
test(answer1)
},500);
}
//开始练习
else if(window.location.href=='https://static.qspfw.moe.gov.cn/xf2023/learn-practice.html?columnId=333'||window.location.href=='https://static.qspfw.moe.gov.cn/xf2023/learn-practice.html?columnId=336'){
window.setTimeout(function(){
test(answer2)
},500);
}
//开始练习
else if(window.location.href=='https://static.qspfw.moe.gov.cn/xf2023/learn-practice.html?columnId=334'||window.location.href=='https://static.qspfw.moe.gov.cn/xf2023/learn-practice.html?columnId=337'){
window.setTimeout(function(){
test(answer3)
},500);
}
//综合练习
else if(window.location.href=='https://static.qspfw.moe.gov.cn/xf2023/learn_exam.html'){
window.setTimeout(function(){
testnew()
document.querySelector('.msg-footer').children[0].click()
},500);
}
//奖状页面则退出
else if(window.location.href=='https://static.qspfw.moe.gov.cn/xf2023/evaluation.html'){
clearInterval(time)
window.setTimeout(function(){
info.shift()//删除第一个学生信息,然后再次将学生信息持久化
if(info.length==0){
alert(GM_getValue('classname')+'所有学生填写完毕')
}
GM_setValue('info',info)
console.log('我删除了')
clearInterval(time)
location.href='https://static.qspfw.moe.gov.cn/user/#/user/login'
document.querySelector('#logout').href='https://static.qspfw.moe.gov.cn/user/#/user/login'
document.querySelector('#logout').click()
// document.querySelector('#logout').href=''
},500);
}
},500)
// Your code here...
})();