Live Danmaku Controller

Auto turn off live danmaku

As of 2020-11-13 17:01:03 UTC. See the latest version.

// ==UserScript==
// @name                Live Danmaku Controller
// @name:en-US          Live Danmaku Controller
// @name:zh-CN          直播弹幕控制
// @name:zh-TW          直播彈幕控制
// @description         Auto turn off live danmaku
// @description:en-US   Auto turn off live danmaku
// @description:zh-CN   自动关闭直播弹幕
// @description:zh-TW   自動關閉直播彈幕
// @namespace           live-danmaku-controller
// @version             2020.11.14
// @author              Akatsuki Rui
// @license             MIT License
// @grant               GM_info
// @run-at              document-idle
// @match               *://live.bilibili.com/*
// @match               *://www.douyu.com/*
// @match               *://www.huya.com/*
// @match               *://www.yy.com/*
// ==/UserScript==

"use strict";

const SELECTOR = {
  "live.bilibili.com": {
    on: "i[class='live-icon-danmaku-on']",
    off: "i[class='live-icon-danmaku-off']",
  },
  "www.douyu.com": {
    on: "div[class^='showdanmu-']:not([class*='removed-'])",
    off: "div[class^='hidedanmu-']:not([class*='removed-'])",
  },
  "www.huya.com": {
    on: "div[class='danmu-show-btn'][title='关闭弹幕']",
    off: "div[class='danmu-show-btn danmu-hide-btn'][title='开启弹幕']",
  },
  "www.yy.com": {
    on: "div[class~='yc__bullet-comments-btn'][title='关闭弹幕']",
    off: "div[class~='yc__bullet-comments-btn'][title='打开弹幕']",
  },
};

// Delay danmaku disabler for some sites (Default 10s)
const DELAY_TIME = 10000;
const DELAY_SITE = ["www.yy.com"];

const LIVE_SITE = document.location.hostname;

// Danmaku disabler
function disableDanmaku() {
  let buttonOn = document.querySelector(SELECTOR[LIVE_SITE].on);
  if (buttonOn !== null) {
    buttonOn.click();
  }
  setTimeout(() => {
    if (document.querySelector(SELECTOR[LIVE_SITE].off) === null) {
      disableDanmaku();
    }
  }, 500);
}

// To fix the button is showing as OFF, but danmaku still appear
DELAY_SITE.includes(LIVE_SITE)
  ? setTimeout(disableDanmaku, DELAY_TIME)
  : disableDanmaku();