将以下脚本复制,粘贴到油猴中
// ==UserScript==
// @name Auto Trigger Video Ended
// @namespace http://tampermonkey.net/
// @version 1.0
// @description 监听视频播放并触发ended事件
// @author Bobo
// @match https://basic.smartedu.cn/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
function addEndedEventTrigger() {
let video = document.querySelector("video");
if (video) {
video.addEventListener('play', function() {
console.log('Video is playing, dispatching "ended" event.');
video.dispatchEvent(new Event("ended"));
}, { once: true }); // 确保只执行一次
}
}
// 初始调用,以防视频已经存在并在页面加载时播放
addEndedEventTrigger();
// 创建 MutationObserver 用于检测新的视频元素
const observer = new MutationObserver((mutations) => {
mutations.forEach((mutation) => {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach((node) => {
if (node.tagName === 'VIDEO') {
addEndedEventTrigger();
}
});
}
});
});
// 观察整个 document 的子元素变化
observer.observe(document.body, { childList: true, subtree: true });
// 处理现有的所有视频元素
document.querySelectorAll('video').forEach(video => {
video.addEventListener('play', function() {
console.log('Video is playing, dispatching "ended" event.');
video.dispatchEvent(new Event("ended"));
}, { once: true });
});
})();