Default Resolution - YouTube

Choose the default resolution for YouTube videos!

As of 2024-03-29. See the latest version.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Default Resolution - YouTube
// @namespace    https://greasyfork.org/en/users/670188-hacker09?sort=daily_installs
// @version      1
// @description  Choose the default resolution for YouTube videos!
// @author       hacker09
// @match        https://m.youtube.com/watch?v=*
// @match        https://www.youtube.com/embed/*
// @match        https://www.youtube.com/watch?v=*
// @icon         https://www.youtube.com/s/desktop/03f86491/img/favicon.ico
// @run-at       document-end
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

setTimeout(() => {
  'use strict';
  if (GM_getValue('Default_Desktop_Resolution') === undefined) //If the Default_Resolution wasn't set yet
  { //Starts the if condition
    GM_setValue('Default_Desktop_Resolution', 'hd1080'); //Save the Default YT Desktop Resolution as 1080p
    GM_setValue('Default_Mobile_Resolution', 'tiny'); //Save the Default YT Mobile Resolution as 144p
  } //Finishes the if condition

  if (localStorage.getItem('yt-player-quality') === null || (navigator.userAgentData.mobile === false && (document.getElementById("movie_player").getAvailableQualityLevels().includes(GM_getValue('Default_Desktop_Resolution')) === true && JSON.parse(localStorage.getItem('yt-player-quality'))?.data !== GM_getValue('Default_Desktop_Resolution')))) { //If the user never manually set the resolution before or if the default user resolution exists and if the current video resolution isn't the default user-chosen resolution
    localStorage.setItem('yt-player-quality', `{\"data\":\"${(navigator.userAgentData.mobile === true) ? GM_getValue('Default_Mobile_Resolution') : (document.getElementById("movie_player").getAvailableQualityLevels().includes(GM_getValue('Default_Desktop_Resolution')) === true) ? GM_getValue('Default_Desktop_Resolution') : document.getElementById("movie_player").getAvailableQualityLevels()[0]}\",\"expiration\":${new Date().valueOf() + 30 * 24 * 60 * 60 * 1000},\"creation\":${new Date().valueOf()}}`);
    location.reload(); //Reloads the page to apply the video resolution
  } //Finishes the if condition
}, 800);