Skip to content

Instantly share code, notes, and snippets.

@octaharon
Created November 13, 2020 15:07
Show Gist options
  • Save octaharon/5e9224516dc0c40b89ab9fa6c6c41731 to your computer and use it in GitHub Desktop.
Save octaharon/5e9224516dc0c40b89ab9fa6c6c41731 to your computer and use it in GitHub Desktop.
Tampermonkey script for filtering slack messages by user
// ==UserScript==
// @name Slack filter messages by UID
// @namespace Slack
// @version 0.1
// @description removes messages from unwanted people in slack channels and threads, or replaces them with kittens
// @author Octaharon <Alexander Uskov>
// @include https://app.slack.com/client/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
const banIds=['U06KSSJFN']; //User ids to filter
const useKittens=true; // use texture. if false, message blocks are removed seamlessly
const kittenTextureUrl='https://d2gg9evh47fn9z.cloudfront.net/800px_COLOURBOX22766112.jpg'; //texture to replace messages
const trackingLink=(id)=>`<a data-message-sender="${id}"/>`;
let removeNodes=[];
const replaceNodes=(node, threads=false)=>{
let messageSender, messageCount=0;
let elementArray=Array.from(node.querySelectorAll("div.c-virtual_list__item"));
if (threads)
elementArray=elementArray.filter(el=>/threads_view_[A-Z0-9]+/.test(el.id));
elementArray.forEach(el=>{
const l=el.querySelector('a[data-message-sender]');
if (l)
messageSender=l.getAttribute('data-message-sender');
if (banIds.includes(messageSender)){
messageCount++;
if (useKittens){
removeNodes.push(el);
}
else
{
el.innerHTML=trackingLink(messageSender);
}
}
else if (useKittens && removeNodes.length){
const offsetHeight=removeNodes.map(t=>t.offsetHeight||0).reduce((sum,t)=>sum+t,0);
removeNodes.forEach((t,ix)=>{
if (ix===0)
t.innerHTML=`<div style='width:100%; height:${offsetHeight}px; background:url("${kittenTextureUrl}") top left;'>${trackingLink(messageSender)}</div>`;
else
t.innerHTML=trackingLink(messageSender);
});
removeNodes=[];
}
});
return messageCount;
};
setInterval(()=>{
const node=document.querySelector('div.c-message_list');
let msgCount=0;
if (node) {
msgCount+=replaceNodes(node);
}
if (document.getElementById('threads_view'))
{
const msgCount=replaceNodes(document.getElementById('threads_view'),true);
}
if (msgCount) console.warn(msgCount +' messages filtered');
},100);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment