Bilibili - 防止视频被自动暂停及弹出登录(不可用)窗口

让您在未登录(不可用)的情况下看B站视频时不再被自动暂停视频及要求登录(不可用)账号 | V0.3 防止视频页面不可视(最小化、放置在别的桌面等)5分钟后自动暂停视频

As of 2023-12-07. See the latest version.

// ==UserScript==
// @name         Bilibili - 防止视频被自动暂停及弹出登录(不可用)窗口
// @namespace    https://bilibili.com/
// @version      0.3
// @description  让您在未登录(不可用)的情况下看B站视频时不再被自动暂停视频及要求登录(不可用)账号 | V0.3 防止视频页面不可视(最小化、放置在别的桌面等)5分钟后自动暂停视频
// @license      GPL-3.0
// @author       DD1969
// @match        https://*.bilibili.com/*
// @icon         https://www.bilibili.com/favicon.ico
// @grant        none
// ==/UserScript==

(async function() {
  'use strict';

  // always return true when trying to know whether the mask exists or not
  const originQuerySelector = document.querySelector;
  document.querySelector = function (selector) {
    return selector === '.bili-mini-mask'
      ? true
      : originQuerySelector.call(this, selector);
  }

  // prevent appending 'bili-mini-mask' element which contains login modal
  const originAppendChild = HTMLElement.prototype.appendChild;
  HTMLElement.prototype.appendChild = function (childElement) {
    return childElement && childElement.classList && childElement.classList.contains('bili-mini-mask') && childElement.innerHTML.includes('bili-mini-login')
      ? null
      : originAppendChild.call(this, childElement);
  }

  // (in video page) wait until the 'getMediaInfo' method appears
  await new Promise(resolve => {
    const timer = setInterval(() => {
      if (window.player && window.player.getMediaInfo) {
        clearInterval(timer);
        resolve();
      }
    }, 1000);
  });

  // modify the 'getMediaInfo' method
  const originFunc = window.player.getMediaInfo;
  window.player.getMediaInfo = function () {
    const { absolutePlayTime, relativePlayTime, playUrl } = originFunc();
    return { absolutePlayTime: 0, relativePlayTime, playUrl };
  }

  // 'isClickedRecently' will be 'true' shortly if user clicked somewhere on the page
  let isClickedRecently = false;
  document.body.addEventListener('click', () => {
    isClickedRecently = true;
    setTimeout(() => isClickedRecently = false, 500);
  });

  // prevent pausing video by scripts
  const originPause = window.player.pause;
  window.player.pause = function () {
    if (!isClickedRecently) return;
    return originPause.apply(this, arguments);
  }

})();
长期地址
遇到问题?请前往 GitHub 提 Issues。