// ==UserScript== // @name 优课在线辅助脚本 // @namespace http://www.qs5.org/?UoocAutoLearn // @version 1.2.181225a // @description 实现自动挂机看视频,作业自动做题/共享答案功能 // @author ImDong // @match *://*.uooconline.com/* // @match *://www1.baidu.com/s?uooc=1&* // @grant none // @downloadURL https://update.greasyfork.icu/scripts/40463/%E4%BC%98%E8%AF%BE%E5%9C%A8%E7%BA%BF%E8%BE%85%E5%8A%A9%E8%84%9A%E6%9C%AC.user.js // @updateURL https://update.greasyfork.icu/scripts/40463/%E4%BC%98%E8%AF%BE%E5%9C%A8%E7%BA%BF%E8%BE%85%E5%8A%A9%E8%84%9A%E6%9C%AC.meta.js // ==/UserScript== (function (window, $) { // 创建对象 var UoocAutoLearn = window.UoocAutoLearn || { apiUrl: 'https://www.qs5.org/tools/szu_tools/index.php' }; // 获取课程列表 UoocAutoLearn.getCatalogList = function () { $.ajax({ type: "GET", url: '/home/learn/getCatalogList', data: { cid: this.cid }, success: function (response) { UoocAutoLearn.loopCatalog(response.data); } }); }; // 遍历课程 UoocAutoLearn.loopCatalog = function (data) { var isFinished = true; for (let index = 0; index < data.length; index++) { const item = data[index]; if (item.finished == 0) { isFinished = false; if (typeof item.children != 'undefined') { UoocAutoLearn.loopCatalog(item.children); } else { // 播放这个课程 console.log('新的课程', item.number, item.name); UoocAutoLearn.catalog_id = item.id; UoocAutoLearn.chapter_id = item.pid; UoocAutoLearn.video_pos = 0; // 开始下一课程 UoocAutoLearn.getUnitLearn(); } break; } } if (isFinished) { console.log('恭喜,本课已全看完。'); } }; // 获取课程进度 UoocAutoLearn.getCourseLearn = function () { $.ajax({ type: "GET", url: '/home/learn/getCourseLearn', data: { cid: this.cid }, success: function (response) { if (response.code != 1) { console.log('Error', response); return; } UoocAutoLearn.chapter_id = response.data.chapter_id; UoocAutoLearn.section_id = response.data.section_id; UoocAutoLearn.resource_id = response.data.resource_id; UoocAutoLearn.catalog_id = response.data.catalog_id; UoocAutoLearn.subsection_id = response.data.subsection_id; UoocAutoLearn.parent_name = response.data.parent_name; // 如果没有看过 if (UoocAutoLearn.chapter_id <= 0) { UoocAutoLearn.getCatalogList(); return; } console.log( '课程信息', UoocAutoLearn.parent_name, '章节', UoocAutoLearn.chapter_id, '部分', UoocAutoLearn.section_id, '资源', UoocAutoLearn.resource_id ); // 获取课程观看时间 UoocAutoLearn.getUnitLearn(); } }); }; // 获取当前课程观看时间 UoocAutoLearn.getUnitLearn = function () { $.ajax({ type: "GET", url: '/home/learn/getUnitLearn', data: { cid: this.cid, chapter_id: this.chapter_id, section_id: this.section_id, catalog_id: this.catalog_id }, success: function (response) { // 遍历每一个视频 var isFinished = true; for (let index = 0; index < response.data.length; index++) { const item = response.data[index]; if (item.finished == 0) { UoocAutoLearn.video_pos = parseFloat(item.video_pos); UoocAutoLearn.videoSource = item.video_play_list[0].source; UoocAutoLearn.title = item.title; UoocAutoLearn.resource_id = item.id; isFinished = false; console.log('当前任务', UoocAutoLearn.parent_name, UoocAutoLearn.title); // 获取视频时长 UoocAutoLearn.getVideoLength(); break; } } // 如果都看完了 if (isFinished) { // 获取下一节课 UoocAutoLearn.getCatalogList(); } } }); }; // 获取视频长度 UoocAutoLearn.getVideoLength = function () { // var video = document.createElement('video'); // 加载完成后调用 // video.onloadeddata = function () { UoocAutoLearn.video_length = 0; // console.log('总时长', UoocAutoLearn.video_length, '秒, 已看至', UoocAutoLearn.video_pos, '秒'); // 开始刷新时间 UoocAutoLearn.markVideoLearn(); // }; // video.src = UoocAutoLearn.videoSource; return; }; // 刷新时间 UoocAutoLearn.markVideoLearn = function () { this.video_pos = this.video_pos + 10; if (this.video_pos > this.video_length && this.video_length > 0) this.video_pos = this.video_length; $.ajax({ type: "POST", url: '/home/learn/markVideoLearn', data: { chapter_id: this.chapter_id, cid: this.cid, // hidemsg_: true, network: 3, resource_id: this.resource_id, section_id: this.section_id, source: 1, subsection_id: this.subsection_id, video_length: this.video_length == 0 ? 100 : this.video_length, video_pos: this.video_pos }, success: function (response) { console.log('已看至', UoocAutoLearn.video_pos, '秒, 总', UoocAutoLearn.video_length == 0 ? '未知' : UoocAutoLearn.video_length, '秒'); if (response.data.finished == 1 || (UoocAutoLearn.video_length > 0 && UoocAutoLearn.video_pos >= UoocAutoLearn.video_length)) { console.log('本课已经结束'); // 获取下一节课 UoocAutoLearn.getCatalogList(); return; } setTimeout(() => { UoocAutoLearn.markVideoLearn(); }, 10 * 1000); } }); }; // 获取课程列表 UoocAutoLearn.homeworkList = function () { console.log('homeworkList'); $.ajax({ type: "GET", url: '/home/task/homeworkList', data: { cid: this.cid, page: 1, pagesize: 20 }, success: function (response) { for (let index = 0; index < response.data.data.length; index++) { const element = response.data.data[index]; // 判断是否批改 if (element.status_code == "20") { // 提交答案到服务器 UoocAutoLearn.examView(UoocAutoLearn.cid, element.id) } } } }); } // 获取作业答案并提交 UoocAutoLearn.examView = function (cid, tid) { console.log('examView', cid, tid); $.ajax({ type: "GET", url: '/exam/view', data: { cid: cid, tid: tid }, success: function (response) { // 判断是否提交试卷 if (response.code == 1) { // 提交试卷到服务器 UoocAutoLearn.sendExam2Server(cid, tid, response.data); } } }); } // 提交试卷到服务器 UoocAutoLearn.sendExam2Server = function (cid, tid, data) { console.log('sendExam2Server', cid, tid); $.ajax({ type: "POST", url: UoocAutoLearn.apiUrl, data: { cmd: 'save_exam_answer', cid: cid, tid: tid, data: JSON.stringify(data) }, success: function (response) { console.log('sendExam2Server', cid, tid, response); } }); } // 从服务器获取答案 UoocAutoLearn.getExamAnswer = function () { console.log('getExamAnswer', this.tid); $.ajax({ type: "GET", url: UoocAutoLearn.apiUrl, dataType: "JSONP", data: { cmd: 'get_exam_answer', tid: this.tid }, success: function (response) { console.log(response); if (response.code == 1) { window._response = response; UoocAutoLearn.answerData = response.data; UoocAutoLearn.loopSetAnchor(); } } }); } // 依次遍历题目修改答案 UoocAutoLearn.loopSetAnchor = function () { console.log("loopSetAnchor"); for (let i = 0; i < UoocAutoLearn.answerData.length; i++) { const item = UoocAutoLearn.answerData[i]; // 获取题目对象 var anchor = $('#anchor' + item.id).parent('.queContainer'); window._item = item; window._anchor = anchor; // 获取题目内容 var anchor_ti = anchor.find('.ti-q-c').text(), answer_ti = $("
").html(item.question).text(); // 题目相同再遍历答案 if (anchor_ti == answer_ti) { // 设置题目绿色背景 // anchor.find('.ti-q-c').css({ backgroundColor: '#99FF99' }); // 获取答案 var ti_alist = anchor.find('.ti-alist label'); for (let k = 0; k < ti_alist.length; k++) { const a_item = ti_alist[k]; // 获取作业答案并提交 var ti_k = $(a_item).find('input').val(), ti_v = $(a_item).find('.ti-a-c').text(), an_v = $('
').html(item.options[ti_k]).text(); // 对比答案是否一致 一致则勾选 if (ti_v == an_v) { // 设置题目绿色 // $(a_item).find('.ti-a-c').css({ backgroundColor: '#99FF99' }) // 题目是否是正确答案 if (item.answer.indexOf(ti_k) >= 0) { $(a_item).find('input').click(); } } else { // 答案不一致 标红 $(a_item).find('.ti-a-c').css({ backgroundColor: 'burlywood' }); // 显示数据库原题 $(a_item).find('.ti-a-c').append(an_v); } } } // 题目不一致 设置红色 else { // var ti_a_list = $('