Guest User

Untitled

a guest
Apr 3rd, 2016
221
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.2
  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.             // repeating chars in line (more than 5). e.g. aaaaaaa !!!!!!!!
  29.             (/(.)\1{5,}/.test(line)) ||
  30.             // Some common messages
  31.             (/(voting will end in approximately|\[robin-grow\]|\[i spam the most used phrase\]|\[message from creator\])/.test(line)) ||
  32.             // no spaces = spam if its longer than 25 chars (dont filter links)
  33.             (line.indexOf(" ") === -1 && line.length > 25 && line.indexOf("http") === -1) ||
  34.             // repeating same word
  35.             /(\b\S+\b)\s+\b\1\b/i.test(line)
  36.         );
  37.     };
  38.  
  39.     /**
  40.      * Check if a message is from an ignored user
  41.      *
  42.      */
  43.     var is_ignored = function($usr, $ele){
  44.         // no user name, go looking for when said it
  45.         if($usr.length === 0){
  46.             while($usr.length === 0){
  47.                 $ele = $ele.prev();
  48.                 $usr = $ele.find(".robin--username");
  49.             }
  50.         }
  51.         // are they ignored?
  52.         return (ignored_users[$usr.text()]);
  53.     }
  54.  
  55.     /**
  56.      * Make links clickable
  57.      *
  58.      */
  59.     var auto_link = function($msg){
  60.         var text = $msg.html(); // read as html so stuff stays escaped
  61.         // normal links
  62.         text = text.replace(/\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim, '<a target="blank" href="$&">$&</a>');
  63.         // reddit subreddit links
  64.         text = text.replace(/ \/r\/(\w+)/gim, ' <a target="blank" href="https://reddit.com/r/$1">/r/$1</a>');
  65.         // update text
  66.         $msg.html(text);
  67.     };
  68.  
  69.     /**
  70.      * Mute a user
  71.      */
  72.     var _mute_user = function(usr){
  73.         console.log(usr);
  74.         // Add to ignore list
  75.         ignored_users[usr] = true;
  76.  
  77.         _render_muted_list();
  78.     };
  79.     /**
  80.      * un-mute a user
  81.      */
  82.     var _unmute_user = function(usr){
  83.         // Add to ignore list
  84.         delete ignored_users[usr];
  85.  
  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 + " - un-mute</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> [mute] </span>").insertAfter($usr).click(function(){
  125.                 _mute_user($usr.text());
  126.             });
  127.         }
  128.     };
  129.  
  130.     // Detect changes, are parse the new message
  131.     $("#robinChatWindow").on('DOMNodeInserted', function(e) {
  132.         if ($(e.target).is('div.robin-message')) {
  133.             // Apply changes to line
  134.             parse_line($(e.target));
  135.         }
  136.     });
  137.  
  138.     // When everything is ready
  139.     $(document).ready(function(){
  140.         // Set default spam filter type
  141.         $("#robinChatWindow").addClass("hide-spam");
  142.  
  143.         // Add checkbox to toggle "hide" behaviors
  144.         $("#robinDesktopNotifier").append("<label><input type='checkbox' checked='checked'>Hide spam completely</label>").click(function(){
  145.             if($(this).find("input").is(':checked')){
  146.                 $("#robinChatWindow").removeClass("mute-spam").addClass("hide-spam");
  147.             }else{
  148.                 $("#robinChatWindow").removeClass("hide-spam").addClass("mute-spam");
  149.             }
  150.         });
  151.  
  152.         // Add Muted list & hook up unmute logic
  153.         $('<div id="muted_users" class="robin-chat--sidebar-widget robin-chat--notification-widget"><strong>Ignored users</strong></div>').insertAfter($("#robinDesktopNotifier"));
  154.         $('#muted_users').click(function(e){
  155.             var user = $(e.target).data("usr");
  156.             if(user) _unmute_user(user);
  157.         });
  158.     });
  159.  
  160.     // Add initial styles for "spam" messages
  161.     document.styleSheets[0].insertRule("#robinChatWindow.hide-spam div.robin-message.spam-hidden { display:none; }", 0);
  162.     document.styleSheets[0].insertRule("#robinChatWindow.mute-spam div.robin-message.spam-hidden { opacity:0.3; font-size:1.2em; }", 0);
  163.  
  164.     // muted user box
  165.     document.styleSheets[0].insertRule("#muted_users { font-size:1.2em; }", 0);
  166.     document.styleSheets[0].insertRule("#muted_users div { padding: 2px 0; }", 0);
  167.     document.styleSheets[0].insertRule("#muted_users strong { font-weight:bold; }", 0);
  168.  
  169.     // FIX RES nightmode (ish)
  170.     document.styleSheets[0].insertRule(".res-nightmode #robinChatWindow div.robin-message { color: #ccc; }", 0);
  171. })();
Add Comment
Please, Sign In to add comment