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

Greasy fork 爱吃馍镜像

Bilibili 关闭自动连播

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

Du musst eine Erweiterung wie Tampermonkey, Greasemonkey oder Violentmonkey installieren, um dieses Skript zu installieren.

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

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

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

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

Sie müssten eine Skript Manager Erweiterung installieren damit sie dieses Skript installieren können

(Ich habe schon ein Skript Manager, Lass mich es installieren!)

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

公众号二维码

扫码关注【爱吃馍】

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

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

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

公众号二维码

扫码关注【爱吃馍】

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

// ==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('自动连播在初始加载时已处理,已停止观察器。');
    }

})();