// ==UserScript== // @name 华医网学习助手(合规版) // @namespace https://local.hyw-assist/ // @version 2.0.0 // @description 支持 CC / 百家云 / 保利威 三种播放页:自动关闭课程评价等弹窗;视频正常播放结束后自动进入下一课;辅助自动播放。不读取登录凭据,不上报或伪造学习进度,不自动签到、不自动答题,不绕过人脸识别。 // @match *://*.91huayi.com/* // @run-at document-start // @noframes // @grant none // @license MIT // ==/UserScript== (function () { 'use strict'; var CONFIG = { autoClosePopup: true, // 自动关闭“课程评价/满意度/作业提醒”等非必要弹窗 autoNext: true, // 视频正常播完后自动进入下一课 autoPlay: true, // 辅助自动播放(含被浏览器拦截时的静音兜底) reloadIfNoPlayer: true, // 播放器长时间未初始化时自动刷新一次(自愈) nextDelayMs: 3000, // 播完到跳转下一课的间隔 popupIntervalMs: 800, playerTimeoutMs: 20000 }; // 三种播放页路由(抓包确认:Video_Type_PC 0=CoursePlay(CC) 1=PolyvCoursePlay 2=BJYCoursePlay) var PLAY_PAGE_RE = /\/exercise\/ExerciseCourse\/(CoursePlay|PolyvCoursePlay|BJYCoursePlay)/i; var ACTION_BY_VIDEO_TYPE = { 0: 'CoursePlay', 1: 'PolyvCoursePlay', 2: 'BJYCoursePlay' }; var state = { courseList: [], listFetchTried: false, currentCourseId: '', lastEndedAt: 0, nextAttempts: 0, assistedVideos: new WeakSet(), reloadGuardUsed: false, mutedHintShown: false }; function log() { var args = Array.prototype.slice.call(arguments); console.log.apply(console, ['[华医网学习助手]'].concat(args)); } function isVisible(el) { if (!el) return false; var style = window.getComputedStyle(el); if (!style || style.display === 'none' || style.visibility === 'hidden' || style.opacity === '0') return false; var rect = el.getBoundingClientRect(); return rect.width > 0 && rect.height > 0; } function findVisible(selector, root) { var nodes = (root || document).querySelectorAll(selector); for (var i = 0; i < nodes.length; i++) { if (isVisible(nodes[i])) return nodes[i]; } return null; } function readHidden(id) { var el = document.getElementById(id); return el ? (el.value || '') : ''; } // ---------- 课程列表(来自页面自身的 OtherCourserList 请求,只读不改动) ---------- function normalizeCourseList(data) { if (Array.isArray(data)) return data; if (data && Array.isArray(data.data)) return data.data; if (data && Array.isArray(data.list)) return data.list; return []; } function acceptCourseList(list) { if (!list.length) return; state.courseList = list; log('已读取课程列表:', list.length, '门课'); updatePanelStatus(); } function installNetworkHook() { var origOpen = XMLHttpRequest.prototype.open; var origSend = XMLHttpRequest.prototype.send; XMLHttpRequest.prototype.open = function (method, url) { this.__hywUrl = String(url || ''); return origOpen.apply(this, arguments); }; XMLHttpRequest.prototype.send = function () { this.addEventListener('load', function () { try { var url = this.__hywUrl || ''; if (url.indexOf('/OtherCourserList') === -1) return; var list = normalizeCourseList(JSON.parse(this.responseText)); acceptCourseList(list); } catch (error) { /* 忽略非目标响应 */ } }); return origSend.apply(this, arguments); }; if (window.fetch) { var origFetch = window.fetch; window.fetch = function (input, init) { return origFetch.call(this, input, init).then(function (response) { try { var url = typeof input === 'string' ? input : (input && input.url) || ''; if (url.indexOf('/OtherCourserList') !== -1) { response.clone().text().then(function (text) { try { acceptCourseList(normalizeCourseList(JSON.parse(text))); } catch (error) { /* 忽略 */ } }); } } catch (error) { /* 忽略钩子异常 */ } return response; }); }; } } // 兜底:脚本注入晚于页面请求(如浏览器启动恢复标签页)时收不到列表,主动拉一次 function fetchCourseListIfMissing() { if (state.listFetchTried || state.courseList.length) return; var planId = readHidden('learningplan_id'); var cwId = state.currentCourseId; if (!planId || !cwId) return; state.listFetchTried = true; var url = '/exercise/ExerciseCourse/OtherCourserList?learnPlanId=' + encodeURIComponent(planId) + '&courseware_id=' + encodeURIComponent(cwId) + '&top=10'; fetch(url, { credentials: 'include', headers: { 'X-Requested-With': 'XMLHttpRequest' } }) .then(function (r) { return r.text(); }) .then(function (text) { acceptCourseList(normalizeCourseList(JSON.parse(text))); }) .catch(function () { /* 静默 */ }); } function getCurrentCourseItem() { var key = state.currentCourseId; if (!key) return null; for (var i = 0; i < state.courseList.length; i++) { if (state.courseList[i].LearningPlan_Courseware_Id === key) return state.courseList[i]; } return null; } function getNextCourseItem() { var current = getCurrentCourseItem(); if (!current || !state.courseList.length) return null; var pool = state.courseList.filter(function (item) { return item.LearningPlan_Id === current.LearningPlan_Id; }); var sorted = pool.slice().sort(function (a, b) { return (a.List_Order || 0) - (b.List_Order || 0); }); var index = -1; for (var i = 0; i < sorted.length; i++) { if (sorted[i].LearningPlan_Courseware_Id === current.LearningPlan_Courseware_Id) { index = i; break; } } if (index >= 0 && index + 1 < sorted.length) return sorted[index + 1]; return null; } function courseDisplayName(item) { return (item && (item.Courseware_Name || item.LearningPlan_Courseware_Id)) || ''; } function videoTypeOf(item) { if (!item) return 0; if (typeof item.Video_Type_PC === 'number') return item.Video_Type_PC; if (typeof item.Video_Type === 'number') return item.Video_Type; return 0; } // ---------- 弹窗(只处理已知安全的弹窗,不碰签到/问答/人脸识别) ---------- // 平台强制流程是否正在进行(此时禁止自动下一课/自动播放) function blockingGateActive() { // 人脸识别弹窗(开始/中途/结束人脸)——必须由用户扫码完成验证 if (findVisible('.popup_box')) return true; // 播放器内置学习签到弹窗(ccH5 / BPlayer 的 signConfig) var signDialog = findVisible('[class*="signin" i],[class*="sign-box" i],[class*="signBox" i]'); if (signDialog) return true; // 平台提示框(例如“您有其他课程正在播放”) var tip = document.getElementById('login'); if (tip && isVisible(tip)) return true; return false; } function closePopups() { if (!CONFIG.autoClosePopup) return; // 1. 课程评价弹窗:点右上角关闭图标 var review = findVisible('.dialog_box_pj'); if (review) { var closeIcon = review.querySelector('.diaheader_box_pj img'); if (closeIcon) { closeIcon.click(); log('已关闭课程评价弹窗'); return; } } // 2. 满意度打分浮层:点“返回”,绝不代提交 var scoreLayer = findVisible('.hidden_layer_pj'); if (scoreLayer) { var backBtn = scoreLayer.querySelector('.lay_let_pj'); if (backBtn) { backBtn.click(); log('已关闭满意度打分浮层'); return; } } // 3. 作业提醒遮罩:点关闭按钮 var homework = document.getElementById('homework_reminder_mask'); if (homework && isVisible(homework)) { var hwClose = homework.querySelector('.homework_reminder_close'); if (hwClose) { hwClose.click(); log('已关闭作业提醒'); return; } } // 4. 人脸识别弹窗:仅在平台提示“人脸验证通过”后代点一次“进入课程学习” var facePopup = findVisible('.popup_box'); if (facePopup) { var okTip = facePopup.querySelector('#sucessTip'); if (okTip && isVisible(okTip)) { var enterBtn = facePopup.querySelector('.button_box button'); if (enterBtn && !enterBtn.disabled) { enterBtn.click(); log('人脸验证已通过,进入课程学习'); } } // 未通过时绝不隐藏、绝不代点,交由用户扫码 return; } // 5. 孤儿遮罩清理:仅当没有任何弹窗/提示可见时 if (!blockingGateActive()) { var mask = findVisible('.mask'); if (mask) mask.style.display = 'none'; } } // ---------- 自动下一课 ---------- function clickNextFromDom() { var nodes = document.querySelectorAll('.listGroup [onclick*="VideoPlay"]'); if (!nodes.length || !state.currentCourseId) return false; var index = -1; for (var i = 0; i < nodes.length; i++) { if ((nodes[i].getAttribute('onclick') || '').indexOf(state.currentCourseId) !== -1) { index = i; break; } } if (index < 0 || index + 1 >= nodes.length) return false; nodes[index + 1].click(); return true; } function navigateToCourse(item) { var id = item.LearningPlan_Courseware_Id || item.Courseware_Id; // 按课程类型选择正确的播放页路由(旧版硬编码 CoursePlay,是百家云课打不开的原因) var action = ACTION_BY_VIDEO_TYPE[videoTypeOf(item)] || 'CoursePlay'; log('跳转下一课(' + action + '):', courseDisplayName(item)); location.href = '/exercise/ExerciseCourse/' + action + '?courseware_id=' + encodeURIComponent(id); } function goToNextCourseOnce() { if (!CONFIG.autoNext) return; if (blockingGateActive()) { // 人脸/签到/平台提示未处理完,等待下一轮重试 if (state.nextAttempts < 150) { state.nextAttempts++; setTimeout(goToNextCourseOnce, 2000); } else { log('等待平台流程超时,停止自动下一课'); } return; } var next = getNextCourseItem(); // 优先用页面侧边栏节点(站点自身 onclick,路由永远正确) if (next && clickNextFromDom()) { log('已点击下一课:', courseDisplayName(next)); return; } // 其次调用页面自身的换课函数 VideoPlay(id, courseType, state, videoType) if (next && typeof window.VideoPlay === 'function') { try { window.VideoPlay(next.LearningPlan_Courseware_Id || next.Courseware_Id, next.courseware_type || 1, next.Learning_state || 0, videoTypeOf(next)); log('已调用页面换课:', courseDisplayName(next)); return; } catch (error) { /* 继续兜底 */ } } if (next) { navigateToCourse(next); return; } // 列表数据缺失时,仅依赖 DOM 顺序 if (clickNextFromDom()) { log('已点击下一课(DOM 顺序)'); return; } log('未找到下一课(可能已是最后一课或列表未加载)'); } function handleVideoEnded(reason) { var now = Date.now(); if (now - state.lastEndedAt < 8000) return; state.lastEndedAt = now; log('当前视频已正常播放结束(' + reason + '),' + CONFIG.nextDelayMs + 'ms 后进入下一课'); updatePanelStatus('已播完,准备下一课'); state.nextAttempts = 0; setTimeout(goToNextCourseOnce, CONFIG.nextDelayMs); } // ---------- 播放器辅助(只辅助起播,不改倍速、不拖进度) ---------- function tryPlay(video) { if (!video || !video.paused || video.ended) return; var promise = video.play(); if (promise && promise.catch) { promise.catch(function () { // 浏览器拦截了带声音的自动播放:按 CC 播放器 realAutoPlay 的同类策略改静音起播 if (!video.muted) { video.muted = true; var p2 = video.play(); if (p2 && p2.catch) p2.catch(function () {}); if (!state.mutedHintShown) { state.mutedHintShown = true; updatePanelStatus('浏览器拦截了自动播放,已静音播放(点播放器喇叭恢复声音)'); log('自动播放被拦截,已改为静音起播'); } } }); } } function assistStartup(video) { if (!CONFIG.autoPlay) return; if (state.assistedVideos.has(video)) return; if (video.readyState < 2 || !(video.duration > 0)) return; if (blockingGateActive()) return; if (video.played.length > 0) { // 已经播过(例如用户手动暂停),不再代播 state.assistedVideos.add(video); return; } state.assistedVideos.add(video); if (video.paused && !video.ended) { log('辅助自动播放'); tryPlay(video); } } function watchVideoPlayer() { var currentVideo = null; function bindVideo(video) { video.addEventListener('ended', function () { handleVideoEnded('ended'); }); video.addEventListener('timeupdate', function () { if (video.duration && video.currentTime >= video.duration - 1.2 && video.readyState >= 3) { handleVideoEnded('临近结尾'); } }); } function scan() { var playerBox = document.getElementById('ccVideo-box') || document; var video = playerBox.querySelector('video'); if (video && video !== currentVideo) { currentVideo = video; bindVideo(video); log('已找到播放器 video 元素'); updatePanelStatus(); } if (currentVideo) assistStartup(currentVideo); } scan(); function startObserver() { new MutationObserver(scan).observe(document.body, { childList: true, subtree: true }); } if (document.body) startObserver(); else document.addEventListener('DOMContentLoaded', startObserver, { once: true }); // 自愈:播放器脚本(bokecc / 百家云)加载失败时页面一直没有 video,刷新一次 setTimeout(function () { if (!CONFIG.reloadIfNoPlayer) return; if (document.querySelector('#ccVideo-box video')) return; if (blockingGateActive()) return; var key = 'hyw_reload_' + state.currentCourseId; try { if (sessionStorage.getItem(key)) { updatePanelStatus('播放器未能初始化,已尝试刷新仍失败'); return; } sessionStorage.setItem(key, '1'); } catch (error) { /* 隐私模式等 */ } log('播放器未初始化,自动刷新一次以恢复'); location.reload(); }, CONFIG.playerTimeoutMs); } // ---------- 面板 ---------- var panelStatusEl = null; function updatePanelStatus(text) { if (!panelStatusEl) return; var msg = text; if (!msg) { var video = document.querySelector('#ccVideo-box video'); if (!video) msg = '等待播放器初始化…'; else if (video.ended) msg = '已播完'; else if (video.paused) msg = '已暂停'; else msg = '播放中'; } var next = getNextCourseItem(); panelStatusEl.textContent = '状态:' + msg + ' | 下一课:' + (next ? courseDisplayName(next) : '(无)'); } function loadToggles() { try { var raw = sessionStorage.getItem('hyw_assist_cfg'); if (raw) { var saved = JSON.parse(raw); CONFIG.autoClosePopup = saved.autoClosePopup !== false; CONFIG.autoNext = saved.autoNext !== false; CONFIG.autoPlay = saved.autoPlay !== false; } } catch (error) { /* 忽略 */ } } function saveToggles() { try { sessionStorage.setItem('hyw_assist_cfg', JSON.stringify({ autoClosePopup: CONFIG.autoClosePopup, autoNext: CONFIG.autoNext, autoPlay: CONFIG.autoPlay })); } catch (error) { /* 忽略 */ } } function createUI() { var box = document.createElement('div'); box.id = 'hyw-course-assist'; box.innerHTML = '
华医网学习助手
' + '' + '' + '' + '
初始化…
'; var style = document.createElement('style'); style.textContent = [ '#hyw-course-assist{position:fixed;top:80px;right:10px;z-index:2147483646;background:rgba(20,20,20,.88);', 'color:#fff;padding:10px 12px;border-radius:8px;font:13px/1.6 "Microsoft YaHei",sans-serif;', 'box-shadow:0 4px 16px rgba(0,0,0,.3);user-select:none;display:flex;flex-direction:column;gap:4px;max-width:260px;}', '#hyw-course-assist .hyw-assist-title{font-weight:700;margin-bottom:2px;color:#ffd54f;}', '#hyw-course-assist label{display:flex;align-items:center;gap:6px;cursor:pointer;white-space:nowrap;}', '#hyw-course-assist input{accent-color:#4caf50;}', '#hyw-course-assist .hyw-assist-status{margin-top:4px;font-size:12px;color:#cfd8dc;white-space:normal;line-height:1.5;}' ].join(''); document.head.appendChild(style); document.body.appendChild(box); var map = { 'hyw-close-popup': 'autoClosePopup', 'hyw-auto-next': 'autoNext', 'hyw-auto-play': 'autoPlay' }; Object.keys(map).forEach(function (id) { var checkbox = box.querySelector('#' + id); checkbox.checked = CONFIG[map[id]]; checkbox.addEventListener('change', function (event) { CONFIG[map[id]] = event.target.checked; saveToggles(); }); }); panelStatusEl = box.querySelector('#hyw-status'); setInterval(function () { updatePanelStatus(); }, 3000); } // ---------- 启动 ---------- function isCoursePlayPage() { return PLAY_PAGE_RE.test(location.pathname); } function detectEnvironmentHints() { // 平台在百家云播放页会检测倍速插件(video.vsc-initialized)并拒绝播放,仅提示不对抗 setInterval(function () { if (document.querySelector('video.vsc-initialized')) { updatePanelStatus('检测到浏览器倍速插件(平台可能拦截播放)'); return; } var tipContent = document.getElementById('msgContent'); if (tipContent && isVisible(tipContent) && tipContent.textContent.indexOf('其他课程正在播放') !== -1) { updatePanelStatus('检测到其他课程正在播放,请关闭其他课程标签页'); } }, 3000); } function start() { loadToggles(); installNetworkHook(); if (!isCoursePlayPage()) return; state.currentCourseId = readHidden('title_id') || (new URLSearchParams(location.search).get('courseware_id') || ''); log('播放页:', location.pathname, '课程:', state.currentCourseId); function init() { setInterval(closePopups, CONFIG.popupIntervalMs); watchVideoPlayer(); createUI(); detectEnvironmentHints(); setTimeout(fetchCourseListIfMissing, 6000); log('已启动(支持 CC / 保利威 / 百家云播放页)'); } if (document.body) init(); else document.addEventListener('DOMContentLoaded', init, { once: true }); } start(); })();