Greasy Fork

Greasy Fork is available in English.

斗鱼直播自动切换画质

斗鱼直播自动切换分辨率,全局设置最高画质或最低画质,单独设置每个直播间:原画、4K、2K、1080p、蓝光、720p、超清、高清清晰度。

当前为 2022-07-29 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         斗鱼直播自动切换画质
// @namespace    https://github.com/Eished
// @author       Eished
// @version      2022.07.30
// @description  斗鱼直播自动切换分辨率,全局设置最高画质或最低画质,单独设置每个直播间:原画、4K、2K、1080p、蓝光、720p、超清、高清清晰度。
// @license      AGPL-3.0
// @match        *://www.douyu.com/*
// @icon         https://www.google.com/s2/favicons?domain=douyu.com
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function () {
  'use strict'

  const videoSub = document.querySelector('.layout-Player-videoSub')
  if (videoSub == undefined) {
    return
  }
  console.log(rid, 'douyu rid')

  const Clarities = ['全局默认最高画质', '全局默认最低画质']

  let selectedClarity = GM_getValue(rid)
  const defaultClarity = GM_getValue('defaultClarity')
  if (!selectedClarity) {
    if (defaultClarity === 0) {
      selectedClarity = Clarities[1]
    } else {
      selectedClarity = Clarities[0]
    }
  }

  const scanClarity = list => {
    let notFoundCount = 0
    list.forEach(li => {
      const availableClarity = li.innerText
      if (availableClarity) {
        GM_registerMenuCommand(availableClarity, () => {
          GM_setValue(rid, availableClarity)
          li.click()
        })
        if (selectedClarity === availableClarity) {
          li.click()
        } else {
          notFoundCount++
        }
      }
    })

    if (selectedClarity === Clarities[0]) {
      list[0].click()
    } else if (selectedClarity === Clarities[1]) {
      list[list.length - 1].click()
    } else if (notFoundCount === list.length) {
      if (defaultClarity === 0) {
        list[list.length - 1].click()
      } else {
        list[0].click()
      }
    }

    Clarities.forEach((clarity, index) => {
      GM_registerMenuCommand(clarity, () => {
        if (index === 0) {
          list[index].click()
          GM_setValue('defaultClarity', 1)
        } else {
          list[list.length - 1].click()
          GM_setValue('defaultClarity', 0)
        }
      })
    })
  }

  const observer = new MutationObserver(callback)
  observer.observe(videoSub, {
    childList: true,
    subtree: true,
  })

  function callback() {
    const controller = videoSub.querySelector(`[value="画质 "]`)
    if (controller) {
      observer.disconnect()
      const ul = controller.nextElementSibling
      const list = ul.querySelectorAll('li')
      scanClarity(list)
    }
  }
})()