Youtube Automatic Max Quality

Set Youtube player automatically to max quality.

目前為 2024-02-15 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Youtube Automatic Max Quality
// @namespace    http://tampermonkey.net/
// @version      2024-02-15
// @description  Set Youtube player automatically to max quality.
// @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(
        // settings button
        ".ytp-settings-button",
        function (buttonSettings) {
            // click settings button
            buttonSettings.click()
            // and run for
            forElement(
                // quality selection button
                ".ytp-panel .ytp-menuitem:nth-child(5)",
                function (buttonSelectQuality) {
                    // click quality selection button
                    buttonSelectQuality.click()
                    // and run for first quality option
                    forElement(
                        ".ytp-quality-menu .ytp-menuitem:nth-child(1)",
                        function (buttonMaxQuality) {
                            // click max quality option.
                            buttonMaxQuality.click()
                        }
                    )
                }
            )
        }
    )

    // NOTE: If you dont have Youtube Premium or dont care for 1080p Premium, you could just use this:
    /*
    // Run for
    forElement(
        // video player
        ".html5-video-player",
        function (player) {
            // get max quality (cannot use getMaxPlaybackQuality because it returns 'auto')
            const maxQuality = player.getAvailableQualityLevels()[0]
            // set max quality.
            player.setPlaybackQualityRange(maxQuality, maxQuality)
        }
    )
    */
})();