Guest User

Untitled

a guest
Apr 3rd, 2016
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Robin Enhancement Script
  3. // @namespace    https://www.reddit.com/
  4. // @version      1.4
  5. // @description  Highlight mentions, auto link links & automatically remove spam
  6. // @author       mr_bag
  7. // @match        https://www.reddit.com/robin/
  8. // @grant        none
  9. // ==/UserScript==
  10. (function() {
  11.  
  12.     // Grab users username + play nice with RES
  13.     var robin_user = $("#header-bottom-right .user a").first().text();
  14.     var ignored_users = {};
  15.  
  16.     /**
  17.      * Check if a message is "spam"
  18.      *
  19.      */
  20.     var is_spam = function(line){
  21.         return (
  22.             // Hide auto vote messages
  23.             (/^voted to (grow|stay|abandon)/.test(line)) ||
  24.             // random unicode?
  25.             (/[\u0080-\uFFFF]/.test(line)) ||
  26.             // hide any auto voter messages
  27.             (/\[.*autovoter.*\]/.test(line)) ||
  28.             // Common bots
  29.             (/^(\[binbot\]|\[robin-grow\])/.test(line)) ||
  30.             // repeating chars in line (more than 5). e.g. aaaaaaa !!!!!!!!
  31.             (/(.)\1{5,}/.test(line)) ||
  32.             // Some common messages
  33.             (/(voting will end in approximately|\[i spam the most used phrase\]|\[message from creator\])/.test(line)) ||
  34.             // no spaces = spam if its longer than 25 chars (dont filter links)
  35.             (line.indexOf(" ") === -1 && line.length > 25 && line.indexOf("http") === -1) ||
  36.             // repeating same word
  37.             /(\b\S+\b)\s+\b\1\b/i.test(line)
  38.         );
  39.     };
  40.  
  41.     /**
  42.      * Check if a message is from an ignored user
  43.      *
  44.      */
  45.     var is_ignored = function($usr, $ele){
  46.         // no user name, go looking for when said it
  47.         if($usr.length === 0){
  48.             while($usr.length === 0){
  49.                 $ele = $ele.prev();
  50.                 $usr = $ele.find(".robin--username");
  51.             }
  52.         }
  53.         // are they ignored?
  54.         return (ignored_users[$usr.text()]);
  55.     };
  56.  
  57.     /**
  58.      * Make links clickable
  59.      *
  60.      */
  61.     var auto_link = function($msg){
  62.         var text = $msg.html(); // read as html so stuff stays escaped
  63.         // normal links
  64.         text = text.replace(/\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim, '<a target="blank" href="$&">$&</a>');
  65.         // reddit subreddit links
  66.         text = text.replace(/ \/r\/(\w+)/gim, ' <a target="blank" href="https://reddit.com/r/$1">/r/$1</a>');
  67.         // update text
  68.         $msg.html(text);
  69.     };
  70.  
  71.     /**
  72.      * Mute a user
  73.      */
  74.     var _mute_user = function(usr){
  75.         // Add to ignore list
  76.         ignored_users[usr] = true;
  77.         _render_muted_list();
  78.     };
  79.  
  80.     /**
  81.      * un-mute a user
  82.      */
  83.     var _unmute_user = function(usr){
  84.         // Add to ignore list
  85.         delete ignored_users[usr];
  86.         _render_muted_list();
  87.     };
  88.  
  89.     // Render list of ignored users
  90.     var _render_muted_list = function(){
  91.         var html = "<strong>Ignored users</strong><br>";
  92.         for(var u in ignored_users){
  93.             html += "<div data-usr='"+ u + "'>" + u + " - [unmute]</div>";
  94.         }
  95.         $("#muted_users").html(html);
  96.     };
  97.  
  98.     /**
  99.      * Parse a link and apply changes
  100.      */
  101.     var parse_line = function($ele){
  102.         var $msg = $ele.find(".robin-message--message");
  103.         var $usr = $ele.find(".robin--username");
  104.  
  105.         var line = $msg.text().toLowerCase();
  106.  
  107.         // If user is ignored or message looks like "Spam". hide it
  108.         if (is_ignored($usr, $ele) || is_spam(line)) {
  109.             $ele.addClass("spam-hidden");
  110.         }
  111.  
  112.         // Highlight mentions
  113.         if(line.indexOf(robin_user) !== -1){
  114.             $ele.css("font-weight", "bold");
  115.         }
  116.  
  117.         // Make links clickable
  118.         if(line.indexOf("http") !== -1){
  119.             auto_link($msg);
  120.         }
  121.  
  122.         // Add mute button to users
  123.         if(!$ele.hasClass("robin--user-class--system") && $usr.text() != robin_user){
  124.             $("<span style='font-size:.8em;cursor:pointer'> [mute] </span>").insertBefore($usr).click(function(){
  125.                 _mute_user($usr.text());
  126.             });
  127.         }
  128.  
  129.         // Add filter support
  130.         if(line.indexOf("%") === 0){
  131.             $ele.addClass("filter-percent");
  132.         }
  133.  
  134.     };
  135.  
  136.     // Detect changes, are parse the new message
  137.     $("#robinChatWindow").on('DOMNodeInserted', function(e) {
  138.         if ($(e.target).is('div.robin-message')) {
  139.             // Apply changes to line
  140.             parse_line($(e.target));
  141.         }
  142.     });
  143.  
  144.     // When everything is ready
  145.     $(document).ready(function(){
  146.         // Set default spam filter type
  147.         $("#robinChatWindow").addClass("hide-spam");
  148.  
  149.         // Add checkbox to toggle "hide" behaviors
  150.         $("#robinDesktopNotifier").append("<label><input type='checkbox' checked='checked'>Hide spam completely</label>").click(function(){
  151.             if($(this).find("input").is(':checked')){
  152.                 $("#robinChatWindow").removeClass("mute-spam").addClass("hide-spam");
  153.             }else{
  154.                 $("#robinChatWindow").removeClass("hide-spam").addClass("mute-spam");
  155.             }
  156.         });
  157.  
  158.         // Add Muted list & hook up unmute logic
  159.         $('<div id="muted_users" class="robin-chat--sidebar-widget robin-chat--notification-widget"><strong>Ignored users</strong></div>').insertAfter($("#robinDesktopNotifier"));
  160.         $('#muted_users').click(function(e){
  161.             var user = $(e.target).data("usr");
  162.             if(user) _unmute_user(user);
  163.         });
  164.  
  165.         // Hook up toggles for filters
  166.         $('<div id="filter_mode">Filter: <span data-filter="">Show all</span> | <span data-filter="%">% Only</span></div>').insertAfter("#robinSendMessage").click(function(e){
  167.             var filter = $(e.target).data("filter");
  168.             if(filter === ''){
  169.                 $("#robinChatWindow").removeClass("filter_on");
  170.             }else if(filter === '%'){
  171.                 $("#robinChatWindow").addClass("filter_on");
  172.             }
  173.         });
  174.         // Auto append % when in filtered mode
  175.         $("#robinSendMessage").submit(function(){
  176.             if($("#robinChatWindow").hasClass("filter_on")){
  177.                 $(".text-counter-input").val("% " + $(".text-counter-input").val());
  178.             }
  179.         });
  180.     });
  181.  
  182.     // Add initial styles for "spam" messages
  183.     document.styleSheets[0].insertRule("#robinChatWindow.hide-spam div.robin-message.spam-hidden { display:none; }", 0);
  184.     document.styleSheets[0].insertRule("#robinChatWindow.mute-spam div.robin-message.spam-hidden { opacity:0.3; font-size:1.2em; }", 0);
  185.  
  186.     // filter styles
  187.     document.styleSheets[0].insertRule("#robinChatWindow.filter_on div.robin-message { display:none; }", 0);
  188.     document.styleSheets[0].insertRule("#robinChatWindow.filter_on div.robin-message.filter-percent { display:block; }", 0);
  189.     document.styleSheets[0].insertRule("#filter_mode span { cursor: pointer;}", 0);
  190.  
  191.     // muted user box
  192.     document.styleSheets[0].insertRule("#muted_users { font-size:1.2em; }", 0);
  193.     document.styleSheets[0].insertRule("#muted_users div { padding: 2px 0; }", 0);
  194.     document.styleSheets[0].insertRule("#muted_users strong { font-weight:bold; }", 0);
  195.  
  196.     // FIX RES nightmode (ish)
  197.     document.styleSheets[0].insertRule(".res-nightmode #robinChatWindow div.robin-message { color: #ccc; }", 0);
  198. })();
Add Comment
Please, Sign In to add comment