-
-
Save CptAsgard/488a6bd0de58724e3132f6da0a8c9476 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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.0 | |
// @author LeoVerto, Wiiplay123, Getnamo, CptAsgard | |
// @match https://www.reddit.com/robin | |
// @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 = ["/me Time left: 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