Hide Slack User

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

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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)
})();