Greasy Fork

Greasy Fork is available in English.

Bilibili - 自动切换直播画质至最高画质

自动切换Bilibili直播画质至最高画质 | V0.4 代码优化

当前为 2023-10-22 提交的版本,查看 最新版本

// ==UserScript==
// @name         Bilibili - 自动切换直播画质至最高画质
// @namespace    https://bilibili.com/
// @version      0.4
// @description  自动切换Bilibili直播画质至最高画质 | V0.4 代码优化
// @license      GPL-3.0
// @author       DD1969
// @match        https://live.bilibili.com/*
// @icon         https://www.bilibili.com/favicon.ico
// @grant        none
// ==/UserScript==

(async function() {
  'use strict';

  // override 'split' method of string to change the processing cookie string
  const originSplit = String.prototype.split;
  String.prototype.split = function (spliter) {
    let str = this;
    if (spliter === ';' && str.length > 0) str += '; DedeUserID=123456';
    return originSplit.call(str, spliter);
  }

  // wait until those essential variables/methods loaded
  await new Promise(resolve => {
    const timer = setInterval(() => {
      if (window.livePlayer && window.livePlayer.getPlayerInfo && window.livePlayer.switchQuality) {
        clearInterval(timer);
        resolve();
      }
    }, 200);
  });

  // switch quality
  do {
    const { quality, qualityCandidates } = window.livePlayer.getPlayerInfo();
    if (quality !== qualityCandidates[0].qn) {
      window.livePlayer.switchQuality(qualityCandidates[0].qn);
      await new Promise(resolve => setTimeout(resolve, 5000));
    } else break;
  } while(true);

  // hide the loading gif
  const styleElement = document.createElement('style');
  styleElement.textContent = `.web-player-loading { opacity: 0; }`;
  document.head.appendChild(styleElement);

})();