Guest User

Untitled

a guest
Apr 2nd, 2016
323
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.0
  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.  
  15.     /**
  16.      * Check if a message is "spam"
  17.      *
  18.      */
  19.     var is_spam = function(line){
  20.         return (
  21.             // Hide auto vote messages
  22.             (/^voted to (grow|stay|abandon)/.test(line)) ||
  23.             // random unicode?
  24.             (/[\u0080-\uFFFF]/.test(line)) ||
  25.             // hide any auto voter messages
  26.             (/\[.*autovoter.*\]/.test(line)) ||
  27.             // repeating chars in line (more than 5). e.g. aaaaaaa !!!!!!!!
  28.             (/(.)\1{5,}/.test(line)) ||
  29.             // Some common messages
  30.             (/(voting will end in approximately|\[robin-grow\]|\[i spam the most used phrase\]|\[message from creator\])/.test(line)) ||
  31.             // no spaces = spam if its longer than 25 chars (dont filter links)
  32.             (line.indexOf(" ") === -1 && line.length > 25 && line.indexOf("http") === -1) ||
  33.             // repeating same word
  34.             /(\b\S+\b)\s+\b\1\b/i.test(line)
  35.         );
  36.     };
  37.  
  38.     /**
  39.      * Make links clickable
  40.      *
  41.      */
  42.     var auto_link = function($msg){
  43.         var text = $msg.html(); // read as html so stuff stays escaped
  44.         // normal links
  45.         text = text.replace(/\b(?:https?|ftp):\/\/[a-z0-9-+&@#\/%?=~_|!:,.;]*[a-z0-9-+&@#\/%=~_|]/gim, '<a target="blank" href="$&">$&</a>');
  46.         // reddit subreddit links
  47.         text = text.replace(/ \/r\/(\w+)/gim, ' <a target="blank" href="https://reddit.com/r/$1">/r/$1</a>');
  48.         // update text
  49.         $msg.html(text);
  50.     };
  51.  
  52.     /**
  53.      * Parse a link and apply changes
  54.      *
  55.      */
  56.     var parse_line = function($ele){
  57.         var $msg = $ele.find(".robin-message--message");
  58.         var $usr = $ele.find(".robin--username");
  59.  
  60.         var line = $msg.text().toLowerCase();
  61.  
  62.         // Spam filter
  63.         if (is_spam(line)) {
  64.             $ele.addClass("spam-hidden");
  65.         }
  66.  
  67.         // Highlight mentions
  68.         if(line.indexOf(robin_user) !== -1){
  69.             $ele.css("font-weight", "bold");
  70.         }
  71.  
  72.         // Make links clickable
  73.         if(line.indexOf("http") !== -1){
  74.             auto_link($msg);
  75.         }
  76.     };
  77.  
  78.     // Detect changes, are parse the new message
  79.     $("#robinChatWindow").on('DOMNodeInserted', function(e) {
  80.         if ($(e.target).is('div.robin-message')) {
  81.             // Apply changes to line
  82.             parse_line($(e.target));
  83.         }
  84.     });
  85.  
  86.     // When everything is ready
  87.     $(document).ready(function(){
  88.         // Set default spam filter type
  89.         $("#robinChatWindow").addClass("hide-spam");
  90.  
  91.         // Add checkbox to toggle behaviors
  92.         $("#robinDesktopNotifier").append("<label><input type='checkbox' checked='checked'>Hide spam completely</label>").click(function(){
  93.             if($(this).find("input").is(':checked')){
  94.                 $("#robinChatWindow").removeClass("mute-spam").addClass("hide-spam");
  95.             }else{
  96.                 $("#robinChatWindow").removeClass("hide-spam").addClass("mute-spam");
  97.             }
  98.         });
  99.     });
  100.  
  101.     // Add initial styles for "spam" messages
  102.     document.styleSheets[0].insertRule("#robinChatWindow.hide-spam div.robin-message.spam-hidden { display:none; }", 0);
  103.     document.styleSheets[0].insertRule("#robinChatWindow.mute-spam div.robin-message.spam-hidden { opacity:0.3; font-size:1.2em; }", 0);
  104.  
  105.     // FIX RES nightmode (ish)
  106.     document.styleSheets[0].insertRule(".res-nightmode #robinChatWindow div.robin-message { color: #ccc; }", 0);
  107. })();
Add Comment
Please, Sign In to add comment