Hide Slack User

Hide / mute / censor / block specified users in Slack.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hide Slack User
// @namespace    d6b21ea17506e772
// @version      1.0.0
// @grant        none
// @description  Hide / mute / censor / block specified users in Slack.
// @match        https://*.slack.com/*
// @author       Picadillo
// ==/UserScript==
(()=>{
const hiddenUsers = ['some_user', 'some_other_user']

// Iterates a container of .c-virtual_list__items
// and, if hiddenUsers includes their sender, hides them.
const hideMessages = (container)=>{
  let hide = false

  for (const message of container.children) {
    if (message.className != 'c-virtual_list__item') { continue }

    // Detect a.c-message__sender_link and, if present, update hide.
    const sender = message.querySelector('a.c-message__sender_link')
    if (sender != null) {
      hide = hiddenUsers.includes(sender.textContent)
    }
    // If hiddenUsers includes the most recent sender, then hide.
    if (hide) {
      message.style.display = 'none'
    }
  }
}
// Observes a container for childList updates
// and, when they occur, invokes hideMessages(container).
const observeMessages = (container)=>{
  // Observe at most once per container.
  if (container.hasAttribute('data-observe_messages')) { return }
  container.setAttribute('data-observe_messages', true)

  const callback = (mutations)=>{
    hideMessages(container)
  }
  callback()
  const observer = new MutationObserver(callback)
  observer.observe(container, {childList:true})
}
// Observes the layout for childList subtree updates
// and, when they occur, searches for the specified view
// and, if found, searches the view for a container
// and, if found, invokes observeMessages(container).
const observeContainerIn = (layout, view)=>{
  const callback = (mutations)=>{
    for (const child of layout.children) {
      if (child.className === view) {
        const container = child.querySelector('div.c-virtual_list__scroll_container')
        if (container != null) { observeMessages(container) }
      }
    }
  }
  callback()
  const observer = new MutationObserver(callback)
  observer.observe(layout, {childList:true, subtree:true})
}
window.addEventListener('load', ()=>{
  const workspace_layout = document.querySelector('div.p-workspace-layout')
  observeContainerIn(workspace_layout, 'p-workspace__primary_view')
  observeContainerIn(workspace_layout, 'p-workspace__secondary_view')
}, false)
})();