Twitch-collapse-messages

Twitch.tv hide chat messages from certain usernames (bots)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name     Twitch-collapse-messages
// @description     Twitch.tv hide chat messages from certain usernames (bots)
// @version  1
// @grant    none
// @match    https://www.twitch.tv/*
// @namespace https://greasyfork.org/users/901386
// ==/UserScript==


//usernames based on the viewed channel
lists = {};
lists['beastr0'] = ["Moobot","Nightbot","zsemlebot"];
  
  


function log(msg) {
  console.log(`[Twitch collapse messages script] ${msg}`);
}

function registerListener() {  
  log('Entering registerListener');
  
	const chatMessageContainer = document.querySelector('[role="log"]');
  
  if (chatMessageContainer == null) {
    log("registerListener couldn't find chat box, trying again in 1s");
    setTimeout(registerListener, 1000);
    return;
  }
  
  channel = window.location.pathname.substr(1);  
  if (lists[channel] === undefined) {
    log("channel ${channel} is not in list of monitored channels");
    return;
  }
  
  const onChatMessageListModified = function(mutationsList, observer) {
      for(let mutation of mutationsList) {
          if (mutation.type !== 'childList') {
              return;
          }

          mutation.addedNodes.forEach(node => {
              const message = node.querySelector("[data-test-selector='chat-line-message-body']");
              const text = node.querySelector('[data-a-target="chat-message-text"]').innerText;
              const username = node.querySelector('[data-a-target="chat-message-username"]').innerText;
              if (lists[channel].indexOf(username) != -1) {
                log(`Hiding message: ${text}`);
                colapseMe(message);
              }
              //if (text.startsWith('!')) {
              //  log(`Hiding message: ${text}`);
              //  node.style.display = 'none';
              //}
          });
      }
  };

  const observer = new MutationObserver(onChatMessageListModified);
  observer.observe(chatMessageContainer, { childList: true });
  
  log('registerListener completed successfully');
}


function colapseMe(e) {
  console.log("adding a plus sign");
  console.log(e);
  e.style.display = "none";
  var expand_b = document.createElement("span");
  function expand_msg(){
    e.style.display = "inherit";
    expand_b.style.display ="none";
    console.log('clicked');
  }
  expand_b.innerHTML = "[+]";
  expand_b.style = "background-color:#aaa; color:#555;";
  expand_b.addEventListener("click", expand_msg);
  e.parentNode.insertBefore(expand_b, e);
  
}


log('beginning first-time registration attempt');
registerListener();