UOOC 自动看视频

自动播放视频和处理视频里的测验

  1. // ==UserScript==
  2. // @name UOOC 自动看视频
  3. // @namespace Violentmonkey Scripts
  4. // @match https://www.uooc.net.cn/home/learn/index*
  5. // @grant none
  6. // @version 1.0
  7. // @author relic-yuexi
  8. // @description 自动播放视频和处理视频里的测验
  9. // @license Apache License 2.0
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // 配置对象
  16. var config = {
  17. autoPlay: true, // 自动播放开关
  18. autoSwitch: true, // 自动切换开关
  19. autoQuiz: true // 自动处理测验开关
  20. };
  21.  
  22. // 添加启动按钮到页面
  23. function addStartButton() {
  24. const btn = document.createElement('button');
  25. btn.innerHTML = '启动自动播放';
  26. btn.style.position = 'fixed';
  27. btn.style.top = '10px';
  28. btn.style.left = '10px';
  29. btn.style.zIndex = '9999';
  30. btn.onclick = initAutoPlaySystem;
  31. document.body.appendChild(btn);
  32. }
  33.  
  34. // 获取当前视频元素
  35. function getCurrentVideo() {
  36. return document.getElementsByTagName('video')[0];
  37. }
  38.  
  39. // 处理视频播放
  40. function handleVideoPlay() {
  41. var currentvideo = getCurrentVideo();
  42. if(currentvideo && config.autoPlay && currentvideo.paused &&
  43. currentvideo.duration != currentvideo.currentTime &&
  44. (currentvideo.currentTime != 0 || config.autoPlay)) {
  45.  
  46. // 尝试播放,并处理可能的错误
  47. currentvideo.play().catch(function(error) {
  48. console.log('播放失败:', error);
  49. // 如果是因为用户未交互导致的错误,不用特别处理
  50. // 用户点击启动按钮后就可以正常播放了
  51. });
  52. }
  53. }
  54.  
  55. // 自动处理测验
  56. function handleQuiz() {
  57. var quizLayer = document.querySelector('.layui-layer.layui-layer-page');
  58. if(quizLayer) {
  59. console.log('检测到测验弹窗,自动处理中...');
  60.  
  61. // 检查是否已经显示了正确答案
  62. var wrongTip = quizLayer.querySelector('.fl_left[style*="color:red"]');
  63. if(wrongTip) {
  64. // 如果显示了正确答案,解析并选择正确选项
  65. console.log('发现正确答案提示');
  66. var answerText = wrongTip.textContent;
  67. try {
  68. // 从文本中提取正确答案数组,例如 '正确答案: ["C","D"]'
  69. var answers = JSON.parse(answerText.split(': ')[1]);
  70. console.log('正确答案:', answers);
  71.  
  72. // 清除所有已选项
  73. var allOptions = quizLayer.querySelectorAll('input[type="checkbox"], input[type="radio"]');
  74. allOptions.forEach(option => {
  75. option.checked = false;
  76. });
  77.  
  78. // 选择正确答案
  79. answers.forEach(answer => {
  80. var option = quizLayer.querySelector(`input[value="${answer}"]`);
  81. if(option) {
  82. option.click();
  83. console.log('选择答案:', answer);
  84. }
  85. });
  86. } catch(e) {
  87. console.log('解析答案出错:', e);
  88. }
  89. } else {
  90. // 如果还没显示正确答案,随机选择一个选项
  91. var options = quizLayer.querySelectorAll('input[type="checkbox"], input[type="radio"]');
  92. if(options.length > 0) {
  93. // 判断是单选还是多选
  94. var isMultiple = options[0].type === 'checkbox';
  95. console.log('题目类型:', isMultiple ? '多选' : '单选');
  96.  
  97. if(isMultiple) {
  98. // 如果是多选,随机选择1-2个选项
  99. var numToSelect = Math.floor(Math.random() * 2) + 1;
  100. var indexes = new Set();
  101. while(indexes.size < numToSelect) {
  102. indexes.add(Math.floor(Math.random() * options.length));
  103. }
  104. indexes.forEach(index => {
  105. options[index].click();
  106. console.log('选择选项:', options[index].value);
  107. });
  108. } else {
  109. // 如果是单选,随机选择一个选项
  110. var randomIndex = Math.floor(Math.random() * options.length);
  111. options[randomIndex].click();
  112. console.log('选择选项:', options[randomIndex].value);
  113. }
  114. }
  115. }
  116.  
  117. // 点击确定按钮
  118. setTimeout(function() {
  119. var confirmBtn = quizLayer.querySelector('.btn.btn-success');
  120. if(confirmBtn) {
  121. console.log('点击确定按钮');
  122. confirmBtn.click();
  123. }
  124. }, 500);
  125. }
  126. }
  127.  
  128.  
  129. // 处理视频播放完成后的切换逻辑
  130. function handleVideoEnd() {
  131. // 首先检查当前小节是否还有未播放的视频
  132. var currentSection = document.querySelector('.resourcelist');
  133. var videos = currentSection.querySelectorAll('.icon-video');
  134. var activeVideoParent = document.querySelector('.basic.ng-scope.active');
  135.  
  136. // 获取当前视频的索引
  137. var currentIndex = -1;
  138. videos.forEach((v, i) => {
  139. if(v.closest('.basic').classList.contains('active')) {
  140. currentIndex = i;
  141. }
  142. });
  143.  
  144. // 如果当前小节还有未播放的视频
  145. if(currentIndex < videos.length - 1) {
  146. console.log('当前小节还有未播放的视频,切换到下一个视频');
  147. videos[currentIndex + 1].closest('.basic').click();
  148.  
  149. // 等待新视频加载
  150. setTimeout(function() {
  151. setupVideoEvents(); // 重新设置视频事件
  152. }, 1000);
  153. return;
  154. }
  155.  
  156. // 如果是小节的最后一个视频,准备切换到下一小节
  157. console.log('当前小节视频已全部播放完,准备切换到下一小节');
  158. var currentSectionLi = document.querySelector('.oneline.ng-binding.active').closest('li');
  159. var nextLi = currentSectionLi.nextElementSibling;
  160.  
  161. if(nextLi) {
  162. // 切换到下一小节
  163. var nextSectionDiv = nextLi.querySelector('.basic');
  164. if(nextSectionDiv) {
  165. console.log('正在切换到下一节:', nextLi.querySelector('.oneline').textContent.trim());
  166. nextSectionDiv.click();
  167.  
  168. // 等待新页面加载完成后点击第一个视频
  169. setTimeout(function() {
  170. var newVideoBtn = document.querySelector('.icon-video');
  171. if(newVideoBtn) {
  172. console.log('正在点击新小节的第一个视频');
  173. newVideoBtn.closest('.basic').click();
  174.  
  175. // 等待新视频加载
  176. setTimeout(function() {
  177. setupVideoEvents(); // 重新设置视频事件
  178. }, 1000);
  179. }
  180. }, 2000);
  181. }
  182. } else {
  183. // 如果是章节的最后一个小节,切换到下一章
  184. var activeSection = document.querySelector('.oneline.ng-binding.active');
  185. if(activeSection) {
  186. var currentChapterItem = activeSection.closest('.catalogItem');
  187. var nextChapterItem = currentChapterItem.nextElementSibling;
  188.  
  189. if(nextChapterItem) {
  190. console.log('正在切换到下一章节:', nextChapterItem.querySelector('.oneline').textContent.trim());
  191. var nextChapterDiv = nextChapterItem.querySelector('.chapter');
  192. if(nextChapterDiv) {
  193. nextChapterDiv.click();
  194.  
  195. // 等待新章节加载完成后点击第一个小节
  196. setTimeout(function() {
  197. var firstSection = document.querySelector('.rank-2 li .basic');
  198. if(firstSection) {
  199. firstSection.click();
  200. // 等待小节加载完成后点击第一个视频
  201. setTimeout(function() {
  202. var firstVideo = document.querySelector('.icon-video');
  203. if(firstVideo) {
  204. console.log('正在点击新章节第一个视频');
  205. firstVideo.closest('.basic').click();
  206.  
  207. // 等待新视频加载
  208. setTimeout(function() {
  209. setupVideoEvents(); // 重新设置视频事件
  210. }, 1000);
  211. }
  212. }, 2000);
  213. }
  214. }, 2000);
  215. }
  216. } else {
  217. console.log('恭喜!课程已全部播放完成!');
  218. }
  219. }
  220. }
  221. }
  222.  
  223.  
  224. // 设置视频事件监听
  225. function setupVideoEvents() {
  226. var currentvideo = getCurrentVideo();
  227. if(currentvideo) {
  228. console.log('设置新视频事件监听');
  229. // 移除可能存在的旧事件监听
  230. currentvideo.removeEventListener('ended', handleVideoEnd);
  231. // 添加新的事件监听
  232. currentvideo.addEventListener('ended', function() {
  233. if(config.autoSwitch) {
  234. console.log('视频播放完成,准备切换下一个视频');
  235. handleVideoEnd();
  236. }
  237. });
  238. }
  239. }
  240.  
  241. // 初始化系统
  242. function initAutoPlaySystem() {
  243. console.log('自动播放系统已启动');
  244. console.log('autoPlay:', config.autoPlay);
  245. console.log('autoSwitch:', config.autoSwitch);
  246. console.log('autoQuiz:', config.autoQuiz);
  247.  
  248. // 设置定时检查
  249. setInterval(function() {
  250. if(config.autoQuiz) {
  251. handleQuiz();
  252. }
  253. handleVideoPlay();
  254. }, 2000);
  255.  
  256. // 初始设置视频事件
  257. setupVideoEvents();
  258. }
  259.  
  260. // 等待页面加载完成
  261. window.addEventListener('load', function() {
  262. // 添加启动按钮
  263. addStartButton();
  264. });
  265.  
  266. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址