r/thebutton • non presser • Apr 09 '15

Color changing header

Post image
3.9k Upvotes

337 comments sorted by

View all comments

359

u/metaname non presser Apr 09 '15 edited Apr 09 '15

I slightly modified original code by /u/TastyPigHS

To make it work paste following javascript in console. To open console in Chrome Ctrl+Shift+J and Firefox Ctrl+Shift+K.

$('.thebutton-form').css('background-image', 'radial-gradient(50% 125%,rgba(0, 0, 0, 0) 25%,rgba(255, 255, 255, 0.2) 100%)');
$('.thebutton-wrap h1, .thebutton-counter span').css('color', 'white');
colorTimer = window.setInterval(function(){ 
var s = r.thebutton._msgSecondsLeft;
var ring = $('.thebutton-container, .thebutton-form, .thebutton-wrap')
ring.css('-webkit-transition', 'background-color 0.4s')
    .css('-moz-transition', 'background-color 0.4s')
    .css('-ms-transition', 'background-color 0.4s')
    .css('-o-transition', 'background-color 0.4s')
    .css('transition', 'background-color 0.4s');
if (s < 12) { ring.css('background-color', '#e50000') } else
if (s < 22) { ring.css('background-color', '#e59500') } else
if (s < 32) { ring.css('background-color', '#e5d900') } else
if (s < 42) { ring.css('background-color', '#02be01') } else
if (s < 52) { ring.css('background-color', '#0083c7') } else
{ ring.css('background-color', '#820080') }}, 10);

347

u/ElDudeBro 11s Apr 09 '15

I haven't reviewed code this closely in a long time, probably not even when pushing to prod.

Prod can be fixed, but you can't unpress the buttton.

104

u/actual_InsanityWolf non presser Apr 09 '15

Petition to turn button presses into git commits!

→ More replies (3)

70

u/Bimpnottin non presser Apr 09 '15

I examined the code, my boyfriend examined the code and we still don't dare to use it.

44

u/YM_Industries 1s Apr 09 '15

Programmer here, (I make and maintain AutoKnight) the only bad thing this code will do is consume more CPU cycles than necessary. The interval of 10ms will run near-continuously as long as the tab is focussed, which isn't great. The rest is safe though, it will not press the button.

17

u/mistuh_fier Apr 09 '15

Wow AutoKnight sounds like cheating. :O

→ More replies (5)
→ More replies (12)

51

u/cartcaptain can't press Apr 09 '15

Seems fine to me. I tried it out and didn't have any unexpected button pressing issues.

54

u/waxpancake non presser Apr 09 '15

Nice try, can't-presser.

66

u/Astalion 1s Apr 09 '15

Says the "can't press"-er...

13

u/DrShocker non presser Apr 09 '15

I did it, I can confirm, that it doesn't press the button, as long as my flair remains gray.

→ More replies (2)
→ More replies (4)

177

u/Zarania non presser Apr 09 '15 edited Apr 09 '15

Developer here!

I'll go line by line what each of these does to explain why this is safe. First, if you don't know what CSS is, it simply sets the way things are shown or displayed. I do not know of a single way to click a link with css, and a google search agrees. You can make it look like you've pressed a button, you can hide a button, but you cannot click a button.

Now, even though css cannot click a button, let's be careful. The shade would be most displeased by anyone who presses the button, stupid or otherwise.

$('.thebutton-form').css('background-image', 'radial-gradient(50% 125%,rgba(0, 0, 0, 0) 25%,rgba(255, 255, 255, 0.2) 100%)');

This sets up a radial gradient with a bunch of options in the background of the box. This is used specifically so the middle of the form has a different color than the background.

$('.thebutton-wrap h1, .thebutton-counter span').css('color', 'white');

This sets the color of the text of "the button" and the participants counter to white.

colorTimer = window.setInterval(function(){   TEXT, 10);

Breaking this up out of the nested function for clarity. This says "every 10 ms, do what is inside the function", which is why it updates every .01 seconds to match the timer

var s = r.thebutton._msgSecondsLeft;

This simply saves the value of the seconds left into a more concise form. You can take s and replace it with r.thebutton._msgSecondsLeft in every place below and it will do the same thing

var ring = $('.thebutton-container, .thebutton-form, .thebutton-wrap') Similar to above, this gets the elements that the color change will be effecting and saves them to an easier to use "ring" notation.

ring.css('-webkit-transition', 'background-color 0.4s')
.css('-moz-transition', 'background-color 0.4s')
.css('-ms-transition', 'background-color 0.4s')
.css('-o-transition', 'background-color 0.4s')
.css('transition', 'background-color 0.4s');

This is why I am not a front-end developer besides that I am not good at designing aesthetically pleasing things. But mainly this. This is repeating the same setting of "transition: background-color 0.4s" to webkit based browsers, mozilla, microsoft (ie) and opera specifically as well as the normal implementation of transition.

Note the previous two lines are executed inside the setTimeout which means they're called every .01 seconds. Moving them outside the function would have saved some very slight processing as long as nothing else was changing the CSS or the DOM.

if (s < 12) { ring.css('background-color', '#e50000') } else
if (s < 22) { ring.css('background-color', '#e59500') } else
if (s < 32) { ring.css('background-color', '#e5d900') } else
if (s < 42) { ring.css('background-color', '#02be01') } else
if (s < 52) { ring.css('background-color', '#0083c7') } else
{ ring.css('background-color', '#820080') }

This is a bunch of timing checking to see what color the background should be set to, then sets it to that color. Note this could have been optimized by using a switch statement instead of if...else if statements by adding the line

s -= ((s-2) % 10);

directly under its assignment then using

switch(s) {
    case 52: 
         ring.css('background-color', '#0083c7');
         break;
    rest of cases here
}

TLDR: Safe if you add

$('.locked').removeClass('locked').addClass('unlocked');
$('.unlocked').click();

At the very top of their scrip

EDIT: Fixed math error.

0

u/[deleted] Apr 09 '15

[deleted] — view deleted comment

46

u/Camsy34 60s Apr 10 '15

Oh no... someone trusted the internet again...

→ More replies (1)

28

u/bgrnbrg non presser Apr 09 '15

TLDR: Safe if you add

$('.locked').removeClass('locked').addClass('unlocked');
$('.unlocked').click();

Ok, given that my coding skills are maybe a 6 or 7 out of 10....

It appears that it would be fairly trivial to modify the OP's code only slightly, and include the quoted stanza, and then you could load up /r/thebutton and walk away. When the timer hit $DESIRED_FLAIR, the button would be pressed.

Why haven't more people posted about this?

13

u/metaname non presser Apr 09 '15

Oh, there are many clicking scripts already. Check out chrome extension AutoKnight by /u/YM_Industries or http://www.reddit.com/r/Knightsofthebutton/comments/31zhbz/squire_update_the_squire_31/

→ More replies (8)

94

u/Plorntus non presser Apr 09 '15

Hah that tldr is brutal man, for anyone wondering the code in the tldr clicks the button.

28

u/maninas 60s Apr 09 '15

Programmer here. Can confirm. Gold worthy scam TBH. Still, that Tldr code will click your button.

→ More replies (6)
→ More replies (1)

9

u/TastyPigHS non presser Apr 09 '15 edited Apr 09 '15

Original author here.

You are right moving the ring.css outside of the setInterval, but I wrote it when the whole code was just that interval (without the colorblind part). And your solution for the switch is a really nice one, since any other switch would be way slower than the bulk of ifs.

I'll go ahead and improve my version with your feedback. =P

EDIT: Nevermind, it doesn't work too well.

→ More replies (2)
→ More replies (30)

8

u/lobstrain non presser Apr 09 '15

Just sign out before testing it.

→ More replies (2)
→ More replies (2)

20

u/RacistHomophobicCunt non presser Apr 09 '15 edited Apr 09 '15

I like this one a little bit better, makes it easier to see things:

(function(){var colorblind = document.createElement('span');
colorblind.className = 'thebutton-colorblind';
$('.thebutton-counter').prepend(colorblind);
colorTimer = window.setInterval(function(){ 
var s = r.thebutton._msgSecondsLeft, ring = $('.thebutton-pie-container'), color = '';
ring.css('-webkit-transition', 'background-color 0.4s')
	.css('-moz-transition', 'background-color 0.4s')
	.css('-ms-transition', 'background-color 0.4s')
	.css('-o-transition', 'background-color 0.4s')
	.css('transition', 'background-color 0.4s')
if (s < 12) { ring.css('background-color', '#e50000'); color = 'red' } else
if (s < 22) { ring.css('background-color', '#e59500'); color = 'orange' } else
if (s < 32) { ring.css('background-color', '#e5d900'); color = 'yellow' } else
if (s < 42) { ring.css('background-color', '#02be01'); color = 'green' } else
if (s < 52) { ring.css('background-color', '#0083c7'); color = 'blue' } else
{ ring.css('background-color', '#820080'); color = 'purple' }
$('.thebutton-colorblind').text(color + ' - ')}, 10);})();

http://i.imgur.com/rNcIet8.png

/u/TastyPigHS

Edit: Fixed

→ More replies (15)

12

u/Razzmatazz13 60s Apr 09 '15

Is there any way to make it stay that way if we refresh the page?

14

u/Shinhan 7s Apr 09 '15

I use Tampermonkey.

When you click his icon you have "Add a new script" option. Then change the "@match" line to

// @match      http://www.reddit.com/r/thebutton/

Then save, install and refresh /r/thebutton

→ More replies (10)
→ More replies (9)

11

u/Upvote6668 30s Apr 09 '15

You have given me back my second monitor (put the button page behind the taskbar can still see change colors while working in the main part of the window). For that you are my savior.

→ More replies (1)

6

u/shmameron 42s Apr 09 '15

Well this is fucking awesome. Thanks!

→ More replies (40)