Dumb Youtube lowest quality chooser

Auto select the lowest available quality of videos (more eco-friendly, and you can still manually select a higher resolution when needed)

目前為 2020-03-08 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Dumb Youtube lowest quality chooser
// @name:fr         Vidéos Youtube en basse minimale par défaut
// @version         1
// @description     Auto select the lowest available quality of videos (more eco-friendly, and you can still manually select a higher resolution when needed)
// @description:fr  Choisir automatiquement la qualité minimum pour la lecture des vidéos (plus eco-friendly, avec toujours la possibilité de choisir manuellement une meilleure qualité quand nécessaire)
// @author          Les noix de coco
// @include         http://youtube.com/*
// @include         https://youtube.com/*
// @include         http://www.youtube.com/*
// @include         https://www.youtube.com/*
// @include         http://gaming.youtube.com/*
// @include         https://gaming.youtube.com/*
// @noframes
// @grant           none
// @namespace https://greasyfork.org/users/456082
// ==/UserScript==

(function() {

    "use strict"

    function sortNumber(a, b) {
      return a - b;
    }

    const DELAY = 1

    function setLowestQuality()
    {
      if (!window.location.href.match(/\?.*v=/)) {
        // Not a video
      	return
      }
      console.log('Will try to set video quality...')
      setTimeout(() => {
        const videoSettingsButtonEl = document.querySelector('.ytp-settings-button')
        if (!videoSettingsButtonEl) {
          return
        }
      	videoSettingsButtonEl.dispatchEvent(new Event('click'))

        setTimeout(() => {
          let found = false
          for (const el of document.querySelectorAll('.ytp-menuitem')) {
            if (el.textContent.match(/Qual|Calidad/)) {
              found = true
            	el.dispatchEvent(new Event('click'))
              break
            }
          }
          if (!found) {
            return
          }

          setTimeout(() => {
            const qualities = {}
            for (const el of document.querySelectorAll('.ytp-menuitem')) {
              const match = el.textContent.match(/(\d+)p/)
            	if (match) {
                const qualityNum = match[1]
              	qualities[qualityNum] = el
              }
            }
            //let qualitiesList = Object.keys(qualities).map(x => { return parseInt(x) })
            let qualitiesList = Object.keys(qualities).sort(sortNumber)
            console.log('Qualities (sorted):', qualitiesList)
            const quality = qualitiesList[0]
            console.log(`Setting lowest quality: ${quality}p`)
            qualities[quality].dispatchEvent(new Event('click'))
          }, DELAY)

        }, DELAY)

      }, DELAY)
    }

    window.addEventListener("yt-navigate-finish", setLowestQuality, true)

})();