芒果TV网页版自动关闭弹幕

自动关闭芒果TV视频弹幕,支持切换集数后自动关闭弹幕,用户可选择启用或禁用功能

目前为 2024-11-07 提交的版本,查看 最新版本

// ==UserScript==
// @name         芒果TV网页版自动关闭弹幕
// @namespace    http://tampermonkey.net/
// @version      1.7
// @description  自动关闭芒果TV视频弹幕,支持切换集数后自动关闭弹幕,用户可选择启用或禁用功能
// @author       mankaki
// @match        *://www.mgtv.com/*
// @grant        none
// @license      GPL-3.0
// ==/UserScript==

(function() {
    'use strict';

    // 获取本地存储中的设置,默认启用自动关闭弹幕
    let autoCloseDanmu = JSON.parse(localStorage.getItem('autoCloseDanmu')) ?? true;

    // 创建开关 UI
    function createToggleButton() {
        const button = document.createElement('div');
        button.style.position = 'fixed';
        button.style.bottom = '10px';  // 放在屏幕右下角
        button.style.right = '10px';
        button.style.zIndex = '9999';
        button.style.display = 'flex';
        button.style.alignItems = 'center';
        button.style.justifyContent = 'center';
        button.style.width = 'auto';  // 自适应宽度
        button.style.height = '40px';
        button.style.borderRadius = '40px 0 0 40px';
        button.style.backgroundColor = '#fff';  // 初始背景色
        button.style.boxShadow = '-1px 4px 8px rgba(0, 0, 0, .06)';
        button.style.color = 'rgb(51, 51, 51)';  // 修改为新的文字颜色
        button.style.cursor = 'pointer';
        button.style.right = '-6px';  // 位置微调
        button.style.whiteSpace = 'nowrap';  // 不换行
        button.style.padding = '0 10px';  // 添加内边距以增加点击区域
        button.style.transition = 'background-color 0.3s, color 0.3s'; // 添加过渡效果

        button.innerText = `自动关闭弹幕: ${autoCloseDanmu ? '是' : '否'}`;

        button.addEventListener('click', () => {
            autoCloseDanmu = !autoCloseDanmu;
            localStorage.setItem('autoCloseDanmu', JSON.stringify(autoCloseDanmu));
            button.innerText = `自动关闭弹幕: ${autoCloseDanmu ? '是' : '否'}`;
        });

        // 添加悬停效果
        button.addEventListener('mouseover', () => {
            button.style.backgroundColor = '#ff5f00'; // 悬停时背景色
            button.style.color = '#fff'; // 悬停时文字颜色
        });

        button.addEventListener('mouseout', () => {
            button.style.backgroundColor = '#fff'; // 悬停移除
            button.style.color = 'rgb(51, 51, 51)'; // 悬停移除,恢复为新文字颜色
        });

        document.body.appendChild(button);
    }

    createToggleButton();

    let lastUrl = window.location.href;

    // 用于关闭弹幕的函数
    function closeDanmu() {
        if (!autoCloseDanmu) return;  // 如果未开启自动关闭弹幕,直接返回

        // 查找 class 中包含 'danmuSwitcher' 的元素
        const danmuButton = document.querySelector("[class*='danmuSwitcher']");

        // 如果找到弹幕开关按钮,则点击关闭弹幕
        if (danmuButton) {
            danmuButton.click();
            console.log("弹幕已关闭");
        } else {
            console.log("未找到弹幕开关按钮");
        }
    }

    // 延时执行关闭弹幕,确保页面内容加载完成
    function delayedCloseDanmu() {
        setTimeout(closeDanmu, 1000);  // 等待1秒后尝试关闭弹幕,可根据页面加载速度调整时间
    }

    // 页面首次加载完成时关闭弹幕
    window.addEventListener('load', delayedCloseDanmu);

    // 监听视频元素的重载
    function monitorVideoReload() {
        const videoElement = document.querySelector('video');  // 获取视频元素
        if (videoElement) {
            // 监听 `loadedmetadata` 事件,视频重载时会触发
            videoElement.addEventListener('loadedmetadata', () => {
                console.log("检测到视频重载,尝试关闭弹幕");
                delayedCloseDanmu();
            });
        }
    }

    // 初次执行监控视频加载的函数
    monitorVideoReload();

    // 监听点击事件来检测 URL 变化,延迟检测
    document.addEventListener('click', () => {
        setTimeout(() => {
            const currentUrl = window.location.href;
            if (currentUrl !== lastUrl) {  // 如果检测到 URL 发生变化
                lastUrl = currentUrl;
                delayedCloseDanmu();  // 延时尝试关闭弹幕
                monitorVideoReload();  // 重新监听新视频的重载
            }
        }, 2000);  // 延迟2秒后再检测 URL 变化
    });

})();