當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
JavaScript——易班优课YOOC课群在线测试自动答题解决方案(二十)整理维护
生活随笔
收集整理的這篇文章主要介紹了
JavaScript——易班优课YOOC课群在线测试自动答题解决方案(二十)整理维护
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
Web安全——易班優課YOOC課群在線測試自動答題解決方案
解決方案
JavaScript腳本?
// ==UserScript== // @name 易班優課YOOC浙理助手 // @namespace http://tampermonkey.net/ // @version 0.2.5 // @description 浙江理工大學易班優課YOOC測試、刷題 // @author STZG // @match *://*.yooc.me/* // @grant unsafeWindow // @grant GM_xmlhttpRequest // @grant GM_setClipboard // @require https://code.jquery.com/jquery-3.5.1.js#sha256=QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc= // @updateURL https://*/*/yooc/yooc_helper.js // @license MIT // ==/UserScript==(function() {// 設置修改后,需要刷新或重新打開網課頁面才會生效const setting = {id:[[${ID}]],baseURL:"https://*/*",yoocURL:"https://www.yooc.me",model:"debug"};let url;let _self,top;let parent,Ext,UE,vjs;if(typeof unsafeWindow!=="undefined"){_self = unsafeWindow;top = _self;$ = _self.jQuery || top.jQuery;parent = _self === top ? self : _self.parent;Ext = _self.Ext || parent.Ext || {};UE = _self.UE;vjs = _self.videojs;}const log={LEVEL:{info:0,debug:1},logMessage:(msg,level)=> {if (log.LEVEL[setting.model]>=log.LEVEL[level]){console.log(msg)}},info:(msg)=> log.logMessage(msg, "info"),debug: (msg)=> log.logMessage(msg, "debug")};const ajaxUsualErrorMessage=(e) =>{if(e.status === 500){//頁面函數xAlert('失敗','網絡請求失敗')}else if(e.status === 404){//頁面函數xAlert('失敗','找不到網頁')}};const htmlToQuestionObj=(id,questionHTML)=>{return {id:id,question:questionHTML.replace(/the-ans fls/g,"the-ans crt").replace(/<li class="crt"/g,'<li class=""').replace(/<li class="fls"/g,'<li class=""')}};let analysisDetailAnswerPage=(pagehtml)=>{log.debug(pagehtml);//創建DOMlet pageDOM=document.createElement("html");pageDOM.innerHTML=pagehtml;let page=$(pageDOM);log.debug(page);//獲取考試信息let groupData=page.find('#group-data');let groupId=groupData.attr("data-group-id");let examId=groupData.attr("data-exam-id");//獲取問題信息let question=page.find('.question-board');log.debug(question);//數據封裝let question_arr=[];question.each((index,q)=>{question_arr.push(htmlToQuestionObj(q.id,q.outerHTML));let aq=$("#"+q.id).find('.the-ans');if(aq.length === 1){aq.html($(q).find('.the-ans').html())}})return {group:groupId,exam:examId,questions:question_arr}}let getDetailPage=(groupId,examId)=>{return $.ajax({//請求方式type : "GET",//請求地址url :setting.yoocURL + '/group/'+groupId+'/exam/'+examId+'/detail'});}let uploadAnswer=(groupId,examId,question_arr)=>{return $.ajax({//請求方式type : "POST",//請求的媒體類型contentType: "application/json;charset=UTF-8",headers:{'JS-ID':setting.id},//請求地址url : setting.baseURL + "/yooc/group/"+groupId+"/exam/"+examId+"/upload",//數據,json字符串data : JSON.stringify(question_arr)});}let getAnswer=(groupId,examId,question_arr)=>{return $.ajax({//請求方式type : "GET",headers:{'JS-ID':setting.id},//請求地址url : setting.baseURL + "/yooc/group/"+groupId+"/exam/"+examId+"/answer?question="+question_arr,});}let unblock=()=>{//選擇document.onselectstart= function(e){e = e || window.event;e.returnValue = true;return true;}//鼠標按下document.onmousedown = function(e) {e = e || window.event;e.returnValue = true;return true;}//鼠標右鍵菜單document.oncontextmenu = function(e) {e = e || window.event;e.returnValue = true;return true;}window.onkeydown = function(e) {//Ctrl+S 保存if (e.ctrlKey && e.keyCode == 83) {e.returnValue = true;return true;}//Ctrl+P 打印if (e.ctrlKey && e.keyCode == 80) {e.returnValue = true;return true;}//Ctrl+C 復制if (e.ctrlKey && e.keyCode == 67) {e.returnValue = true;return true;}}}let examsPage=()=>{//獲取考試信息let group=document.getElementById('group-data')let groupId=group.getAttribute("data-group-id")let csrf=group.getAttribute("data-csrf")let auth=group.getAttribute("data-auth")let seeExam=(exam)=>{let a_score=exam.getElementsByClassName('score')[0]if(a_score&&a_score.innerHTML==='禁止查卷'){let t_bgc=document.getElementsByClassName('t-bgc fl')[7]let edit_history_url=t_bgc.getElementsByTagName('a')[0].hrefa_score.href=edit_history_url.replace('edit_history','detail')a_score.innerHTML='查看詳情'}}let autoPractice=(exam)=>{let search=exam.getElementsByClassName('board-bottom')[0]if(search){return}let examId=exam.getAttribute("data-exam-id")let sum=Number(/(.*)\u9898/.exec(exam.getElementsByClassName('board-det')[0].childNodes[1].getElementsByTagName('span')[0].innerText.trim())[1])let is_repeat=exam.getElementsByClassName('board-left')[0].childNodes[25].innerText.trim()==='允許'let template='<div class="fl board-bottom robot" style="width: 770px;"><div class="fl board-left" style=" height: 40px!important;line-height: 40px;font-size: 14px;padding: 0 10px;"><div class="progress-tar" style="width:70%;display: inline-block;"><span style="line-height: 20px;height: 20px;">刷題進度:</span><progress class="progress" value="30" max="100" style="width: 60%;border-radius: 2px;border-left: 1px #ccc solid;border-right: 1px #ccc solid;border-top: 1px #aaa solid;background-color: #eee;margin-bottom: 1px;">您的瀏覽器不支持progress元素</progress><span style="margin: 10px; line-height: 20px;"><span class="count-practice">0</span>/<span class="sum-practice"></span></span></div><div style="width: 25%;text-align: center;display: inline-block;"><span class="status-practice" style="margin: 10px;">正在xxxx</span></div><div style="width: 5%;display: inline-block;text-align: center;"><a href="https://www.shentuzhigang.top:8443/MyZSTU/favicon.ico"><svg t="1591623102863" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2609" width="32" height="32" style="width: 16px;height: 16px;"><path d="M859.990016 634.09664l0 139.674624c0 39.171072-30.993408 70.9376-69.229568 70.9376L236.429312 844.708864c-38.23616 0-69.229568-31.766528-69.229568-70.9376L167.199744 634.09664l-101.376 0 0 173.45024c0 78.331904 68.68992 141.860864 145.16224 141.860864l617.2416 0c76.473344 0 138.464256-63.52896 138.464256-141.860864L966.69184 634.09664 859.990016 634.09664zM754.766848 383.643648l-135.146496 1.11104L618.540032 66.707456 409.6256 66.707456l0 318.047232L274.47296 384.754688l240.146432 319.127552L754.766848 383.643648z" p-id="2610"></path></svg><a></div></div><div class="fl board-right" style="height: 40px!important;line-height:40px;font-size:14px;background-color: #fe8333;cursor: pointer;color: white;"><div class="button-practice"></div></div></div>'//添加偽元素CSSdocument.styleSheets[0].addRule('progress::-webkit-progress-bar','background-color: #d7d7d7;'); // 支持IEdocument.styleSheets[0].addRule('progress::-webkit-progress-value','background-color: #aadd6a;'); // 支持IElet board_bottom=document.createElement('div')board_bottom.innerHTML=templateboard_bottom=board_bottom.childNodes[0]exam.appendChild(board_bottom)let button_practice=board_bottom.getElementsByClassName('button-practice')[0]let count_practice=board_bottom.getElementsByClassName('count-practice')[0]let sum_practice=board_bottom.getElementsByClassName('sum-practice')[0]let status_practice=board_bottom.getElementsByClassName('status-practice')[0]let progress=board_bottom.getElementsByClassName('progress')[0]sum_practice.innerHTML=sumprogress.value=0/sum*100count_practice.innerHTML=0let updateFlag=false;let now_sum=0;let updateStatus=(o)=>{now_sum=oif(updateFlag){return}else{let timer=setInterval(()=>{let now_num=Math.round(Number(count_practice.innerHTML))let cmp=now_sum-now_numif(cmp===0){updateFlag=false;clearInterval(timer);}else{if(now_num===0){now_num=1;}else{now_num=now_num+Math.round(cmp/Math.abs(cmp))}count_practice.innerHTML=now_numprogress.value=now_num/sum*100}},50)}}let refreshStatusSum=()=>{updateStatus(0)return $.ajax({url:setting.baseURL + '/yooc/group/'+groupId+'/exam/'+examId+'/answer/total',type:'get',headers:{'JS-ID':setting.id},success:(res)=>{log.debug(res)updateStatus(res.data)}})}if(is_repeat){button_practice.innerHTML="開始自動刷題"let autoPracticeStatus=falsebutton_practice.onclick=e=>{let exam_status=0let repeat=exam.getElementsByClassName('repeat')[0]if(repeat){if(autoPracticeStatus){autoPracticeStatus=falsebutton_practice.innerHTML="開始自動刷題"window.location.href=document.URL}else{if(now_sum === sum){//頁面方法xAlert('提示','刷題完成進度已經100%')return}autoPracticeStatus=truebutton_practice.innerHTML="關閉自動刷題"let repeat_url=repeat.getAttribute("repeat-url")let start_exam=exam.getElementsByClassName('start_exam')[0]if(start_exam){exam_status=0}else if(repeat){exam_status=4}let autoPracticeMain=()=>{if(now_sum === sum){//頁面方法xAlert('提示','刷題完成')autoPracticeStatus=falsebutton_practice.innerHTML="開始自動刷題"return}if(!autoPracticeStatus){return}log.info("start")status_practice.innerHTML="刷題開始"log.info("apply-repeat")status_practice.innerHTML="申請重做"$.ajax({url: repeat_url,type : "POST",contentType: "application/x-www-form-urlencoded; charset=UTF-8",headers:{'X-CSRFToken':csrf},data: {'csrfmiddlewaresretoken': csrf},success: (res)=>{if(!autoPracticeStatus){return}status_practice.innerHTML="申請重做成功"let data=resif(data.result){log.info('申請重做成功')if(data.url){log.info("practice")if(!autoPracticeStatus){return}status_practice.innerHTML="申請考試頁面"$.ajax({url: data.url,type: 'GET',success: (res)=>{if(!autoPracticeStatus){return}status_practice.innerHTML="自動練習開始"let examuser=/var AnswerData = JSON.parse\(localStorage.getItem\("exam(.*)"\)\) \|\| {.*};/.exec(res)[1]log.debug(examuser)repeat_url=setting.yoocURL + '/group/'+groupId+'/exam/'+examId+'/examuser/'+examuser+'/repeat'let practice=document.createElement('html')practice.innerHTML=reslog.debug(practice)//獲取問題信息let question=Array.from(practice.getElementsByClassName('question-board'))let AnswerData={}question.forEach(q=>{log.debug(q)let inputTag=q.getElementsByTagName('input')log.debug(inputTag)if(inputTag.length>0){let Ele=inputTag[0]if(Ele.type==="radio"||Ele.type==="checkbox"){let arr=Ele.id.split('_')let questionId=arr[0]let name=arr[1]let qData=[arr[2]]AnswerData[questionId] = {};AnswerData[questionId][name] = qData;}else if(Ele.type==="text"){let Eles=Array.from(inputTag)Eles.forEach(e=>{e.value="test"let arr=e.id.split('_')let questionId=arr[0]let name=arr[1]AnswerData[questionId] = {};if (AnswerData[questionId][name]===undefined) {AnswerData[questionId][name] = [e.value.trim()];} else {AnswerData[questionId][name].push(e.value.trim());}})}}})status_practice.innerHTML="自動練習完成"log.info("save")localStorage.setItem("exam"+examuser,JSON.stringify(AnswerData));status_practice.innerHTML="提交答案"$.ajax({url: setting.yoocURL + '/group/'+groupId+'/exam/'+examId+'/answer/save',type : "POST",contentType: "application/json; charset=UTF-8",headers:{'X-CSRFToken':csrf},data: JSON.stringify(AnswerData),success: (res)=>{if(!autoPracticeStatus){return}status_practice.innerHTML="提交答案成功"log.info("submit")let submitUrl = setting.yoocURL + '/group/'+groupId+'/exam/'+examId+'/answer/submit';let _AnswerData = [];for(let obj in AnswerData){let m = {};m[obj] = AnswerData[obj];_AnswerData.push(m);}let postData = {'csrfmiddlewaretoken': csrf,'answers': JSON.stringify(_AnswerData),'type': 0,'auto': 0,'completed':1};if(!autoPracticeStatus){return}status_practice.innerHTML="提交試卷"$.ajax({url: submitUrl,type : "POST",contentType: "application/x-www-form-urlencoded; charset=UTF-8",headers:{'X-CSRFToken':csrf},data: postData,success: (res)=>{if(!autoPracticeStatus){return}status_practice.innerHTML="提交試卷成功"log.info("upload")status_practice.innerHTML="申請答案頁面"getDetailPage(groupId,examId).then(detailPage=>{if(!autoPracticeStatus){return}status_practice.innerHTML="答案分析"let info=analysisDetailAnswerPage(detailPage)if(!autoPracticeStatus){return}status_practice.innerHTML="上傳答案"uploadAnswer(info.group,info.exam,info.questions).then(res=>{log.debug(res)if(!autoPracticeStatus){return}status_practice.innerHTML="上傳成功"log.info("end")refreshStatusSum().then((res)=>{if(!autoPracticeStatus){return}log.info("update")status_practice.innerHTML="更新狀態"updateStatus(res.data)log.info("reboot")status_practice.innerHTML="正在重新開始"autoPracticeMain()},(e)=>{//頁面方法xAlert('失敗','網絡請求失敗')autoPracticeStatus=falsebutton_practice.innerHTML="開始自動刷題"window.location.href=document.URL})},(e)=>{//頁面方法xAlert('失敗','網絡請求失敗')autoPracticeStatus=falsebutton_practice.innerHTML="開始自動刷題"window.location.href=document.URL})},(e)=>{//頁面方法xAlert('失敗','網絡請求失敗')autoPracticeStatus=falsebutton_practice.innerHTML="開始自動刷題"window.location.href=document.URL})},error:(e)=>{//頁面方法xAlert('失敗','網絡請求失敗')autoPracticeStatus=falsebutton_practice.innerHTML="開始自動刷題"window.location.href=document.URL}});},error:(e)=>{//頁面方法xAlert('失敗','網絡請求失敗')autoPracticeStatus=falsebutton_practice.innerHTML="開始自動刷題"window.location.href=document.URL}})},error:(e)=>{//頁面方法xAlert('失敗','網絡請求失敗')autoPracticeStatus=falsebutton_practice.innerHTML="開始自動刷題"window.location.href=document.URL}})}}else{//頁面方法xAlert(data.message);autoPracticeStatus=falsebutton_practice.innerHTML="開始自動刷題"window.location.href=document.URL}},error:(e)=>{//頁面方法xAlert('失敗','網絡請求失敗')autoPracticeStatus=falsebutton_practice.innerHTML="開始自動刷題"window.location.href=document.URL}});}autoPracticeMain()}}else{//頁面方法xAlert('失敗','無法獲取重做按鈕,請測試允許反復練習并且確保有重做按鈕在頁面上')}}}else{button_practice.innerHTML="刷新刷題進度"button_practice.onclick=e=>{log.info("start")log.info("get")log.info("update")refreshStatusSum()log.info("end")}}//初始化狀態refreshStatusSum(res=>{},ajaxUsualErrorMessage)status_practice.innerHTML=""}let exams_board=document.getElementsByClassName('exams-board')[0]if(exams_board){let exams=Array.from( exams_board.getElementsByTagName('li'))if(exams||exams!==[]){exams.forEach(exam=>{autoPractice(exam)seeExam(exam)})}else{return}}}let detailPage=()=>{//獲取考試信息let group=document.getElementById('group-data');let groupId=group.getAttribute("data-group-id");let csrf=group.getAttribute("data-csrf");let examId=group.getAttribute("data-exam-id");let examQuestionNum=Number(group.getAttribute("data-questions"));//獲取問題信息let questions=document.getElementsByClassName('question-board');let questionNum = questions.length;//獲取答案信息let ansElements = document.getElementsByClassName('the-ans');let ansNum = ansElements.length;let addIdTag=()=>{//獲取問題信息let question=Array.from(document.getElementsByClassName('question-board'))question.forEach(q=>{let board_type=q.id.split('-')[0]let question_id=q.id.split('-')[1]let idTag=q.getElementsByClassName('question-id-tag')if(idTag.length===0){idTag=document.createElement('span')idTag.classList.add('question-id-tag')q.children[0].appendChild(idTag)}if(board_type==='question'){idTag.innerHTML=" 題目ID:"+question_id}else if(board_type==='answer'){idTag.innerHTML=" 答案ID:"+question_id}})}let nullSubmit=()=>{//獲取問題信息let question=Array.from(document.getElementsByClassName('question-board'))question.forEach(q=>{log.debug(q)let inputTag=q.getElementsByTagName('input')log.debug(inputTag)if(inputTag.length>0){let Ele=inputTag[0]if(Ele.type==="radio"||Ele.type==="checkbox"){if(Ele.type==="radio"){Ele.checked=true;}else if(Ele.type==="checkbox"){Ele.checked=true;}//頁面方法choiceAnswerData($(q))}else if(Ele.type==="text"){let Eles=Array.from(inputTag)Eles.forEach(e=>{e.value="test"})//頁面方法inputAnswerData($(q))}let ele_name=Ele.name.replace(/\_[0-9]+/, '')let questionTag=document.getElementById(ele_name)questionTag.classList.add('done')}})}let writeAnswer=(question)=>{let ansEle=question.getElementsByClassName(('the-ans'))[0]let pattern=/\u6b63\u786e\u7b54\u6848\uff1a(.*)/let ans=ansEle.children[0].innerText.trim()if(pattern.test(ans)){let answers=pattern.exec(ans)[1]let question_id=question.id.split('-')[1]let answers_arr=answers.split('、')answers_arr.forEach(answer=>{if(/[A-Z]/.test(answer)){let li_Eles=question.getElementsByTagName('li')let li_Ele=li_Eles[answer.charCodeAt(0)-65]let input_id=li_Ele.dataset.questionName+"_"+li_Ele.dataset.questionValuelet Ele=document.getElementById(input_id)if(Ele.type==="radio"){Ele.checked=true;}else if(Ele.type==="checkbox"){Ele.checked=true;}//頁面方法choiceAnswerData($(document.getElementById('question-'+question_id)))}else{let input_name=question_id+"_" + (answers_arr.indexOf(answer) + 1)let Ele=$("[name='"+input_name+"']").get(0)if(Ele){Ele.value=answer.split('|')[0]//頁面方法inputAnswerData($(document.getElementById('question-'+question_id)))}}})let questionTag=document.getElementById(question_id)questionTag.classList.add('done')}}let autoAnswer=()=>{let ansElements = Array.from(document.getElementsByClassName('the-ans'))ansElements.forEach(ansEle=>{writeAnswer(ansEle.parentNode)})}let copyMobileAnswer=()=>{//獲取問題信息questions=$('.question-board')let questionContainer=document.getElementsByClassName('exam-detial-container')[0]log.debug(questionContainer)$.ajax({type : "GET",url:setting.yoocURL + "/mobile/group/"+groupId+"/exams/"+examId+"/subject?view_type=result",}).then(res=>{let pattern_t =/var data=(.*);\n/if(!pattern_t .test(res)){xAlert('提示',res)return}let data=JSON.parse(pattern_t.exec(res)[1])let question_arr=[]let num=0data.forEach(e=>{log.debug(e[2]);e[2].forEach(q=>{let question=document.createElement('div')question.innerHTML=q.questionquestion.classList.add('question-board')question.id=questions.get(num).idlog.debug(question)question_arr.push(htmlToQuestionObj(question.id,question.outerHTML))writeAnswer(question)question.id = 'answer-' + question.id.split('-')[1]questionContainer.insertBefore(question,questions.get(num++))log.debug(num)})})log.debug(question_arr)uploadAnswer(groupId,examId,question_arr)addIdTag()},ajaxUsualErrorMessage)}var repeatExam=()=>{$.ajax({url: 'https://www.yooc.me/group/'+groupId+'/exam/'+examId+'/detail',type: 'GET'}).then(res=>{let examuser=/var AnswerData = JSON.parse\(localStorage.getItem\("exam(.*)"\)\) \|\| {.*};/.exec(res)[1];$.ajax({beforeSend: function(xhr){// 取消掉全局loading效果if($('#mask').length>0){$('#mask').hide();}},url:'https://www.yooc.me/group/'+groupId+'/exam/'+examId+'/examuser/'+examuser+'/repeat',type: 'post',data: {'csrfmiddlewaretoken': csrf},success: function(data){if(data.result){window.location.href = data.url;}else{alert(data.message);}}});},ajaxUsualErrorMessage)}let appendsomething=()=>{let releaseButton=document.getElementsByClassName('release-board')[0]let ns=document.createElement('a')ns.id="null-submit"ns.href="javascript:;"ns.innerText="提交白卷"ns.style="margin-left: 0px;margin-top: 10px;"ns.title="隨機答案提交試卷"ns.onclick=()=>{nullSubmit();//頁面方法xAlert('提示','隨機答案選擇完畢,可以提交了');};releaseButton.appendChild(ns);let qa=document.createElement('a');qa.id="query-answer";qa.href="javascript:;";qa.innerText="查詢答案";qa.style="margin-left: 20px;margin-top: 10px;";qa.title="云端題庫查詢答案";qa.onclick=()=>{let ansElements = document.getElementsByClassName('the-ans');let ansNum = ansElements.lengthif(ansNum<=0){showAnswer();addIdTag()}else{//頁面方法xAlert('失敗','已經有答案了哦')}};releaseButton.appendChild(qa);let autoAnswerButton=document.createElement('a');autoAnswerButton.id="auto-answer-submit";autoAnswerButton.href="javascript:;";autoAnswerButton.innerText="自動答題";autoAnswerButton.style="margin-left: 0px;margin-top: 10px;";autoAnswerButton.title="答案自動選擇";autoAnswerButton.onclick=()=>{autoAnswer();addIdTag();//頁面方法xAlert('提示','答案自動選擇完畢,請自行檢查填空題部分')}releaseButton.appendChild(autoAnswerButton);let copyAnswerButton=document.createElement('a');copyAnswerButton.id="auto-answer-submit";copyAnswerButton.href="javascript:;";copyAnswerButton.innerText="正確答案";copyAnswerButton.style="margin-left: 20px;margin-top: 10px;";copyAnswerButton.title="利用PC端和移動端之間BUG實現獲取正確答案";let checked_a=false;copyAnswerButton.onclick=()=>{let ansElements = document.getElementsByClassName('the-ans');let ansNum = ansElements.length;if(ansNum<=0){copyMobileAnswer();addIdTag();//頁面方法xAlert('提示','正確答案復制完畢,請自行檢查填空題部分');}else{//頁面方法xAlert('失敗','已經有答案了哦');}};releaseButton.appendChild(copyAnswerButton);var repeatButton=document.createElement('a')repeatButton.id="repeat-exam-submit"repeatButton.href="javascript:;"repeatButton.innerText="強制重做"repeatButton.style="margin-left: 0px;margin-top: 10px;"repeatButton.title="強制申請重做試卷"repeatButton.onclick=()=>{repeatExam();//頁面方法xAlert('提示','強制申請重做試卷成功')}releaseButton.appendChild(repeatButton)var tempButton=document.createElement('a')tempButton.id="temp-submit"tempButton.href="javascript:;"tempButton.innerText="占位按鈕"tempButton.style="margin-left: 20px;margin-top: 10px;"tempButton.title="未定義功能"tempButton.onclick=()=>{//頁面方法xAlert('失敗','占位按鈕:未定義功能')}releaseButton.appendChild(tempButton)};let showAnswer=()=>{//獲取問題信息questions=$('.question-board');let question_arr=[];questions.each((index,q)=>{question_arr.push(q.id.substr(9))});getAnswer(groupId,examId,question_arr).then(res=>{log.debug(res);let questionContainer=document.getElementsByClassName('exam-detial-container')[0];res.forEach(q=>{let question=document.getElementById('question-'+q.id);let answer = document.createElement("div");answer.innerHTML = q.question;let child=answer.childNodes[0];child.id='answer-'+q.id;questionContainer.insertBefore(child,question);log.debug(question);log.debug(answer)});addIdTag()},ajaxUsualErrorMessage)};if(ansNum>0){if(questionNum===examQuestionNum){getDetailPage(groupId,examId).then(detailPage=>{let info=analysisDetailAnswerPage(detailPage);uploadAnswer(info.group,info.exam,info.questions).then(res=>{log.debug(res)},ajaxUsualErrorMessage)},ajaxUsualErrorMessage)}else{log.info("現在是考試中,不能上傳答案哦。")}}else{appendsomething();unblock();}addIdTag()};let start=()=>{if(url!=location.pathname){url=location.pathname;log.info(url);if(!url.match(/^\/mobile/)){if(url.match(/\/group\/[0-9]*\/exams.*/)){examsPage()}else if(url.match(/\/group\/[0-9]*\/exam\/[0-9]*\/detail/)){detailPage()}}}};let int=self.setInterval(()=>{start()},1000); })();Java后端
Maven?
<!--Spring Boot Web--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--Spring Mybatis--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.1</version></dependency><!--MyBatis-Plus--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.1.tmp</version></dependency><!--Spring Thymeleaf--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>Controller
package cn.edu.zstu.myzstu.web.controller.YOOC;import cn.edu.zstu.myzstu.dto.ApiResponse; import cn.edu.zstu.myzstu.dto.yooc.ExamDTO; import cn.edu.zstu.myzstu.dto.yooc.QuestionDTO; import cn.edu.zstu.myzstu.model.yooc.Question; import cn.edu.zstu.myzstu.service.IYOOCExamQuestionService; import cn.edu.zstu.myzstu.web.utils.ApiResponseUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*;import javax.servlet.http.HttpServletResponse; import java.util.ArrayList; import java.util.List; import java.util.UUID;/*** @Author ShenTuZhiGang* @Version 1.0.0* @Date 2020-04-26 08:26*/ @Controller @CrossOrigin @RequestMapping("/yooc") public class YOOCController {@AutowiredIYOOCExamQuestionService iyoocExamQuestionService;@ResponseBody@RequestMapping(value = "/group/{groupId}/exam/{examId}/upload",method = RequestMethod.POST)public Object uploadExam(@RequestHeader("JS-ID")String JSID,@PathVariable("groupId")String groupId,@PathVariable("examId")String examId,@RequestBody List<QuestionDTO> questionDTOS){List<Question> questions = new ArrayList<>();for (QuestionDTO questionDTO: questionDTOS) {Question question =new Question(groupId,examId,questionDTO.getId().substring(9),questionDTO.getQuestion());questions.add(question);//System.out.println(question);}iyoocExamQuestionService.saveOrUpdateBatch(questions);return questions;}@ResponseBody@RequestMapping(value = "/group/{groupId}/exam/{examId}/answer",method = RequestMethod.GET)public Object getAnswerByQuestionId(@RequestHeader("JS-ID")String JSID,@PathVariable("groupId")String groupId,@PathVariable("examId")String examId,@RequestParam("question") List<String> questions){List<QuestionDTO> questionDTOS = new ArrayList<>();for (String question:questions) {Question questiona=iyoocExamQuestionService.getById(question);if(questiona==null){questionDTOS.add(QuestionDTO.getNullQuestionDTO(question));}else{questionDTOS.add(new QuestionDTO(questiona.getId(),questiona.getQuestion()));}}return questionDTOS;}@ResponseBody@RequestMapping(value = "/group/{groupId}/exam/{examId}/answer/search",method = {RequestMethod.GET})public Object getUserByPage(@RequestHeader("JS-ID")String JSID,@PathVariable("groupId")String groupId,@PathVariable("examId")String examId,@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10") Integer size,@RequestParam(defaultValue = "false")Boolean onlynow,String key){QueryWrapper<Question> queryWrapper = new QueryWrapper<>();if(onlynow){if(!StringUtils.isEmpty(groupId)){queryWrapper.eq("group",groupId);}if(!StringUtils.isEmpty(examId)){queryWrapper.eq("exam",examId);}}queryWrapper.like("question", key);return iyoocExamQuestionService.page(new Page<>(page,size), queryWrapper);}@ResponseBody@RequestMapping(value = "/group/{groupId}/exam/{examId}/answer/total",method = RequestMethod.GET)public Object getAnswerTotalByQuestionId(@PathVariable("groupId")String groupId,@PathVariable("examId")String examId){int total = iyoocExamQuestionService.count(new QueryWrapper<>(new Question(groupId, examId)));ApiResponse retTemp = ApiResponseUtil.getRetTemp();retTemp.setData(total);return retTemp;}@RequestMapping(value = "/yooc_helper.js",method = RequestMethod.GET)public String updateJS(Model model){model.addAttribute("ID", UUID.randomUUID().toString());return "yooc_helper.js";} }無答案時
public static QuestionDTO getNullQuestionDTO(String id){QuestionDTO question = new QuestionDTO();question.setId(id);question.setQuestion("<div class=\"question-board\" id=\"question-"+id+"\">\n" +" <div class=\"the-ans fls\">\n" +" <p>這一題還沒有正確的答案,快點上傳吧!</p>\n" +" </div>\n" +" </div>");return question;}?數據庫設計
運行結果
考試列表面板?
自動刷題過程
考試面板?
答題參考?
完成標記
模板腳本?
數據庫存儲?
參考文章
https://blog.csdn.net/qq_41523096/article/details/104758355
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的JavaScript——易班优课YOOC课群在线测试自动答题解决方案(二十)整理维护的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JavaScript——易班优课YOOC
- 下一篇: Java——将JSONP返回的结果转化为