Last active
August 29, 2015 14:19
-
-
Save syntaqx/b20fc43c70c561e2ea48 to your computer and use it in GitHub Desktop.
Save the button...
This file contains 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
(function() { | |
// What number should we save the button at? | |
var saveTheButton = 10; | |
// Socket connection | |
var sock; | |
var redditRequester = new XMLHttpRequest(); | |
// Other goodness | |
var baseTitle = document.title; | |
// Stats variables | |
var ticks = 0; | |
var lowest = 60; | |
var participants = 0; | |
var today = new Date(); | |
var started = today.getDate() + "/" | |
+ (today.getMonth() + 1) + "/" | |
+ today.getFullYear() + " @ " | |
+ today.getHours() + ":" | |
+ today.getMinutes() + ":" | |
+ today.getSeconds(); | |
// Clear the button lock, just cause. | |
$(".thebutton-container.locked").click(); | |
// Suppress the favicon | |
var $icons = $("link[rel='shortcut icon'], link[rel='icon']").prop("href", "#"); | |
// Stats box | |
var $stats = $("<div />").css({ | |
"background-color": "#fefefe", | |
"border": "1px solid #e0e0e0", | |
"margin-left": "-655px", | |
"padding": "10px", | |
"width": "175px", | |
"position": "fixed", | |
"top": "85px", | |
"left": "50%", | |
"z-index": "100000" | |
}).appendTo("body"); | |
// Clear the console for happiness | |
console.clear(); | |
// When we're good to go, set up the socket. | |
redditRequester.onreadystatechange = function() { | |
if (redditRequester.readyState !== 4) { | |
return; | |
} | |
var websocketURL; | |
if (redditRequester.status === 200) { | |
var regex = /"(wss:\/\/wss\.redditmedia\.com\/thebutton\?h=[^"]*)"/g; | |
websocketURL = regex.exec(redditRequester.responseText)[1]; | |
} | |
websocketURL = websocketURL || "wss://wss.redditmedia.com/thebutton?h=7f66bf82878e6151f7688ead7085eb63a0baff0b&e=1428621271"; | |
console.log("Connecting to: " + websocketURL); | |
sock = new WebSocket(websocketURL); | |
sock.onmessage = tick; | |
}; | |
// Use CORS proxy by lezed1 to get the Reddit homepage! | |
redditRequester.open("get", "//cors-unblocker.herokuapp.com/get?url=https%3A%2F%2Fwww.reddit.com%2Fr%2Fthebutton", true); | |
redditRequester.send(); | |
// Handle each tick | |
function tick(evt) { | |
var packet = JSON.parse(evt.data); | |
if (packet.type != "ticking") { | |
return; | |
} | |
// Save the button!! | |
if (packet.payload.seconds_left <= saveTheButton) { | |
$("#thebutton").click(); | |
} | |
// Update statistics | |
ticks++; | |
participants = packet.payload.participants_text; | |
lowest = packet.payload.seconds_left < lowest ? packet.payload.seconds_left : lowest; | |
// Update displays | |
updateStats(packet.payload.seconds_left); | |
updateTitle(packet.payload.seconds_left); | |
} | |
// Update the stats box on the page with various goodies | |
function updateStats(seconds_left) { | |
$stats.html([ | |
"<div><b>Started</b>: " + started + "</div>", | |
"<div><b>Participants</b>: " + participants + "</div>", | |
"<div><b>Ticks</b>: " + numberWithCommas(ticks) + "</div>", | |
"<div><b>Lowest time</b>: " + lowest + "</div>", | |
"<div><b>Current time</b>: " + seconds_left + "</div>", | |
"<div><b>Save The Button at</b>: " + saveTheButton + "</div>", | |
].join("\n")); | |
} | |
// Update the title of the page to display seconds left. | |
function updateTitle (seconds_left) { | |
document.title = "[" + seconds_left + "s] " + baseTitle; | |
$icons | |
.prop("type", "image/x-icon") | |
.prop("href", flairIcon(seconds_left)); | |
} | |
// Retrieve base64 encoded flair color icon | |
function flairIcon (seconds) { | |
if (seconds > 51) { | |
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA1BMVEWCAICoc3EUAAAAC0lEQVR4XmMgEQAAADAAAYFIpkQAAAAASUVORK5CYII='; | |
} | |
if (seconds > 41) { | |
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA1BMVEUAg8ey1KFBAAAAC0lEQVR4XmMgEQAAADAAAYFIpkQAAAAASUVORK5CYII='; | |
} | |
if (seconds > 31) { | |
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA1BMVEUCvgGpv1oUAAAAC0lEQVR4XmMgEQAAADAAAYFIpkQAAAAASUVORK5CYII='; | |
} | |
if (seconds > 21) { | |
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA1BMVEXl2QBYqVHHAAAAC0lEQVR4XmMgEQAAADAAAYFIpkQAAAAASUVORK5CYII='; | |
} | |
if (seconds > 11) { | |
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA1BMVEXllQAEZVHOAAAAC0lEQVR4XmMgEQAAADAAAYFIpkQAAAAASUVORK5CYII='; | |
} | |
return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAAA1BMVEXlAAAIUy+RAAAAC0lEQVR4XmMgEQAAADAAAYFIpkQAAAAASUVORK5CYII='; | |
} | |
// Adds commas to a number | |
function numberWithCommas(x) { | |
var parts = x.toString().split("."); | |
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ","); | |
return parts.join("."); | |
} | |
// Return some good'ness | |
return "Initialized."; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment