Skip to content

Instantly share code, notes, and snippets.

@elis
Forked from CptAsgard/timeleftprinter.js
Last active April 2, 2016 20:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elis/948b2a6cde75fd4df635b3e87fe620ea to your computer and use it in GitHub Desktop.
Save elis/948b2a6cde75fd4df635b3e87fe620ea 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" ) {
var tl = timeLeft();
var message = ["T", tl[0] === '-' ? 'plus ' + tl.substr(1) : 'minus ' + tl, 'and counting.'].join(' ');
sendMessage(message);
}
}
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