快手直播禁用鼠标滚轮切换直播间

禁用快手直播页面的鼠标滚轮上下滚动切换直播间功能

// ==UserScript==
// @name         快手直播禁用鼠标滚轮切换直播间
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  禁用快手直播页面的鼠标滚轮上下滚动切换直播间功能
// @author       You
// @match        https://live.kuaishou.com/*
// @match        https://*.kuaishou.com/live/*
// @grant        none
// @license      MIT
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    // 防止滚轮事件的函数
    function preventWheelSwitch(event) {
        // 检查是否在直播播放区域
        const target = event.target;
        const videoContainer = target.closest('.player-container, .live-player, .video-container, [class*="player"], [class*="video"]');

        if (videoContainer) {
            console.log('快手直播滚轮切换已被禁用');
            event.preventDefault();
            event.stopPropagation();
            event.stopImmediatePropagation();
            return false;
        }
    }

    // 等待页面加载完成后执行
    function init() {
        // 移除可能存在的滚轮事件监听器
        document.addEventListener('wheel', preventWheelSwitch, {
            passive: false,
            capture: true
        });

        // 也处理 mousewheel 事件(兼容性)
        document.addEventListener('mousewheel', preventWheelSwitch, {
            passive: false,
            capture: true
        });

        // 处理 DOMMouseScroll 事件(Firefox兼容性)
        document.addEventListener('DOMMouseScroll', preventWheelSwitch, {
            passive: false,
            capture: true
        });

        console.log('快手直播滚轮切换禁用脚本已启动');
    }

    // 如果页面已经加载完成
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }

    // 监听页面变化(SPA应用)
    let lastUrl = location.href;
    new MutationObserver(() => {
        const url = location.href;
        if (url !== lastUrl) {
            lastUrl = url;
            setTimeout(init, 1000); // 延迟重新初始化
        }
    }).observe(document, { subtree: true, childList: true });

})();