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';
    /**
     * Get element with selector and call callback with it.
     * @param {string} selector Selector for the element.
     * @param {function} callback Callback function to call with the element.
     */
    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) {
            // 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
            forElement.timeoutCount[selector] = 0
            // and call callback with element.
            callback(element)
        }
    }

    // Run for
    forElement(
        // Youtube page manager
        ".ytd-page-manager",
        function (manager) {
            console.error("manager.theater", manager.theater)
            // If theater mode is on
            if (manager.theater) {
                // just return.
                return
            }

            // Run for
            forElement(
                // Youtube size button
                ".ytp-size-button",
                function (button) {
                    /**
                     * Turn Youtube player to theater mode.
                     */
                    function toTheaterMode() {
                        // If manager is already in theater mode
                        if (manager.theater) {
                            // just return.
                            return
                        }

                        // Click the size button.
                        button.click()

                        // Init timeout count.
                        if (toTheaterMode.timeoutCount === undefined)
                            toTheaterMode.timeoutCount = 0

                        // Try again after timeout.
                        setTimeout(
                            function () {
                                toTheaterMode()
                            },
                            (
                                // Base timeout.
                                100
                                *
                                // Increase timeout after each try.
                                (toTheaterMode.timeoutCount++)
                            )
                        )
                    }

                    // Call turning to theater mode.
                    toTheaterMode()
                }
            )
        }
    )
})();

QingJ © 2025

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