// ==UserScript== // @name 青书学堂答案一键填充 // @namespace http://tampermonkey.net/ // @version 0.1 // @description 青书学堂答案一键填充助手 // @author wulai // @match https://*.qingshuxuetang.com/*/ViewExerciseAnswer* // @match https://*.qingshuxuetang.com/*/ExercisePaper* // @icon https://www.google.com/s2/favicons?sz=64&domain=qingshuxuetang.com // @grant unsafeWindow // @grant GM_setValue // @grant GM_getValue // @grant GM_listValues // @grant GM_deleteValue // @resource https://unpkg.com/browse/bootstrap@5.1.3/dist/css/bootstrap.min.css // @require https://unpkg.com/jquery@3.6.0/dist/jquery.js // @require https://unpkg.com/bootstrap@5.1.3/dist/js/bootstrap.min.js // @downloadURL none // ==/UserScript== (function() { 'use strict'; /* * 浏览器打开设置,找到 隐私设置和安全性 >> 网站设置 >> Cookie和网站数据将 * “允许网站保存和读取 Cookie 数据(推荐)”选项开启、 * 将“阻止第三方 Cookie”选项关闭就可以了。(针对Chrome浏览器,其他浏览器Cookie设置的位置可能有所不同) */ $(function(){ createBtn(); // 存储 $('.page-header .save2strage').on('click', function(){ saveAllAnswer(); }); // 一键设置答案 $('.page-header .get2strage').on('click', function(){ setAnswer(); }); // 提交答案 $('.page-header .wantCommit').on('click', function(){ document.querySelector('#btn_submit_edit').click(); }); // 保存答案 $('.page-header .wantSave').on('click', function(){ document.querySelector('#btn_save_edit').click(); }); // 查看答案 $('.page-header .checkAnswer').on('click',function(){ createSeeDialog(); }); $('.page-header .clearAnswer').on('click',function(){ clearStorage() }); function saveAllAnswer(callback) { var arr = []; $.each($('.question-entity'),function() { // 题号 var num = $(this).find('.test-heading h4').text().slice(0,2).replace(/[^0-9]+/,''); var title = $(this).find('.test-heading h4').text(); //答案 var val = $(this).find('.test_answer .c76ace2:contains("标准答案:")').siblings('.fe855a').text(); arr.push({ num: num, title: title, value: val }); }); var key = location.href.match(/courseId=[0-9]+/); if(!key) return alert('ops, 出错了!'); //清除以前的,设置新的 GM_deleteValue(key[0]); GM_setValue(key[0], JSON.stringify(arr)); alert('okk, 设置成功!,即将返回'); $('#btn_back').click(); } function setAnswer(){ var key = location.href.match(/courseId=[0-9]+/); var storagekeys = GM_listValues(); if(!key || storagekeys.indexOf(key[0])<0) { return alert('未获得答案,请重新保存再试!'); }; var ansArr = JSON.parse(GM_getValue(key[0])); $.each($('.question-entity'),function(k) { // 题号 var num = $(this).find('.test-heading h4').text().slice(0,2).replace(/[^0-9]+/,''); var that = $(this); $.each(ansArr, function(key, aitem){ if(num === aitem.num) { var answer = aitem.value.split(''); $.each(answer, function(key, val) { that.find(`.test_answer_radio label input[value=${val}]`).attr('checked', true); }); }; }); if(k === ($('.question-entity').length-1)){ alert('设置成功'); } }); } function createBtn() { var str = ''; // 保持唯一 $('.save2strage').remove(); $('.get2strage').remove(); $('.checkAnswer').remove(); var href = location.href; var status = location.pathname.split('/'); status = status[status.length-1]; // 判断是查看答案页,还是作业答题页 // ViewExerciseAnswer 查看答案 // ExercisePaper 作业答题 if(status === 'ViewExerciseAnswer') { str = $(''); }else if(status === 'ExercisePaper'){ str = $(` `); }; $('.page-header').append(str); } function createSeeDialog() { var key = location.href.match(/courseId=[0-9]+/); if(!GM_getValue(key[0])){return alert('未找到保存的答案,确定保存了?')}; var ansArr = JSON.parse(GM_getValue(key[0])); $('.seeAnswer').remove(); var str = $(`
序号 题目 答案
`); var loopStr = ''; $.each(ansArr, function(k, item) { loopStr+=` ${item.num} ${item.title} ${item.value} `; }); str.find('.tbody').append($(loopStr)); $('.container').append(str); $('.closeAnswer').click(function() { $('.seeAnswer').remove(); }) } function clearStorage(){ var stg = GM_listValues(); if(Object.prototype.toString.call(stg).indexOf('Array')>-1){ $.each(stg,function(k,item){ GM_deleteValue(item); }); } } }); })();