Disable YouTube Ambient Mode

Disables YouTube Ambient Mode on desktop

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
});