Youtube automatic theater.

Turn Youtube player to theater mode automatically.

目前为 2024-02-15 提交的版本。查看 最新版本

// ==UserScript==
// @name         Youtube automatic theater.
// @namespace    http://tampermonkey.net/
// @version      2024-02-15
// @description  Turn Youtube player to theater mode automatically.
// @author       Santeri Hetekivi
// @match        https://www.youtube.com/watch?v=*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// ==/UserScript==

(function () {
    'use strict';
    function forElement(selector, callback) {
        // Init forElement.timeoutCount.
        if (forElement.timeoutCount === undefined) {
            forElement.timeoutCount = {}
        }
        // Init forElement.timeoutCount[selector].
        if (forElement.timeoutCount[selector] === undefined) {
            forElement.timeoutCount[selector] = 0
        }

        // Get element.
        const element = document.querySelector(selector)

        // If element not found.
        if (element === null) {
            console.debug("Element not found:", selector)
            // try again after timeout.
            setTimeout(
                function () {
                    forElement(selector, callback)
                },
                (
                    // Base timeout.
                    100
                    *
                    // Increase timeout after each try.
                    (forElement.timeoutCount[selector]++)
                )
            )
        }
        // If element found
        else {
            // reset timeout count
            console.debug("Element found:", selector, element)
            forElement.timeoutCount[selector] = 0
            // and call callback with element.
            callback(element)
        }
    }

    // Run for
    forElement(
        // Youtube page manager
        ".ytd-page-manager",
        function (manager) {
            // If theater mode is on, just return.
            if (manager.theater)
            {
                return
            }
            // Run for
            forElement(
                // Youtube size button
                ".ytp-size-button",
                function (button) {
                    // click it.
                    button.click()
                }
            )
        }
    )
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址