Skip to content

Instantly share code, notes, and snippets.

@CptAsgard
Created April 2, 2016 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save CptAsgard/ec4c6ca92c59496aa4823e20baf7dfc3 to your computer and use it in GitHub Desktop.
Save CptAsgard/ec4c6ca92c59496aa4823e20baf7dfc3 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Robin Time Left Printer
// @description Violently Butchered Robin Assistant
// @namespace com.github.cptasgard
// @include https://www.reddit.com/robin/
// @include https://www.reddit.com/robin
// @version 1.9
// @author LeoVerto, Wiiplay123, Getnamo, CptAsgard
// @grant none
// ==/UserScript==
function sendMessage(msg) {
$(".text-counter-input")[0].value = msg;
$(".text-counter-input")[0].nextSibling.click();
}
function addMins(date, mins) {
var newDateObj = new Date(date.getTime() + mins * 60000);
return newDateObj;
}
function timeLeft() { // mostly from /u/Yantrio
var remainingMessageArray = $(".robin-message--message:contains('approx')");
if (remainingMessageArray.length == 0) {
//This shouldn't happen
return "Unknown";
}
var message = remainingMessageArray.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]);
var fraction = Math.floor((endTime - new Date()) / 60 / 1000 * 10) / 10;
var min = Math.floor(fraction);
var sec = Math.round((fraction - min) * 60);
return min + " m " + sec + " s";
} catch (e) {
return "Fail";
}
//grab the timestamp from the first post and then calc the difference using the estimate it gives you on boot
}
function checkCommand(message) {
if( message.toLowerCase() == "!timeleft" ) {
sendMessage( "/me Time left before round end (roughly): " + timeLeft() );
}
}
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
var added = mutation.addedNodes[0];
// Filters all new messages
if ($(added).hasClass("robin-message")) {
var msg = added;
var msgText = $(msg).find(".robin-message--message").text();
//console.log(msgText)
checkCommand(msgText);
}
});
});
observer.observe($("#robinChatMessageList").get(0), {
childList: true
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment