Skip to content

Instantly share code, notes, and snippets.

@rlemon
Last active April 2, 2016 04:01
Show Gist options
  • Save rlemon/cc13cb4c31861e5d5ba2a92bfc920aeb to your computer and use it in GitHub Desktop.
Save rlemon/cc13cb4c31861e5d5ba2a92bfc920aeb to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Robin Grow
// @namespace http://tampermonkey.net/
// @version 1.32XYZ
// @description Try to take over the world!
// @author /u/mvartan - modified by /u/rlemon
// @include https://www.reddit.com/robin*
// ==/UserScript==
function exec(fn) {
var script = document.createElement('script');
script.setAttribute("type", "application/javascript");
script.textContent = '(' + fn + ')();';
document.body.appendChild(script); // run the script
}
exec(function() {
jQuery = jQuery || window.jQuery || window.$ || $;
var me = jQuery('.user a').text();
function addMins(date, mins) {
var newDateObj = new Date(date.getTime() + mins * 60000);
return newDateObj;
}
function howLongLeft() { // mostly from /u/Yantrio
var remainingMessageContainer = jQuery(".robin--user-class--system:contains('approx')");
if (remainingMessageContainer.length == 0) {
// for cases where it says "soon" instead of a time on page load
return 0;
}
var message = jQuery(".robin-message--message", remainingMessageContainer).text();
var time = new Date(jQuery(".robin--user-class--system:contains('approx') .robin-message--timestamp").attr("datetime"));
try {
var endTime = addMins(time, message.match(/\d+/)[0]);
return Math.floor((endTime - new Date()) / 60 / 1000 * 10) / 10;
} catch (e) {
return 0;
}
//grab the timestamp from the first post and then calc the difference using the estimate it gives you on boot
}
jQuery("#robinVoteWidget").prepend("<div class='addon'><div class='timeleft robin-chat--vote' style='font-weight:bold;'></div></div>");
jQuery('.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>");
jQuery('#robinVoteWidget .robin-chat--vote').css('padding', '5px');
var timeStarted = new Date();
var name = jQuery(".robin-chat--room-name").text();
function update() {
jQuery(".timeleft").text(howLongLeft() + " minutes remaining");
var list = {}
jQuery.get("/robin/", function(a) {
var start = "{" + a.substring(a.indexOf("\"robin_user_list\": ["));
var end = start.substring(0, start.indexOf("}]") + 2) + "}";
list = JSON.parse(end).robin_user_list;
var increaseCount = list.filter(function(voter) {
return voter.vote === "INCREASE"
}).length;
var abandonCount = list.filter(function(voter) {
return voter.vote === "ABANDON"
}).length;
var novoteCount = list.filter(function(voter) {
return voter.vote === "NOVOTE"
}).length;
var continueCount = list.filter(function(voter) {
return voter.vote === "CONTINUE"
}).length;
jQuery('#robinVoteWidget .robin--vote-class--increase .robin-chat--vote-label').html('grow<br>(' + increaseCount + ')');
jQuery('#robinVoteWidget .robin--vote-class--abandon .robin-chat--vote-label').html('abandon<br>(' + abandonCount + ')');
jQuery('#robinVoteWidget .robin--vote-class--novote .robin-chat--vote-label').html('no vote<br>(' + novoteCount + ')');
jQuery('#robinVoteWidget .robin--vote-class--continue .robin-chat--vote-label').html('stay<br>(' + continueCount + ')');
});
var lastChatString = jQuery(".robin-message--timestamp").last().attr("datetime");
var timeSinceLastChat = new Date() - (new Date(lastChatString));
var now = new Date();
if (timeSinceLastChat !== undefined && (timeSinceLastChat > 60000 && now - timeStarted > 60000)) {
window.location.reload(); // reload if we haven't seen any activity in a minute.
}
if (jQuery(".robin-message--message:contains('that is already your vote')").length === 0) {
jQuery(".text-counter-input").val("/vote grow").submit();
}
// Try to join if not currently in a chat
if (jQuery("#joinRobinContainer").length) {
jQuery("#joinRobinContainer").click();
setTimeout(function() {
jQuery("#joinRobin").click();
}, 1000);
}
setTimeout(update, 3000);
}
// hash string so finding spam doesn't take up too much memory
function hashString(str) {
var hash = 0;
if (str == 0) return hash;
for (i = 0; i < str.length; i++) {
char = str.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash; // Convert to 32bit integer
}
return hash;
}
// Searches through all messages to find and hide spam
var spamCounts = {};
function findAndHideSpam() {
jQuery('.robin-message--message:not(.addon--hide)').each(function() {
// skips over ones that have been hidden during this run of the loop
var hash = hashString(jQuery(this).text());
var user = jQuery('.robin-message--from', jQuery(this).closest('.robin-message')).text();
if (!(user in spamCounts)) {
spamCounts[user] = {};
}
if (hash in spamCounts[user]) {
spamCounts[user][hash].count++;
spamCounts[user][hash].elements.push(this);
} else {
spamCounts[user][hash] = {
count: 1,
text: this.textContent,
elements: [this]
};
}
});
jQuery.each(spamCounts, function(user, messages) {
jQuery.each(messages, function(hash, message) {
if (message.count >= 2) {
jQuery.each(message.elements, function(index, element) {
jQuery(element).closest('.robin-message').remove();
});
} else {
message.count = 0;
}
message.elements = [];
});
});
}
function removeSpam() {
jQuery(".robin-message").filter(function(num, message) {
var txt = jQuery(message).find(".robin-message--message").text();
return txt.indexOf("[") === 0 || txt.indexOf("voted to") === 0 || txt.indexOf("Autovoter") > -1 || isDoubleByte(txt)|| isTooLong(txt); // starts with a [ or has "Autovoter"
}).remove();
}
function isDoubleByte(str) {
for (var i = 0, n = str.length; i < n; i++) {
if (str.charCodeAt( i ) > 255) { return true; }
}
return false;
}
function isTooLong(str) {
return str.split(' ').some(word => word.length > 100);
}
new MutationObserver(records => records.forEach(record => parse(record.addedNodes))).observe(document.body, {
childList: true,
subtree: true
});
function mention(node) {
if( jQuery(node).text().indexOf(me) >= 0 ) {
jQuery(node).css({background:'rgba(255, 0, 0, 0.3)', color: '#242424'});
}
}
function parse(node) {
if (jQuery(node).is('.robin--message-class--message')) {
mention(node);
findAndHideSpam();
removeSpam();
}
}
update()
});
@dashed
Copy link

dashed commented Apr 2, 2016

Needs whitelist system

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment