🏠 Home 

Slack user censor

Replace the messages of a special user with a tiny red bar


Install this script?
  1. // ==UserScript==
  2. // @name Slack user censor
  3. // @version 1.0.0
  4. // @grant none
  5. // @author github.com/geezee
  6. // @description Replace the messages of a special user with a tiny red bar
  7. // @match https://app.slack.com/*
  8. // @license GPLv3
  9. // @namespace https://greasyfork.org/users/387744
  10. // ==/UserScript==
  11. const userId = ""; // user id can be retrieved from data-message-sender attribute on .c-message__sender_link
  12. const targetInstanceName = ""; // the name of the workplace to run this script on
  13. const style = "background:rgba(255,50,50,0.1); padding: 3px 20px; font-size: 8px;"; // the style of the red bar
  14. const blockMsg = "Message blocked";
  15. const hoverMsg = "read"; // if you don't want to read the message when hovering then set this to an empty string
  16. function blockMessage(msg) {
  17. const bodyContainer = msg.querySelector(".c-message__body");
  18. if (bodyContainer == null) return;
  19. const body = bodyContainer.innerHTML;
  20. msg.innerHTML = '<div style="' + style + '">'
  21. + '<a href="#" data-message-sender="' + userId + '"></a>'
  22. + blockMsg
  23. + ' - <abbrv class="c-message__contents__toblock">' + hoverMsg + '</abbrv></div>';
  24. msg.querySelector(".c-message__contents__toblock").title = body;
  25. }
  26. function hideMessages() {
  27. const messages = Array.from(document.querySelectorAll(".c-message"));
  28. for (var i=0; i<messages.length; i++) {
  29. var msg = messages[i];
  30. // make sure the message has an author and it matches the troll's id
  31. const senderLink = msg.querySelector("a.c-message__sender_link");
  32. if (senderLink == null) continue;
  33. const sender = senderLink.dataset.messageSender;
  34. if (sender != userId) continue;
  35. // replace the message with a block notice
  36. blockMessage(msg);
  37. // search for adjacent messages
  38. while (messages[i+1].classList.contains("c-message--adjacent")) {
  39. i++;
  40. blockMessage(messages[i]);
  41. }
  42. }
  43. }
  44. setTimeout(function() {
  45. const instanceName = document.querySelector(".p-classic_nav__team_header__team__name").innerHTML;
  46. if (instanceName != targetInstanceName) return;
  47. document.querySelector(".p-workspace__primary_view").addEventListener("DOMSubtreeModified", () => {
  48. setTimeout(hideMessages, 1000);
  49. });
  50. hideMessages();
  51. }, 2000);