Greasy Fork

Greasy Fork is available in English.

自用bilibili脚本

吧啦吧啦

当前为 2021-12-09 提交的版本,查看 最新版本

// ==UserScript==
// @name         自用bilibili脚本
// @namespace    mimiko/bilibili
// @version      0.0.4
// @description  吧啦吧啦
// @author       Mimiko
// @license      MIT
// @match        *://*.bilibili.com/*
// @grant        GM.addStyle
// ==/UserScript==
// http://greasyfork.icu/zh-CN/scripts/436748-%E8%87%AA%E7%94%A8bilibili%E8%84%9A%E6%9C%AC

(() => {

  if (window.top !== window.self) return

  // function

  const asBangumiMain = () => {
    asPlayerMain()
    hide('#review_module') // 隐藏点评
  }

  const asCommonMain = () => {
    hide('#internationalHeader .nav-link-item:last-of-type') // 移除下载APP
  }

  const asIndexLive = () => {
    // 隐藏顶部播放器
    hide('.player-area-ctnr')
    const timer = window.setInterval(() => {
      const $video = document.querySelector('video')
      if (!$video) return
      window.clearInterval(timer)
      remove('.player-area-ctnr')
    }, 50)
  }

  const asIndexMain = () => {
    hide('#reportFirst2') // 隐藏推广
    hide('#bili_live') // 隐藏直播
    hide('#bili_guochuang') // 隐藏国创
    hide('#bili_manga') // 隐藏漫画
    // 处理电梯
    hideByText('#elevator .item', '直播')
    hideByText('#elevator .item', '国创')
    hideByText('#elevator .item', '漫画')
  }

  const asLive = () => {
    const path = window.location.pathname
    if (path === '/') return asIndexLive()
    if (parseInt(path.substring(1), 10)) return asRoomLive()
  }

  const asMain = () => {
    asCommonMain()
    const path = window.location.pathname
    if (path === '/') return asIndexMain()
    if (path.startsWith('/video/BV')) return asVideoMain()
    if (path.startsWith('/bangumi/play/ep')) return asBangumiMain()
  }

  const asPlayerMain = () => {
    hide('.bilibili-player-electric-panel') // 隐藏充电面板
    hide('.bilibili-player-ending-panel') // 隐藏视频推荐
    hide('.bilibili-player-video-hint') // 隐藏弹幕礼仪
    // 自动宽屏
    const timer = window.setInterval(() => {
      const $btn = document.querySelector('.bilibili-player-video-btn-widescreen')
      if (!$btn) return
      window.clearInterval(timer)
      $btn.click()
      document.querySelector('video')?.scrollIntoView() // 宽屏后滚动到视频
    }, 50)
  }

  const asRoomLive = () => {
    remove('#my-dear-haruna-vm') // 移除22/33娘
    hide('#room-background-vm') // 隐藏背景
  }

  const asVideoMain = () => {
    asPlayerMain()
  }

  const hide = (
    selector,
  ) => GM.addStyle(`${selector} { display: none !important; }`)

  const hideByText = (
    selector,
    text,
  ) => document.querySelectorAll(selector).forEach($it => {
    const content = $it.textContent?.trim()
    if (content === text) $it.setAttribute('style', 'display: none !important;')
  })

  const log = (...args) => console.log('[bilibili]', ...args)

  const remove = (
    selector,
  ) => document.querySelectorAll(selector).forEach($it => $it.remove())

  const route = () => {
    const host = window.location.host.split('.')[0]
    if (host === 'www') return asMain()
    if (host === 'live') return asLive()
  }

  // execute
  route()

})()