Advertisement
AyrA

Reddit /robin spam blocker

Apr 1st, 2016
2,216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         robin-spam-filter
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.4
  5. // @description  Robin Spam Filter
  6. // @author       u/AyrA_ch
  7. // @match        https://www.reddit.com/robin*
  8. // @grant        none
  9. // ==/UserScript==
  10.  
  11. function injectMessage(message)
  12. {
  13.     var d2=function(q)
  14.     {
  15.         if(q<10)
  16.         {
  17.             return "0"+q;
  18.         }
  19.         return q;
  20.     };
  21.     var dt=new Date();
  22.     var time=(dt.getHours()>12?dt.getHours()-12:dt.getHours())+":"+d2(dt.getMinutes())+":"+d2(dt.getSeconds())+" "+(dt.getHours()>=12?"PM":"AM");
  23.     var template="<div class=\"robin-message robin--flair-class--no-flair robin--message-class--message robin--user-class--system\">"+
  24.         "<span class=\"robin-message--timestamp\">"+time+"</span>"+
  25.         "<span class=\"robin-message--from robin--username\">[SCRIPT]</span>"+
  26.         "<span class=\"robin-message--message\">"+message+"</span>"+
  27.         "</div>";
  28.     document.querySelector('#robinChatMessageList').innerHTML+=template;
  29.     return template;
  30. }
  31.  
  32. function spamFilterInit(){
  33.     //CONFIG HERE//
  34.    
  35.     //Add stuff to block in the list below. Be sure to add commas except after the last item
  36.     var Block=[
  37.         "voted to GROW",
  38.         "voted to STAY",
  39.         "voted to ABANDON",
  40.         "/4cyqiy",
  41.         "/4cwk2s",
  42.         "Current grow(",
  43.         "Current stay(",
  44.         "Current abandon(",
  45.         "Available commands:",
  46.         "[Robin Autovoter",
  47.         "[Silent Robin",
  48.         "Current standings [",
  49.         "RobinFacts",
  50.         "[Robin-",
  51.         "[STATS]",
  52.         "Autovote",
  53.         "autovote",
  54.         "oooooooooooo",
  55.         "卐",
  56.         "http://",
  57.         "https://",
  58.         "www.",
  59.         "---DEV---",
  60.         "doot",
  61.         "777",
  62.         ">>>",
  63.         " - - -"
  64.     ];
  65.    
  66.     //Add users to block here:
  67.     var Users=[];
  68.    
  69.     //Add users to never block here. Same comma rule as with the "Users" variable above
  70.     var Whitelist=[
  71.         "AyrA_ch",
  72.         "[robin]"
  73.     ];
  74.    
  75.     //set to true, to see notes in the console when a message is blocked
  76.     var printer=false;
  77.     //set to true, to hide text containing excessive unicode crap
  78.     var HideUnicode=true;
  79.     //set the unicode cutoff. Lower removes more messages, but might hide legit messages too
  80.     var UnicodeSegments=20;
  81.     //Hides uppercase spam if it exceeds 1/4 of all chars
  82.     var HideUppercase=true;
  83.    
  84.     //STOP HERE//
  85.    
  86.     //generic stuff
  87.     var title=document.title.split(" ")[2];
  88.     var usercount=title.length/2;
  89.     var target=document.querySelector('#robinChatMessageList');
  90.     var txtBox=$("#robinSendMessage > input[type='text']");
  91.  
  92.     if(localStorage.getItem("robin.blockedUsers"))
  93.     {
  94.         Users=Users.concat(localStorage.getItem("robin.blockedUsers").split(','));
  95.         console.debug("Loaded blocked users list:",Users);
  96.     }
  97.    
  98.     var isUnicodeSpam=function(m)
  99.     {
  100.         return encodeURIComponent(m).replace(/\%20/g,"_").split("%").length>UnicodeSegments;
  101.     };
  102.    
  103.     var isUppercaseSpam=function(m)
  104.     {
  105.         var uc="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  106.         return m.split("").filter(function(v){return uc.indexOf(v)>=0;}).length>m.length/4;
  107.     };
  108.    
  109.     var observer = new MutationObserver(function(mutations) {
  110.         mutations.forEach(function(mutation) {
  111.             var hide=function()
  112.             {
  113.                 mutation.addedNodes[0].style.display = "none";
  114.             };
  115.             var msg = mutation.addedNodes[0].lastElementChild.textContent;
  116.             var username = mutation.addedNodes[0].lastElementChild.previousElementSibling.textContent;
  117.            
  118.             //if the username is the time, because it was missing. We add it from the previous node
  119.             if(mutation.addedNodes[0].lastElementChild.previousElementSibling.nodeName===document.createElement("time").nodeName)
  120.             {
  121.                 username=mutation.addedNodes[0].previousElementSibling.lastElementChild.previousElementSibling.textContent;
  122.                 var ele=document.createElement("span");
  123.                 ele.textContent=username;
  124.                 ele.setAttribute("class","robin-message--from robin--username");
  125.                 mutation.addedNodes[0].insertBefore(ele,mutation.addedNodes[0].lastElementChild);
  126.                 console.debug("detected user:",username);
  127.             }
  128.            
  129.             //always show our messages and system messages
  130.             if(username===$(".user > a").text() || Whitelist.indexOf(username)>=0)
  131.             {
  132.                 return;
  133.             }
  134.            
  135.             mutation.addedNodes[0].lastElementChild.previousElementSibling.onclick=function()
  136.             {
  137.                 if(confirm("block "+username+"?"))
  138.                 {
  139.                     Users.push(username);
  140.                     localStorage.setItem("robin.blockedUsers",Users.join(","));
  141.                     injectMessage("User blocked: "+username);
  142.                 }
  143.             };
  144.            
  145.             if(Users.indexOf(username)>=0)
  146.             {
  147.                 hide();
  148.                 if(printer)
  149.                 {
  150.                     console.log("[USER] blocked:",username);
  151.                 }
  152.                 return;
  153.             }
  154.             for(var x in Block)
  155.             {
  156.                 if(msg.indexOf(Block[x])>=0)
  157.                 {
  158.                     hide();
  159.                     if(printer)
  160.                     {
  161.                         console.log("[CONTENT] blocked:",Block[x]);
  162.                     }
  163.                     return;
  164.                 }
  165.             }
  166.             if(HideUppercase && isUppercaseSpam(msg))
  167.             {
  168.                 hide();
  169.                 if(printer)
  170.                 {
  171.                     console.log("[UPPERCASE] blocked");
  172.                 }
  173.             }
  174.             if(HideUnicode && isUnicodeSpam(msg))
  175.             {
  176.                 hide();
  177.                 if(printer)
  178.                 {
  179.                     console.log("[UNICODE] blocked");
  180.                 }
  181.             }
  182.         });
  183.     });
  184.     observer.observe(target, {childList: true});
  185.    
  186.     txtBox.on("keyup",function(e){
  187.         if(e.keyCode==13)
  188.         {
  189.             if(txtBox.val().indexOf("/unblock ")===0)
  190.             {
  191.                 var param=txtBox.val().split(' ')[1];
  192.                 if(Users.indexOf(param)>=0)
  193.                 {
  194.                     Users=Users.filter(function(v){return v!=param;});
  195.                     txtBox.val("");
  196.                     injectMessage("Unblocked "+param);
  197.                 }
  198.                 else
  199.                 {
  200.                     injectMessage("This user is not in the block list. Use /blist to show blocked users");
  201.                 }
  202.             }
  203.             else if(txtBox.val()=="/blist")
  204.             {
  205.                 txtBox.val("");
  206.                 injectMessage("List of blocked users:\r\n"+Users.join(", "));
  207.             }
  208.             else if(txtBox.val()=="/cls")
  209.             {
  210.                 txtBox.val("");
  211.                 target.innerHTML=injectMessage("Chat cleared");
  212.             }
  213.             else if(txtBox.val()=="/title")
  214.             {
  215.                 txtBox.val("");
  216.                 injectMessage(title);
  217.             }
  218.         }
  219.     });
  220.     console.debug("Spam filter ready");
  221.     injectMessage("Spam filter ready");
  222.     injectMessage(title);
  223.     injectMessage(usercount+" users here");
  224. }
  225.            
  226. //setTimeout(spamFilterInit, 5000);
  227.  
  228. if(typeof($)==typeof(undefined))
  229. {
  230.     console.debug("no jQuery");
  231. }
  232. else
  233. {
  234.     spamFilterInit();
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement