Advertisement
Guest User

/r/orderofstay Invite Script + Anti-Spam + Auto-Vote

a guest
Apr 1st, 2016
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Robin Stay
  3. // @namespace    http://tampermonkey.net/
  4. // @version      1.3
  5. // @description  Try to take over the world!
  6. // @author       /u/mvartan (modified by /u/gibs, subreddit plug added by /u/brilliantlyInsane)
  7. // @include      https://www.reddit.com/robin*
  8. // @updateURL    https://github.com/corinth/robin-stay/raw/master/robin.user.js
  9. // @grant   GM_getValue
  10. // @grant   GM_setValue
  11. // ==/UserScript==
  12.  
  13. function addMins(date,mins) {
  14.     var newDateObj = new Date(date.getTime() + mins*60000);
  15.     return newDateObj;
  16. }
  17.  
  18. function howLongLeft() { // mostly from /u/Yantrio
  19.     var remainingMessageContainer = $(".robin--user-class--system:contains('approx')");
  20.     if(remainingMessageContainer.length == 0) {
  21.         // for cases where it says "soon" instead of a time on page load
  22.         return 0;
  23.     }
  24.     var message = $(".robin-message--message", remainingMessageContainer).text();
  25.     var time = new Date($(".robin--user-class--system:contains('approx') .robin-message--timestamp").attr("datetime"));
  26.     try {
  27.         var endTime = addMins(time,message.match(/\d+/)[0]);
  28.         return Math.floor((endTime - new Date())/60/1000*10)/10;
  29.     } catch(e){
  30.         return 0;
  31.     }
  32.  
  33.     //grab the timestamp from the first post and then calc the difference using the estimate it gives you on boot
  34. }
  35.  
  36. $("#robinVoteWidget").prepend("<div class='addon'><div class='timeleft robin-chat--vote' style='font-weight:bold;'></div></div>");
  37. $('.robin-chat--buttons').prepend("<div class='robin-chat--vote robin--vote-class--novote'><span class='robin--icon'></span><div class='robin-chat--vote-label'></div></div>");
  38. $('#robinVoteWidget .robin-chat--vote').css('padding', '5px');
  39.  
  40. var timeStarted = new Date();
  41. var name = $(".robin-chat--room-name").text();
  42.  
  43. function update() {
  44.     $(".timeleft").text(howLongLeft()+" minutes remaining");
  45.  
  46.     var list = {}
  47.     $.get("/robin/",function(a){
  48.           var start = "{"+a.substring(a.indexOf("\"robin_user_list\": ["));
  49.           var end = start.substring(0,start.indexOf("}]")+2)+"}";
  50.           list = JSON.parse(end).robin_user_list;
  51.           var increaseCount = list.filter(function(voter){return voter.vote === "INCREASE"}).length;
  52.           var abandonCount = list.filter(function(voter){return voter.vote === "ABANDON"}).length;
  53.           var novoteCount = list.filter(function(voter){return voter.vote === "NOVOTE"}).length;
  54.           var continueCount = list.filter(function(voter){return voter.vote === "CONTINUE"}).length;
  55.           $('#robinVoteWidget .robin--vote-class--increase .robin-chat--vote-label').html('grow<br>('+increaseCount+')');
  56.           $('#robinVoteWidget .robin--vote-class--abandon .robin-chat--vote-label').html('abandon<br>('+abandonCount+')');
  57.           $('#robinVoteWidget .robin--vote-class--novote .robin-chat--vote-label').html('no vote<br>('+novoteCount+')');
  58.           $('#robinVoteWidget .robin--vote-class--continue .robin-chat--vote-label').html('stay<br>('+continueCount+')');
  59.           });
  60.     var lastChatString = $(".robin-message--timestamp").last().attr("datetime");
  61.     var timeSinceLastChat = new Date() - (new Date(lastChatString));
  62.     var now = new Date();
  63.     if(timeSinceLastChat !== undefined && (timeSinceLastChat > 60000 && now-timeStarted > 60000)) {
  64.         window.location.reload(); // reload if we haven't seen any activity in a minute.
  65.     }
  66.     if($(".robin-message--message:contains('that is already your vote')").length === 0) {
  67.         $(".text-counter-input").val("/vote stay").submit();
  68.         $(".text-counter-input").val("Be a STAYer, and enjoy a friendly, pieceful community. /r/orderofstay").submit();
  69.     }
  70.  
  71.  
  72.     // Try to join if not currently in a chat
  73.     if ($("#joinRobinContainer").length) {
  74.         $("#joinRobinContainer").click();
  75.         setTimeout(function(){
  76.                    $("#joinRobin").click();
  77.                    }, 1000);
  78.     }
  79. }
  80.  
  81. // hash string so finding spam doesn't take up too much memory
  82. function hashString(str) {
  83.     var hash = 0;
  84.  
  85.     if (str == 0) return hash;
  86.  
  87.     for (i = 0; i < str.length; i++) {
  88.         char = str.charCodeAt(i);
  89.         hash = ((hash<<5)-hash)+char;
  90.         hash = hash & hash; // Convert to 32bit integer
  91.     }
  92.  
  93.     return hash;
  94. }
  95.  
  96. // Searches through all messages to find and hide spam
  97. var spamCounts = {};
  98.  
  99. function findAndHideSpam() {
  100.     $('.robin-message--message:not(.addon--hide)').each(function() {
  101.  
  102.         // skips over ones that have been hidden during this run of the loop
  103.         var hash = hashString($(this).text());
  104.         var user = $('.robin-message--from', $(this).closest('.robin-message')).text();
  105.  
  106.         if (!(user in spamCounts)) {
  107.             spamCounts[user] = {};
  108.         }
  109.  
  110.         if (hash in spamCounts[user]) {
  111.             spamCounts[user][hash].count++;
  112.             spamCounts[user][hash].elements.push(this);
  113.         } else {
  114.             spamCounts[user][hash] = {
  115.                 count: 1,
  116.                 text: $(this).text(),
  117.                 elements: [this]
  118.             };
  119.         }
  120.     });
  121.    
  122.     $.each(spamCounts, function(user, messages) {
  123.            $.each(messages, function(hash, message) {
  124.                   if (message.count >= 3) {
  125.                   $.each(message.elements, function(index, element) {
  126.                          $(element).closest('.robin-message').addClass('addon--hide').hide();
  127.                          });
  128.                   } else {
  129.                   message.count = 0;
  130.                   }
  131.                  
  132.                   message.elements = [];
  133.                   });
  134.            });
  135. }
  136.  
  137. function removeSpam() {
  138.     $(".robin-message").filter(function(num,message){
  139.         return $(message).find(".robin-message--message").text().indexOf("[") === 0
  140.         || $(message).find(".robin-message--message").text().indexOf("Autovoter") > -1; // starts with a [ or has "Autovoter"
  141.     }).hide();
  142. }
  143.  
  144. setInterval(findAndHideSpam, 1000);
  145. setInterval(removeSpam, 1000);
  146. setInterval(update, 10000);
  147. update();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement