// ==UserScript== // @name 无锡人才培训平台自动播放 // @namespace https://github.com // @version 1.0 // @description 视频播放完毕后自动播放下一个视频,避免重复点击 // @author AutoPlay Assistant // @match https://rs.hrss.wuxi.gov.cn:8031/* // @grant none // @run-at document-idle // @license MIT // ==/UserScript== (function() { 'use strict'; // 配置 const CONFIG = { checkInterval: 3000, // 检查间隔(毫秒) minPlayTime: 30, // 最短播放时间(秒) debug: true // 调试模式 }; // 状态管理 const state = { isProcessing: false, // 是否正在处理切换 lastVideoId: null, // 上次处理的视频ID currentVideoId: null, // 当前视频ID canClickNext: true // 是否可以点击下一个 }; // 日志输出 function log(...args) { if (CONFIG.debug) { console.log('[AutoPlay]', ...args); } } // 获取当前视频章节 function getCurrentChapter() { const activeSection = document.querySelector('.childSection.active'); if (!activeSection) return null; const title = activeSection.querySelector('.title')?.textContent?.trim(); const status = activeSection.querySelector('.isFinsh')?.textContent?.trim(); return { element: activeSection, title: title || '未知', status: status || '', id: title ? title.replace(/\s+/g, '_') : 'unknown' }; } // 获取下一个待学习章节 function getNextChapter() { const chapters = document.querySelectorAll('.childSection'); let foundCurrent = false; for (const chapter of chapters) { // 先找到当前活跃的章节 if (chapter.classList.contains('active')) { foundCurrent = true; continue; } // 在当前章节后面找第一个"待学习"的章节 if (foundCurrent) { const status = chapter.querySelector('.isFinsh')?.textContent?.trim(); if (status === '待学习') { const title = chapter.querySelector('.title')?.textContent?.trim(); return { element: chapter, title: title || '未知', id: title ? title.replace(/\s+/g, '_') : 'unknown' }; } } } return null; } // 检查视频状态 function checkVideoStatus() { // 检查视频元素 const video = document.querySelector('video'); if (!video) { log('未找到视频元素'); return 'no_video'; } // 检查当前章节 const currentChapter = getCurrentChapter(); if (!currentChapter) { log('未找到当前章节'); return 'no_chapter'; } // 如果正在处理切换,直接返回 if (state.isProcessing) { log('正在处理切换,跳过检查'); return 'processing'; } // 检查是否重复处理同一个视频 if (state.currentVideoId === currentChapter.id && state.lastVideoId === currentChapter.id) { log('已经处理过此视频,跳过'); state.canClickNext = false; return 'already_processed'; } // 检查视频是否播放完毕 if (video.ended) { log('视频已结束'); return 'ended'; } // 检查是否接近结束 if (video.duration > 0 && video.currentTime >= video.duration - 5) { log('视频即将结束'); return 'near_end'; } // 检查是否播放了足够时间 if (video.currentTime >= CONFIG.minPlayTime) { // 检查章节状态是否变为"已完成" if (currentChapter.status === '已完成') { log('章节状态为已完成'); return 'completed'; } } return 'playing'; } // 点击下一个视频 function clickNextVideo() { // 检查是否可以点击 if (!state.canClickNext) { log('不允许点击下一个'); return false; } // 设置处理状态 state.isProcessing = true; const nextChapter = getNextChapter(); if (!nextChapter) { log('没有下一个待学习的视频'); state.isProcessing = false; return false; } log('准备切换到:', nextChapter.title); try { // 记录当前视频ID const currentChapter = getCurrentChapter(); if (currentChapter) { state.lastVideoId = state.currentVideoId; state.currentVideoId = currentChapter.id; } // 点击下一个章节 nextChapter.element.click(); // 等待页面响应 setTimeout(() => { state.isProcessing = false; state.canClickNext = true; log('切换完成,等待视频加载'); }, 2000); return true; } catch (error) { log('切换失败:', error); state.isProcessing = false; state.canClickNext = true; return false; } } // 主监控函数 function startMonitoring() { log('开始监控视频状态'); // 初始状态检查 const currentChapter = getCurrentChapter(); if (currentChapter) { state.currentVideoId = currentChapter.id; log('当前视频:', currentChapter.title); } // 定期检查视频状态 const monitorInterval = setInterval(() => { try { const status = checkVideoStatus(); // 如果视频结束或完成,点击下一个 if (status === 'ended' || status === 'completed') { log('检测到视频完成,准备切换'); clickNextVideo(); } } catch (error) { log('监控出错:', error); } }, CONFIG.checkInterval); return monitorInterval; } // 添加简单控制面板 function addControlPanel() { const panel = document.createElement('div'); panel.style.cssText = ` position: fixed; top: 10px; right: 10px; background: rgba(0,0,0,0.8); color: white; padding: 10px; border-radius: 5px; z-index: 9999; font-size: 12px; font-family: Arial; min-width: 150px; `; panel.innerHTML = `
🎯 自动播放
状态: 监控中
进度: -
`; document.body.appendChild(panel); // 更新进度信息 function updateProgress() { const chapters = document.querySelectorAll('.childSection'); const completed = Array.from(chapters).filter(ch => { const status = ch.querySelector('.isFinsh')?.textContent?.trim(); return status === '已完成'; }).length; document.getElementById('autoProgress').textContent = `${completed}/${chapters.length}`; const current = getCurrentChapter(); if (current) { document.getElementById('autoStatus').textContent = current.status === '已完成' ? '已完成' : '学习中'; } } // 手动下一个按钮 document.getElementById('autoNextBtn').addEventListener('click', () => { document.getElementById('autoNextBtn').disabled = true; document.getElementById('autoNextBtn').textContent = '处理中...'; const success = clickNextVideo(); setTimeout(() => { document.getElementById('autoNextBtn').disabled = false; document.getElementById('autoNextBtn').textContent = success ? '已切换' : '下一个'; setTimeout(() => { document.getElementById('autoNextBtn').textContent = '下一个'; }, 2000); }, 3000); }); // 定期更新进度 setInterval(updateProgress, 2000); setTimeout(updateProgress, 1000); } // 初始化 function init() { log('脚本初始化'); // 等待页面加载 setTimeout(() => { // 添加控制面板 addControlPanel(); // 开始监控 const monitor = startMonitoring(); // 监听页面卸载 window.addEventListener('beforeunload', () => { log('停止监控'); clearInterval(monitor); }); log('脚本启动完成'); }, 2000); } // 启动脚本 if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();