Disable YouTube Ambient Mode

Disables YouTube Ambient Mode on desktop

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Disable YouTube Ambient Mode
// @namespace   Violentmonkey Scripts
// @match       https://www.youtube.com/*
// @grant       none
// @version     1.0
// @author      thehus
// @description Disables YouTube Ambient Mode on desktop
// @license     GNU GPLv3
// ==/UserScript==

window.addEventListener("load", (event) => {
    const MAX_RETRIES = 10;
    const WAIT_MS = 500;

    const runScript = () => {
        // wait for an element to appear on the page
        const waitForElement = (selector) => {
            let timeout = MAX_RETRIES;

            return new Promise((resolve, reject) => {
                const interval = setInterval(() => {
                    const el = selector();
                    if (el) {
                        clearInterval(interval);
                        resolve(el);
                    }
                    if (timeout-- <= 0) {
                        clearInterval(interval);
                        reject("timeout");
                    }
                }, WAIT_MS);
            });
        };

        // find the settings cog and press it
        waitForElement(() => document.getElementById("settings-cog")).then((cog) => {
            cog.click();
            cog.click();

            const getAmbientMode = () => Array.from(document.getElementsByClassName("ytp-menuitem")).find(e => e.innerText.toLowerCase().includes("ambient mode"));
            // find the ambient mode button and press it if it is enabled
            waitForElement(getAmbientMode).then((el) => {
                if (el.ariaChecked === "true") el.click();
            }).catch((e) => {
                console.log("couldn't find ambient mode button");
            });

        }).catch((e) => {
            console.log("couldn't find settings cog");
        });
    };

    const checkAndRunScript = () => {
        if (window.location.href.includes("youtube.com/watch")) {
            runScript();
        }
    };

    window.addEventListener('yt-page-data-updated', function (e) {
        checkAndRunScript();
    });

    // Initial check
    checkAndRunScript();
});