Youtube Music High Quality Video

Tries to set the video quality to 1440p on Youtube Music (or the highest available quality)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Youtube Music High Quality Video
// @namespace    https://github.com/Alistair1231/my-userscripts/
// @version      0.2.2
// @description  Tries to set the video quality to 1440p on Youtube Music (or the highest available quality)
// @author       Alistair1231
// @match        https://music.youtube.com/watch*
// @icon         https://icons.duckduckgo.com/ip2/youtube.com.ico
// @license      MIT
// ==/UserScript==
// https://greasyfork.org/en/scripts/535348-youtube-music-high-quality-video
// https://github.com/Alistair1231/my-userscripts/blob/master/youtube-music-high-quality-video.user.js

(function () {
  "use strict";
  /**
   *
   * @param {string} quality - The quality to set the video to. Can be one of the following:
   * auto / highres / hd2880 / hd2160 / hd1440 / hd1080 / hd720 / large / medium / small / tiny
   * If a quality is unavailable, it will be set to the next best quality.
   * On Youtube Music, videos seem to be limited to 1080p on the backend, but no harm in trying for more.
   * @returns
   */
  const setQuality = (quality) =>
    document.getElementById("movie_player").setPlaybackQualityRange(quality);

  const run = () => {
    if (document.getElementById("movie_player")) setQuality("hd1440");

    // create a MutationObserver to watch for new videos
    const observer = new MutationObserver((mutations) => {
      for (const mutation of mutations) {
        if (mutation.type === "childList") {
          // check if the video has changed
          const newElement = document.getElementById("movie_player");
          if (newElement) {
            setQuality("hd1440");
          }
        }
      }
    });
    // start observing the element for changes
    observer.observe(document.body, {
      childList: true,
      subtree: true,
    });
  };

  setTimeout(run, 2000);
})();