Skip to content

Instantly share code, notes, and snippets.

@z0z0r4
Last active October 12, 2025 03:30
Show Gist options
  • Select an option

  • Save z0z0r4/523f47d6ea2a4858962728bb5479e46a to your computer and use it in GitHub Desktop.

Select an option

Save z0z0r4/523f47d6ea2a4858962728bb5479e46a to your computer and use it in GitHub Desktop.
砺儒云课堂 - 自动播放 油猴脚本
// ==UserScript==
// @name 砺儒云课堂 - 自动播放
// @namespace https://gist.github.com/z0z0r4/523f47d6ea2a4858962728bb5479e46a
// @version 1.0
// @description 砺儒云课堂 - 自动播放下一个视频
// @author z0z0r4
// @match https://moodle.scnu.edu.cn/mod/fsresource/view.php*
// @grant none
// @run-at document-idle
// ==/UserScript==
(function () {
'use strict';
let hasBoundEnded = false;
let retryCount = 0;
const MAX_RETRY = 3;
function bindEndedEvent() {
if (hasBoundEnded) return;
const player = window.player;
if (player && typeof player.on === 'function') {
player.on('ended', () => {
console.log('✅ 视频播放结束,准备跳转...');
const nextUrl = findNextVideoUrl();
if (nextUrl) {
console.log('⏭️ 跳转到下一个视频:', nextUrl);
window.location.href = nextUrl;
} else {
console.log('⏹️ 已是最后一个视频');
}
});
hasBoundEnded = true;
console.log('🔗 已绑定 ended 事件');
}
}
function tryPlayAndVerify() {
const player = window.player;
if (!player || typeof player.play !== 'function') {
console.warn('⚠️ 播放器未准备好');
return;
}
bindEndedEvent();
console.log(`▶️ 第 ${retryCount + 1} 次尝试:调用 player.play()`);
player.play();
setTimeout(() => {
if (player._status === 'playing') {
console.log('🎉 播放成功!当前状态:playing');
} else {
console.warn(`❌ 播放失败,当前状态:${player._status}`);
retryCount++;
if (retryCount < MAX_RETRY) {
console.log(`🔄 ${2000}ms 后重试...`);
setTimeout(tryPlayAndVerify, 2000);
} else {
console.error('💥 达到最大重试次数,放弃自动播放');
}
}
}, 1000);
}
function initAutoPlay() {
console.log('⏳ 等待 5 秒后尝试自动播放...');
setTimeout(() => {
const player = window.player;
if (player && typeof player.play === 'function') {
tryPlayAndVerify();
} else {
console.warn('❌ 播放器未检测到或不支持 play 方法');
}
}, 5000);
}
function findNextVideoUrl() {
const urlParams = new URLSearchParams(window.location.search);
const currentCmid = urlParams.get('id');
if (!currentCmid) return null;
const videoLinks = Array.from(
document.querySelectorAll('#courseindex a.courseindex-link[href*="/mod/fsresource/view.php?id="]')
).filter(link => {
const text = link.textContent.trim();
return text.endsWith('.mp4') || !text.includes('测验');
});
for (let i = 0; i < videoLinks.length; i++) {
const linkUrl = new URL(videoLinks[i].href);
if (linkUrl.searchParams.get('id') === currentCmid) {
return i + 1 < videoLinks.length ? videoLinks[i + 1].href : null;
}
}
return null;
}
initAutoPlay();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment