// ==UserScript==
// @name 浙江中医药大学实验室安全教育平台 自动刷课(带UI)
// @namespace http://tampermonkey.net/
// @version 2.1
// @description 自动刷视频,带悬浮窗日志和倍速调节
// @author YourName
// @match https://sysaq.zcmu.edu.cn/lab-study-front/security/safetyVideoList/7
// @match https://sysaq.zcmu.edu.cn/lab-study-front/security/safetyVideo/7/*
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_notification
// @run-at document-end
// ==/UserScript==
(function() {
'use strict';
// ========== 配置 ==========
const TOTAL_VIDEOS = 27;
const STORAGE_KEY = 'zcmu_completed_v2';
let playbackRate = 1.0; // 默认1倍速
// ========== 工具函数 ==========
function getCompletedSet() {
const data = GM_getValue(STORAGE_KEY, '[]');
return new Set(JSON.parse(data));
}
function saveCompletedSet(set) {
GM_setValue(STORAGE_KEY, JSON.stringify([...set]));
}
function isAllComplete() {
const set = getCompletedSet();
for (let i = 1; i <= TOTAL_VIDEOS; i++) {
if (!set.has(i)) return false;
}
return true;
}
function getNextUncompleted() {
const set = getCompletedSet();
for (let i = 1; i <= TOTAL_VIDEOS; i++) {
if (!set.has(i)) return i;
}
return null;
}
function markComplete(id) {
const set = getCompletedSet();
set.add(id);
saveCompletedSet(set);
}
// ========== 全局日志和状态函数(先占位) ==========
let logFn = (msg) => {};
let statusFn = (msg) => {};
// ========== UI 悬浮窗 ==========
function createUI() {
// 容器
const container = document.createElement('div');
container.id = 'zcmu-ui';
container.style.cssText = `
position: fixed;
top: 100px;
right: 20px;
width: 340px;
max-height: 450px;
background: #ffffff;
border-radius: 8px;
box-shadow: 0 0 20px rgba(0,0,0,0.3);
z-index: 99999;
font-family: Menlo, Monaco, Consolas, 'Courier New', monospace;
font-size: 13px;
color: #333;
display: flex;
flex-direction: column;
user-select: none;
border: 1px solid #ccc;
transition: all 0.2s;
`;
// 标题栏(可拖动)
const header = document.createElement('div');
header.style.cssText = `
padding: 8px 12px;
background: #f5f5f5;
border-radius: 8px 8px 0 0;
cursor: move;
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 1px solid #ddd;
`;
header.innerHTML = `
🎓 ZCMU 刷课助手
−
`;
container.appendChild(header);
// 可折叠内容区
const content = document.createElement('div');
content.id = 'zcmu-content';
content.style.cssText = `
padding: 8px 12px;
overflow: auto;
flex: 1;
display: block;
`;
// 倍速控件
const speedDiv = document.createElement('div');
speedDiv.style.cssText = 'margin-bottom: 8px; display: flex; align-items: center; gap: 8px;';
speedDiv.innerHTML = `
`;
content.appendChild(speedDiv);
// 日志区域
const logDiv = document.createElement('div');
logDiv.id = 'zcmu-log';
logDiv.style.cssText = `
background: #1e1e1e;
color: #d4d4d4;
padding: 6px 8px;
border-radius: 4px;
height: 180px;
overflow-y: auto;
font-size: 12px;
line-height: 1.6;
white-space: pre-wrap;
word-break: break-all;
margin-bottom: 4px;
`;
logDiv.textContent = '[系统] 脚本已启动\n';
content.appendChild(logDiv);
// 状态信息
const statusDiv = document.createElement('div');
statusDiv.id = 'zcmu-status';
statusDiv.style.cssText = 'font-size:12px; color:#666; text-align:center; padding:2px 0; border-top:1px solid #eee; margin-top:4px;';
statusDiv.textContent = '就绪';
content.appendChild(statusDiv);
container.appendChild(content);
// 添加到页面
document.body.appendChild(container);
// ---------- 事件绑定 ----------
// 拖动
let isDragging = false, offsetX, offsetY;
header.addEventListener('mousedown', (e) => {
if (e.target.tagName === 'SELECT') return;
isDragging = true;
const rect = container.getBoundingClientRect();
offsetX = e.clientX - rect.left;
offsetY = e.clientY - rect.top;
document.addEventListener('mousemove', onDrag);
document.addEventListener('mouseup', onDragEnd);
});
function onDrag(e) {
if (!isDragging) return;
let left = e.clientX - offsetX;
let top = e.clientY - offsetY;
left = Math.max(0, Math.min(window.innerWidth - container.offsetWidth, left));
top = Math.max(0, Math.min(window.innerHeight - container.offsetHeight, top));
container.style.left = left + 'px';
container.style.top = top + 'px';
container.style.right = 'auto';
}
function onDragEnd() {
isDragging = false;
document.removeEventListener('mousemove', onDrag);
document.removeEventListener('mouseup', onDragEnd);
}
// 折叠/展开
const toggleBtn = document.getElementById('zcmu-toggle');
toggleBtn.addEventListener('click', () => {
const content = document.getElementById('zcmu-content');
if (content.style.display === 'none') {
content.style.display = 'block';
toggleBtn.textContent = '−';
container.style.width = '340px';
} else {
content.style.display = 'none';
toggleBtn.textContent = '+';
container.style.width = 'auto';
}
});
// 倍速变更(现在使用全局 logFn)
document.getElementById('zcmu-speed').addEventListener('change', (e) => {
playbackRate = parseFloat(e.target.value);
logFn(`[设置] 倍速调整为 ${playbackRate}x`);
});
// 返回UI元素
return {
container,
getPlaybackRate: () => playbackRate,
updateSpeedDisplay: () => {
document.getElementById('zcmu-speed').value = playbackRate;
}
};
}
// ========== 初始化UI并绑定日志 ==========
function initUI() {
// 先定义日志和状态函数(覆盖全局占位)
logFn = (msg) => {
const logDiv = document.getElementById('zcmu-log');
if (logDiv) {
const time = new Date().toLocaleTimeString();
logDiv.textContent += `[${time}] ${msg}\n`;
logDiv.scrollTop = logDiv.scrollHeight;
}
};
statusFn = (msg) => {
const statusDiv = document.getElementById('zcmu-status');
if (statusDiv) statusDiv.textContent = msg;
};
// 再创建UI(此时 logFn 已可用)
const ui = createUI();
// 记录一条启动日志
logFn('页面加载完成');
statusFn('初始化...');
return ui;
}
// ========== 核心逻辑 ==========
(async function main() {
// 初始化UI
const ui = initUI();
// 为了方便,将 logFn 和 statusFn 赋给局部变量(也可直接使用全局)
const log = logFn;
const setStatus = statusFn;
const path = window.location.pathname;
if (path.includes('/safetyVideoList/7')) {
// ---------- 列表页 ----------
log('进入列表页');
if (isAllComplete()) {
log('🎉 所有视频已学习完毕!');
setStatus('全部完成');
GM_notification('🎉 所有视频已学习完毕!');
return;
}
const nextId = getNextUncompleted();
if (nextId === null) {
log('所有视频已完成!');
setStatus('完成');
return;
}
log(`即将进入第 ${nextId} 个视频...`);
setStatus(`跳转至视频 ${nextId}`);
setTimeout(() => {
window.location.href = `/lab-study-front/security/safetyVideo/7/${nextId}`;
}, 1500);
} else if (path.includes('/safetyVideo/7/')) {
// ---------- 详情页 ----------
const match = path.match(/\/safetyVideo\/7\/(\d+)/);
if (!match) {
log('无法解析视频ID');
return;
}
const videoId = parseInt(match[1]);
log(`进入视频 ${videoId}`);
// 等待视频加载
function waitForVideo(timeout = 30000) {
return new Promise((resolve, reject) => {
const start = Date.now();
const check = setInterval(() => {
const video = document.querySelector('video');
if (video && video.readyState >= 2) {
clearInterval(check);
resolve(video);
} else if (Date.now() - start > timeout) {
clearInterval(check);
reject(new Error('视频加载超时'));
}
}, 500);
});
}
// 播放视频
async function playVideo() {
try {
const video = await waitForVideo();
const rate = ui.getPlaybackRate();
video.playbackRate = rate;
video.muted = true;
await video.play();
log(`视频 ${videoId} 开始播放(${rate}x)`);
setStatus(`播放中 ${videoId}/${TOTAL_VIDEOS}`);
return new Promise((resolve) => {
let resolved = false;
const onEnd = () => {
if (!resolved) {
resolved = true;
resolve();
}
};
video.addEventListener('ended', onEnd);
// 备用检测
function checkProgress() {
if (video.duration && video.currentTime >= video.duration - 0.5) {
video.removeEventListener('timeupdate', checkProgress);
onEnd();
}
}
video.addEventListener('timeupdate', checkProgress);
// 如果duration为NaN,等待
if (isNaN(video.duration)) {
const interval = setInterval(() => {
if (!isNaN(video.duration)) {
clearInterval(interval);
if (video.currentTime >= video.duration - 0.5) onEnd();
}
}, 500);
}
});
} catch (err) {
log(`❌ 视频 ${videoId} 播放失败: ${err.message}`);
setStatus('播放失败');
GM_notification(`视频 ${videoId} 加载失败,请刷新重试`);
}
}
await playVideo();
markComplete(videoId);
log(`✅ 视频 ${videoId} 完成`);
setStatus(`视频 ${videoId} 完成`);
// 查找返回按钮
let backBtn = document.querySelector('button.ivu-btn-primary');
if (!backBtn) {
const candidates = document.querySelectorAll('button, a, span, div[role="button"]');
backBtn = Array.from(candidates).find(el => el.textContent.trim().includes('返回'));
}
if (backBtn) {
log('点击返回按钮');
setTimeout(() => {
backBtn.click();
}, 500);
} else {
log('未找到返回按钮,执行后退');
history.back();
}
} else {
log('未知页面,脚本不处理');
}
})();
})();