Tampermonkey: Always select best Video Quality for YouTube

I'm starting to open source the Tampermonkey Extension scripts that I'm using all the time!

07 Feb 2025
|
1 min read

This is my Tampermonkey Script to always select the highest Quality available* on YouTube.com.

There are some rules:

  • Don't go beyond 1440p, since I only have 1440p displays

  • Select 1080p Premium, if available. This will required YouTube Premium ofc

How to use

  • Install the Tampermonkey Extension for Firefox, Chrome, Edge or Safari

  • Go to the Tampermonkey Dashboard

  • Click the "+" Button to create a new script

  • Copy & Paste the Scripts below

Update

If you don't want to enable auto updates or you want to modify your script, remove the updateURL and downloadURL lines.

The Script

// ==UserScript==
// @name         YouTube always select Highest Quality
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Automatically select the best available quality for YouTube videos
// @author       Roman Zipp <[email protected]>
// @match        https://www.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// @updateURL    https://gist.githubusercontent.com/romanzipp/4225a5d81be72a59c06c11e2ff0c0495/raw/script.js
// @downloadURL  https://gist.githubusercontent.com/romanzipp/4225a5d81be72a59c06c11e2ff0c0495/raw/script.js
// ==/UserScript==

(function() {
    'use strict';

    function selectQuality(qualityButton) {
        qualityButton.click();

        const menuItems = document.querySelectorAll('.ytp-settings-menu .ytp-panel .ytp-menuitem');

        menuItems.forEach(item => {
            if (!item.innerText.includes('Quality')) {
                return;
            }

            item.click();

            const qualities = [...document.querySelectorAll('.ytp-settings-menu .ytp-panel.ytp-quality-menu .ytp-menuitem')];

            // use max 1440p if available
            const fourteenFourtyPeePee = qualities.find(q => q.innerText.includes('1440p'));

            if (fourteenFourtyPeePee) {
                fourteenFourtyPeePee.click();
                return;
            }

            // just use the best one aye
            const bestQuality = qualities[0];

            bestQuality.click();
        })
    }

    function init() {
        const qualityButton = document.querySelector('.ytp-settings-button');

        if ((qualityButton !== null) && (qualityButton !== undefined)) {
            log('found quality button without watching for mutations, excellent news.');
            selectQuality(qualityButton);
          
            return;
        }

        const observer = new MutationObserver((mutations) => {
            for (const { addedNodes } of mutations) {
                for (const node of addedNodes) {
                    if (!node.tagName) {
                        continue; // not an element
                    }

                    if (node.classList.contains('ytp-settings-button')) {
                        selectQuality(node);
                        observer.disconnect();

                        continue;
                    }

                    const qualityButtons = node.getElementsByClassName('ytp-settings-button');
                    if (qualityButtons[0] === undefined) {
                        contine;
                    }

                    selectQuality(qualityButtons[0]);
                    observer.disconnect();
                }
            }
        });

        observer.observe(document, {
            childList: true,
            subtree: true,
        });
    }

    init();

    document.body.addEventListener("yt-navigate-finish", function(event) {
        init();
    });
})();

Comments

Read more...