🎉 欢迎访问GreasyFork.Org 镜像站!本镜像站由公众号【爱吃馍】搭建,用于分享脚本。联系邮箱📮

Greasy fork 爱吃馍镜像

Bilibili 关闭自动连播

自动检测Bilibili视频页面的“自动连播”功能,如果开启则自动关闭。

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

🚀 安装遇到问题?关注公众号获取帮助

公众号二维码

扫码关注【爱吃馍】

回复【脚本】获取最新教程和防失联地址

// ==UserScript==
// @name         Bilibili 关闭自动连播
// @namespace    http://tampermonkey.net/
// @version      1.6
// @description  自动检测Bilibili视频页面的“自动连播”功能,如果开启则自动关闭。
// @author       vnry
// @match        *://www.bilibili.com/video/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    console.log('Bilibili 自动连播关闭脚本已启动。');

    let isProcessingClick = false;
    const MAX_ATTEMPTS = 5;
    let currentAttempts = 0;

    function displayToast(message, duration = 1500) {
        let toastElement = document.getElementById('bilibili-autoplay-toast');

        if (toastElement) {
            clearTimeout(toastElement.hideTimer);
            toastElement.remove();
        }

        toastElement = document.createElement('div');
        toastElement.id = 'bilibili-autoplay-toast';
        toastElement.textContent = message;
        Object.assign(toastElement.style, {
            position: 'fixed',
            bottom: '20px',
            right: '20px',
            backgroundColor: 'rgba(0, 0, 0, 0.7)',
            color: '#f05b72',
            padding: '10px 15px',
            borderRadius: '5px',
            zIndex: '9999',
            opacity: '0',
            transition: 'opacity 0.5s ease-in-out',
            pointerEvents: 'none',
            fontSize: '30px',
            whiteSpace: 'nowrap'
        });

        document.body.appendChild(toastElement);

        void toastElement.offsetWidth;
        toastElement.style.opacity = '1';

        toastElement.hideTimer = setTimeout(() => {
            toastElement.style.opacity = '0';
            setTimeout(() => {
                if (toastElement.parentNode) {
                    toastElement.remove();
                }
            }, 500);
        }, duration);
    }

    function disableAutoPlay() {
        if (isProcessingClick || currentAttempts >= MAX_ATTEMPTS) {
            return false;
        }

        const recommendList = document.querySelector('.recommend-list-v1');

        if (recommendList) {
            const switchBtn = recommendList.querySelector('.continuous-btn .switch-btn');

            if (switchBtn) {
                if (switchBtn.classList.contains('on')) {
                    isProcessingClick = true;
                    currentAttempts++;

                    console.log(`检测到自动连播已开启,正在尝试关闭... (第 ${currentAttempts} 次尝试)`);

                    setTimeout(() => {
                        const eventOptions = { bubbles: true, cancelable: true };
                        switchBtn.dispatchEvent(new MouseEvent('mouseover', eventOptions));
                        switchBtn.dispatchEvent(new MouseEvent('mousemove', eventOptions));
                        switchBtn.dispatchEvent(new MouseEvent('mousedown', eventOptions));
                        switchBtn.dispatchEvent(new MouseEvent('mouseup', eventOptions));
                        switchBtn.click();

                        console.log('已发送点击事件给自动连播按钮。');

                        setTimeout(() => {
                            if (!switchBtn.classList.contains('on')) {
                                console.log('自动连播已成功关闭!');
                                displayToast('自动连播已关闭');
                                isProcessingClick = false;
                                currentAttempts = 0;
                                return true;
                            } else {
                                console.warn('自动连播点击后仍处于开启状态。');
                                isProcessingClick = false;
                            }
                        }, 200);
                    }, 100);

                    return false;
                } else {
                    console.log('自动连播已处于关闭状态。');
                    isProcessingClick = false;
                    currentAttempts = 0;
                    return true;
                }
            }
        }
        return false;
    }

    const observer = new MutationObserver((mutations, obs) => {
        if (disableAutoPlay()) {
            obs.disconnect();
            console.log('已停止 DOM 观察器,自动连播已处理。');
        }
    });

    observer.observe(document.body, { childList: true, subtree: true });

    if (disableAutoPlay()) {
        observer.disconnect();
        console.log('自动连播在初始加载时已处理,已停止观察器。');
    }

})();