diff --git a/qingshuxuetang_auto_play.user.js b/qingshuxuetang_auto_play.user.js index 2b376c7..b288664 100644 --- a/qingshuxuetang_auto_play.user.js +++ b/qingshuxuetang_auto_play.user.js @@ -15,6 +15,8 @@ const url = window.location.href; const MIN_STUDY_SECONDS = 5 * 60; + const COURSEWARE_DETECT_TIMEOUT_SECONDS = 60; + const TEXT_COURSEWARE_SELECTOR = "#easyXDM_default2872_provider"; console.log("插件加载完成,等待界面渲染"); const parseStudySeconds = (text) => { @@ -39,6 +41,17 @@ const studiedSeconds = parseStudySeconds(mark.innerText); return studiedSeconds < MIN_STUDY_SECONDS; }; + + const returnToStudyPage = () => { + const backLink = document.querySelector(".back-link"); + + if (backLink) { + backLink.click(); + return; + } + + history.back(); + }; const onPageLoad = () => { console.log("页面加载完成,开始执行脚本"); @@ -77,17 +90,38 @@ } console.log("所有视频均已播放完毕,结束脚本"); }, 3000); - } else if (url.includes("CourseShow")) { - console.log("进入学习阶段"); - // 学习阶段 - const videoInterval = setInterval(() => { - const video = document.querySelector("#vjs_video_3_html5_api"); - - if (!video) { - console.error("视频元素未找到,结束脚本"); - clearInterval(videoInterval); - return; - } + } else if (url.includes("CourseShow")) { + console.log("进入学习阶段"); + // 学习阶段 + const showPageStartedAt = Date.now(); + const videoInterval = setInterval(() => { + const video = document.querySelector("#vjs_video_3_html5_api"); + + if (!video) { + const textCourseware = document.querySelector(TEXT_COURSEWARE_SELECTOR); + + if (textCourseware) { + console.log("检测到文本课件,等待5分钟后返回准备页面"); + clearInterval(videoInterval); + setTimeout(() => { + console.log("文本课件等待完成,返回准备页面"); + returnToStudyPage(); + }, MIN_STUDY_SECONDS * 1000); + return; + } + + if ( + Date.now() - showPageStartedAt > + COURSEWARE_DETECT_TIMEOUT_SECONDS * 1000 + ) { + console.error("视频和文本课件元素均未找到,结束脚本"); + clearInterval(videoInterval); + return; + } + + console.log("课件元素尚未渲染完成,继续等待"); + return; + } if (video.paused && video.currentTime === 0) { video.play(); @@ -95,11 +129,11 @@ return; } - if (video.currentTime >= video.duration - 1) { - console.log("视频播放结束,返回准备页面"); - clearInterval(videoInterval); - document.querySelector(".back-link").click(); - } + if (video.currentTime >= video.duration - 1) { + console.log("视频播放结束,返回准备页面"); + clearInterval(videoInterval); + returnToStudyPage(); + } if (video.playbackRate === 1) { console.log("将视频倍速设置为2倍速");