Hide NG Streams - twitch.tv

twitchのライブ一覧からNGワードで指定したものを含むチャンネルを非表示にします

// ==UserScript==
// @name        Hide NG Streams - twitch.tv
// @namespace   Violentmonkey Scripts

// @include     https://www.twitch.tv/*


// @grant       none
// @version     1.0.2
// @license     MIT
// @run-at      document-end
// @noframes

// @description twitchのライブ一覧からNGワードで指定したものを含むチャンネルを非表示にします
// ==/UserScript==

const NG_WORDS = [].map(x => x.toLowerCase());

const reg = null; //new RegExp(//, 'i')

const HIDDEN_ATTR = 'data-hidden';

let remove_retried = 0

function shouldBeHidden(x) {
  const lowerX = x.toLowerCase();
  return NG_WORDS.some((ng) => lowerX.includes(ng)) || (reg && reg.test(x));
}

function hide() {
  const channels = [...document.querySelectorAll('article')].filter(x => {
    return !x.getAttribute(HIDDEN_ATTR)
  });

  if (channels.length === 0 && remove_retried < 3) {
    remove_retried +=1;
    setTimeout(
      () => remove()
    , 100);
    return;
  }

  channels.forEach(function(x) {
    if(shouldBeHidden(x.innerText)) {
      x?.parentNode?.parentNode?.parentNode?.parentNode?.setAttribute('style', 'display: none;');
      x.setAttribute(HIDDEN_ATTR, true);
    }
  })
}

const observer = new MutationObserver(() => {
  if(window.location.pathname === '/directory/all' || window.location.pathname.startsWith('/directory/category')){
    hide();
  };
});


(function() {
  const body = document.querySelector('body');
  if (body) {
    observer.observe(body, {
      attributes: true,
      childList: true,
      subtree: true,
    });
  }
})()