H2P: 虎牙自动禁言

虎牙自动禁言

目前為 2020-09-13 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        H2P: 虎牙自动禁言
// @namespace   http://tampermonkey.net/
// @version     0.0.5
// @icon        https://a.msstatic.com/huya/h5player/room/2006231627/src/img/output/replay-fornotice-icon.png
// @description 虎牙自动禁言
// @author      H2P
// @compatible  chrome
// @match       *://*.huya.com/*
// @note        2020.09.13-V0.0.05    修复虎牙禁言(禁言记录可查看控制台输出)
// ==/UserScript==

(function() {
  'use strict';

  // 在字符串前(后)添加 0
  function add0(s = '', len = 0, isAddFront = true) {
    s = s.toString();
    while (s.length < len) { s = isAddFront ? '0' + s : s + '0'; }
    return s;
  }

  const $H2P = function (xpath, one = true) {
    if (one) { return document.querySelector(xpath); }
    else { return Array.from(document.querySelectorAll(xpath)); }
  }

  // format time: yyyy-MM-dd hh-mm-ss
  Date.prototype.$formatTime = function() { return `${this.getFullYear()}-${add0(this.getMonth() + 1, 2)}-${add0(this.getDate(), 2)} ${add0(this.getHours(), 2)}:${add0(this.getMinutes(), 2)}:${add0(this.getSeconds(), 2)}`; }
  

  const myKeyCode = {
    'a': 65, 'b': 66, 'c': 67, 'd': 68, 'e': 69, 'f': 70, 'g': 71,
    'h': 72, 'i': 73, 'j': 74, 'k': 75, 'l': 76, 'm': 77, 'n': 78,
    'o': 79, 'p': 80, 'q': 81, 'r': 82, 's': 83, 't': 84,
    'u': 85, 'v': 86, 'w': 87, 'x': 88, 'y': 89, 'z': 90,
  }

  const HuyaMute = 'h2p-huya-config-mute';
  let config_mute_pre = {
    // 恶意刷屏
    EYiShuaPing: {
      time    : '1800',
      keyWords: [],
    },
    // 谩骂
    ManMa: {
      time    : '1800',
      keyWords: [],
    },
    // 刷广告
    ShuaGuangGao: {
      time    : '1800',
      keyWords: [],
    }
  };
  let config_mute = {};
  Object.assign(config_mute, config_mute_pre);
  let config_mute_tmp = JSON.parse(localStorage.getItem(HuyaMute)) || {};
  Object.assign(config_mute, config_mute_tmp);
  for (let key in config_mute) { if (!(key in config_mute_pre)) { delete config_mute[key]; } }
  localStorage.removeItem(HuyaMute);
  localStorage.setItem(HuyaMute, JSON.stringify(config_mute));

  const muteTimes = ['1800', '86400', '259200', '604800'];
  const muteTimes2 = ['30分钟', '1天', '3天', '7天'];
  const muteTypes = ['恶意刷屏', '谩骂', '刷广告'];

  new Promise((resolve, reject) => {
    let style = document.createElement('style');
    style.innerHTML = `
      #h2p-huya-script {
        position        : fixed;
        top             : 60px;
        left            : 0;
        width           : 450px;
        height          : 325px;
        border-radius   : 0 0 10px 0;
        padding         : 10px;
        background      : #eeeeee;
        z-index         : 100000;
      }
      .h2p-huya-div {
        display         : flex;
        flex-flow       : row wrap;
        justify-content : space-between;
      }
      .h2p-huya-layer {
        justify-content : flex-start;
        width           : 140px;
      }
      .h2p-huya-textarea {
        width           : 130px;
        height          : 265px;
        padding         : 5px;
        margin          : 5px 0 0;
        resize          : none;
        font-size       : 10px;
        line-height     : 15px;
      }
      .h2p-huya-button {
        width           : 100%;
        height          : 25px;
        border          : none;
        border-radius   : 5px;
        background-color: #00deba;
        cursor          : pointer;
        outline         : none;
        transition      : all 0.5s;
      }
      .h2p-huya-button:hover {
        background-color: #00ccaa;
      }
      .h2p-huya-button-active {
        background-color: #99aaff;
        transition      : all 0.5s;
      }
      .h2p-huya-button-active:hover {
        background-color: #8899cc;
      }
    `;
    document.body.appendChild(style);

    let div = document.createElement('div');
    div.id = 'h2p-huya-script';
    div.style = 'display: none';
    div.innerHTML = `
      <div class="h2p-huya-div">
        <div class="h2p-huya-layer">
          <label>恶意刷屏:</label>
          <select id="h2p-huya-select-EYiShuaPing">
            <option value="1800">30 分钟</option>
            <option value="86400">1 天</option>
            <option value="259200">3 天</option>
            <option value="604800">7 天</option>
          </select>
          <textarea id="h2p-huya-textarea-EYiShuaPing" class="h2p-huya-textarea"></textarea>
        </div>
        <div class="h2p-huya-layer">
          <label>谩骂:</label>
          <select id="h2p-huya-select-ManMa">
            <option value="1800">30 分钟</option>
            <option value="86400">1 天</option>
            <option value="259200">3 天</option>
            <option value="604800">7 天</option>
          </select>
          <textarea id="h2p-huya-textarea-ManMa" class="h2p-huya-textarea"></textarea>
        </div>
        <div class="h2p-huya-layer">
          <label>刷广告:</label>
          <select id="h2p-huya-select-ShuaGuangGao">
            <option value="1800">30 分钟</option>
            <option value="86400">1 天</option>
            <option value="259200">3 天</option>
            <option value="604800">7 天</option>
          </select>
          <textarea id="h2p-huya-textarea-ShuaGuangGao" class="h2p-huya-textarea"></textarea>
        </div>
      </div>
      <div class="h2p-huya-div">
        <button id="h2p-huya-button" class="h2p-huya-button">启动</button>
      </div>
    `;

    document.body.appendChild(div);

    resolve();
  })
  .then(() => {
    let div = $H2P('div#h2p-huya-script');
    div.addEventListener('change', (e) => {
      const target = e.target;
      if (target.tagName.toLowerCase() !== 'select') return;

      const value = target.selectedOptions[0].value;
      if (target.id === 'h2p-huya-select-EYiShuaPing') {
        config_mute.EYiShuaPing.time = value;
      } else if (target.id === 'h2p-huya-select-ManMa') {
        config_mute.ManMa.time = value;
      } else if (target.id === 'h2p-huya-select-ShuaGuangGao') {
        config_mute.ShuaGuangGao.time = value;
      }

      localStorage.setItem(HuyaMute, JSON.stringify(config_mute));
    });

    div.addEventListener('input', (e) => {
      const target = e.target;
      if (target.tagName.toLowerCase() !== 'textarea') return;

      const value = target.value;
      if (target.id === 'h2p-huya-textarea-EYiShuaPing') {
        config_mute.EYiShuaPing.keyWords = value.split('\n');
      } else if (target.id === 'h2p-huya-textarea-ManMa') {
        config_mute.ManMa.keyWords = value.split('\n');
      } else if (target.id === 'h2p-huya-textarea-ShuaGuangGao') {
        config_mute.ShuaGuangGao.keyWords = value.split('\n');
      }

      localStorage.setItem(HuyaMute, JSON.stringify(config_mute));
    });

    let button = $H2P('button#h2p-huya-button');
    button.addEventListener('click', () => {
      button.classList.toggle('h2p-huya-button-active');
      button.textContent = button.classList.contains('h2p-huya-button-active') ? '启动中' : '启动';
      setINVL_checkMsg();
    });
  })
  .then(() => {
    for (let i = 0; i < muteTimes.length; i++) {
      if (config_mute.EYiShuaPing.time === muteTimes[i]) {
        $H2P('select#h2p-huya-select-EYiShuaPing').selectedIndex = i;
      }
      if (config_mute.ManMa.time === muteTimes[i]) {
        $H2P('select#h2p-huya-select-ManMa').selectedIndex = i;
      }
      if (config_mute.ShuaGuangGao.time === muteTimes[i]) {
        $H2P('select#h2p-huya-select-ShuaGuangGao').selectedIndex = i;
      }
    }
    $H2P('textarea#h2p-huya-textarea-EYiShuaPing').value = Array.isArray(config_mute.EYiShuaPing.keyWords) ? config_mute.EYiShuaPing.keyWords.join('\n') : '';
    $H2P('textarea#h2p-huya-textarea-ManMa').value = Array.isArray(config_mute.ManMa.keyWords) ? config_mute.ManMa.keyWords.join('\n') : '';
    $H2P('textarea#h2p-huya-textarea-ShuaGuangGao').value = Array.isArray(config_mute.ShuaGuangGao.keyWords) ? config_mute.ShuaGuangGao.keyWords.join('\n') : '';
  })
  .then(() => {
    document.addEventListener('keydown', (e) => {
      if (e.shiftKey && e.which == myKeyCode.j) {
        $H2P('div#h2p-huya-script').style.display = $H2P('div#h2p-huya-script').style.display === 'none' ? '' : 'none';
      }
    });
  })

  let checkMsg = null;
  let muteUser = null;
  let minDataID = -1;             // 检测弹幕开始编号
  function setINVL_checkMsg() {
    if (checkMsg) {
      window.clearInterval(checkMsg);
      window.clearInterval(muteUser);
      checkMsg = null;
      muteUser = null;
      return;
    }
    checkMsg = setInterval(() => {
      if (muteUser) { return; }
      let msgs = $H2P('ul#chat-room__list > li', false).filter(ele => Number(ele.getAttribute('data-id')) > minDataID);
      let index = -1;
      let timeIndex = -1;
      for (let i = 0; i < msgs.length; i++) {
        let ele = msgs[i];
        minDataID = Number(ele.getAttribute('data-id'));
        if (ele.querySelector('span.msg') && ele.querySelector('span.name.J_userMenu')) {
          let user = ele.querySelector('span.name.J_userMenu').textContent;
          let msg = ele.querySelector('span.msg').textContent.replace(/\s*/g, '');
          console.log(`检测[${user}]发的弹幕:${msg}`)
          // 判断是否存在满足禁言的弹幕
          for (let j = 0; j < config_mute.EYiShuaPing.keyWords.length && timeIndex < 0; j++) {
            let keyWord = config_mute.EYiShuaPing.keyWords[j];
            if (keyWord.length > 0 && msg.includes(keyWord)) {
              index = 0;
              timeIndex = muteTimes.indexOf(config_mute.EYiShuaPing.time);
              break;
            }
          }
          for (let j = 0; j < config_mute.ManMa.keyWords.length && timeIndex < 0; j++) {
            let keyWord = config_mute.ManMa.keyWords[j];
            if (keyWord.length > 0 && msg.includes(keyWord)) {
              index = 1;
              timeIndex = muteTimes.indexOf(config_mute.ManMa.time);
              break;
            }
          }
          for (let j = 0; j < config_mute.ShuaGuangGao.keyWords.length && timeIndex < 0; j++) {
            let keyWord = config_mute.ShuaGuangGao.keyWords[j];
            if (keyWord.length > 0 && msg.includes(keyWord)) {
              index = 2;
              timeIndex = muteTimes.indexOf(config_mute.ShuaGuangGao.time);
              break;
            }
          }
        }
        if (timeIndex > -1) {
          ele.querySelector('span.name.J_userMenu').click();
          // 开始禁言
          muteUser = setInterval(() => {
            // 禁言后没有该按钮
            const ele_openMute = $H2P('div[class^=user-viewer-content] i[class^=mute-icon]');
            if (ele_openMute) {
              window.clearInterval(muteUser);
              muteUser = null;

              // 该用户已被禁言
              if (ele_openMute.nextSibling.textContent === '解除禁言') {

              } else if (ele_openMute.nextSibling.textContent === '禁止发言') {
                // 隐藏禁言选项框
                let style = document.createElement('style');
                style.id = 'h2p-huya-style-mute';
                style.innerHTML = `
                  #duya-header, #J_mainWrap, #J_roomBd,
                  .mod-sidebar, .duya-header-wrap {
                    z-index         : 99999;
                  }
                  #J_mainWrap, #main_col {
                    background-color: #f4f5f8;
                  }
                  .dlg {
                    display: none!important;
                  }
                `;
                !$H2P('style#h2p-huya-style-mute') && document.body.appendChild(style);

                ele_openMute.click();
                muteUser = setInterval(() => {
                  const muteTime = $H2P(`div[class^=mute-add] div[class^=form-item--]:nth-child(3) p[class^=radio--]`, false);
                  const muteType = $H2P(`div[class^=mute-add] div[class^=form-item--]:nth-child(4) p[class^=radio--]`, false);
                  if (muteTime) {
                    window.clearInterval(muteUser);
                    muteUser = null;

                    // 选择禁言时间和类型
                    muteTime[timeIndex].click();
                    muteType[index].click();
                    muteUser = setInterval(() => {
                      if ($H2P(`a[class^=modal-btn--]`)) {
                        window.clearInterval(muteUser);
                        muteUser = null;

                        console.log(`${new Date().$formatTime()}  --  【${ele.querySelector('span.name.J_userMenu').textContent}】【${muteTypes[index]}】:${ele.querySelector('span.msg').textContent.replace(/\s*/g, '')}  --  禁言【${muteTimes2[timeIndex]}】`);
                        $H2P(`a[class^=modal-btn--]`).click();
                        $H2P('style#h2p-huya-style-mute') && $H2P('style#h2p-huya-style-mute').remove();
                        $H2P('i[class^=ucard-x]').click(); // 关闭个人资料
                      }
                    }, 50);
                  }
                }, 50);
              }
            } else if ($H2P('i[class^=ucard-x]')) {
              window.clearInterval(muteUser);
              muteUser = null;

              console.log(`无法禁言 ${ele.querySelector('span.name.J_userMenu').textContent}`);
              $H2P('i[class^=ucard-x]').click();
            }
          }, 50);
          break;
        };
      }
    }, 500);
  }
})();