2024暑期研修成果 注入代码 送给有缘人

将以下脚本复制,粘贴到油猴中 

// ==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 });
    });

})();

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值