r/javascript • • May 19 '19

Did you know you can style console.log text?

I just found it today.

With append "%c" at first of log string, you can pass style as second parameter.

Example: console.log("%c0_0","font-size: 50px;border: 5px solid #aef; border-radius: 50%;padding: 30px;color: #359")

More examples : https://stackoverflow.com/questions/7505623/colors-in-javascript-console

374 Upvotes

33 comments sorted by

119

u/Earhacker May 19 '19

My work (and many others) use this to put a recruitment link in the console. It gets zero click-throughs but whatever, it passed the time one Friday afternoon months ago.

The BBC do this too. So do Reddit but it's not styled and is obscured by an ass-ton of console errors.

14

u/ya3ya6 May 19 '19 edited May 19 '19

what about puting a not working emaill address in console (for small businesses) ? i think when developer see a message from website in the console, he will become exited , like he found something, so the message should be abit rewarding and friendly. perhaps something like :

"Hi developer! i'm john, from Piedpiper. if you're interested in us, here is my email if you want to collaborate or talk with us: [email protected] . don't hesitate to send me a message, i'll answer to it."

8

u/tribak May 19 '19

You're pure evil.

13

u/ya3ya6 May 19 '19

Haha, i didn't meant non working email. i want to say "personal email" , not "work email". i edited my comment. my English sucks :p

8

u/tribak May 19 '19

Now my comment makes no sense :'(

8

u/ya3ya6 May 19 '19

lol, I changed it back :p

14

u/tribak May 19 '19

You're pure evil.

hahaha

67

u/[deleted] May 19 '19

Facebook uses it to tell you not to be in the devtools lol

48

u/danielleiellle May 19 '19

That’s because half their activity are naive people spamming their friends because a post told them to. Of course some people would happily jump into the console and paste something if it would get them a free Aruba vacation.

9

u/[deleted] May 19 '19

Paste this to your Facebook console to see all private messages of your friends.

After pasting there's an exact same message to everyone sent via your account.. or worse, some adware filled porno link to all your personal, professional contacts, crushes, bois,etc.

4

u/danielleiellle May 19 '19

Don’t let your dreams be just dreams.

I recommend building a distracting game on top of this. Or maybe a “Which Sex and the City Character Are You?” quiz. FB likely has rate limiting and anomaly detection and you’ll likely want to scatter the HTTP requests some, so you have to keep the user put for a few.

1

u/[deleted] May 20 '19

Haven't been using Facebook for a few years. I saw something like this back in 2014. It's nice if they've added protection against something like this. Used to wreck people's reputation and shit.

7

u/[deleted] May 19 '19

I know Discord does the same thing :D

1

u/evenisto May 19 '19

I stole that idea at work.

19

u/CleverCaviar May 19 '19

Was about to post, 'AKA the recruitment feature', but you kind of beat me to it. Guardian does it also.

8

u/BrianAndersonJr May 19 '19

wow, i didn't know this was common practice, i once stumbled upon one, and i thought that was so weird and awkward for some reason :D

1

u/Doctor_Spicy May 20 '19

Discord and Apple do it too.

10

u/ya3ya6 May 19 '19

Wow, animated images(gifs) will also work, try :

console.log("%c.","padding:100px;background-size:cover;background-image:url('http://clipart-library.com/images/6Tp6Mna8c.gif')")

2

u/catwhatcat May 19 '19

console.log("%c.","padding:50px;background-size:cover;background-image:url('https://media3.giphy.com/media/5kq0GCjHA8Rwc/giphy.gif')")

2

u/ya3ya6 May 19 '19

console.log("%c.","padding:30px 510px;background-size:contain;background-repeat-y:no-repeat;background-image:url('https://media.tenor.com/images/59478acf4603b91da87453051869fa80/tenor.gif');");

console.log("%c.","padding:30px 450px;background-size:contain;background-image:url('https://media3.giphy.com/media/5kq0GCjHA8Rwc/giphy.gif');background-repeat-y:no-repeat;margin-left: 60px;");

console.log("%c.","padding:30px 510px;background-size:contain;background-repeat-y:no-repeat;background-image:url('https://media.tenor.com/images/59478acf4603b91da87453051869fa80/tenor.gif');");

19

u/davidkonrad May 19 '19

Yeah, and that is one of those "cool" completely unnecessary features with poor support, you in the end realise just causes problems.

For example, in a browser-module project (not made public yet) I used some styling in the console output to make certain kind of errors more clear. The test however is running mocha / chai in node, and instantiate the browser-module, and must pass the console object in order to make the browser-module work "serverside". Then %c becomes less attractive, because nodes' console not support this feature === %c0_0","font-size: 50px;border: 5px solid #aef; border-radius: 50%;padding: 30px;color: #359 (as example) is printed out as text in the console. Not really what you want users to see, when they evaluate your project :)

5

u/Hanlonsrazorburns May 19 '19

Well generally speaking you turn off your console logs BEFORE you deploy to production. This is very useful when you have built in debugging logs and need to separate out the information quickly.

1

u/davidkonrad May 20 '19

Yes, totally agree, if you have som kind of consoling you are using as helper for yourself / team when you are in development.

But I was merely talking about that kind of info you really need / want to pass to the end user. I actually styled console.log() because a certain kind of "error" (in production) is an error (or misconfiguration, wrong use, bad use) but not severe or crashing anything. Just a hint that "x is wrong because of y and is being skipped". A category between a real error and "useful info". And I needed also to create tests for those scenarios, and then I realised that nodes console not support (that kind of) styling. And that the browsers styling capabilities are rather new (is it possible in FF yet, without a Firebug plugin?) and so on.

1

u/ya3ya6 May 19 '19 edited May 19 '19

console.log("%c*_0","font-size: 30px;border: 5px solid #aef; border-radius: 50%;padding: 10px;color: #359"); console.log('%cYou seems suspicious, please dont consider hacking or copy this website, cuase: i p== *_*','padding:10px;color:red;background-color:#aef'); console.log("%cThat's a gun buy the way, and that means:",'color:red;background-color:#aef;padding:10px;'); console.log("%cI'll kill you",'color:red;background-color:red;padding:10px;');

1

u/[deleted] May 19 '19

Lawsuit, you said kill!

3

u/elephantengineer May 19 '19

Discovered this a while back from browsing facebook with the console open. Decent use case for it as well.

1

u/-Pad May 19 '19

What would be a useable case for this be though?

3

u/ya3ya6 May 19 '19

aside from custom styling logs, and make new log systems(like vividlog), it's attractive for developers, so some use cases are :

https://airdev.co/ : it's created by bubble (a website creator), so bubble promote itself to developers.

bbc.com : bbc promote it's recruitment to developers.

2

u/-Pad May 19 '19

Ah that’s good to know! Cheers for the insight!

2

u/examinedliving May 19 '19

I think it’s only useful if you are developing a testing platform for an enterprise app. At the level of individual and small biz, it takes too much energy for too little reward. I tried to write a shortcut JS class once, but the privacy of Console methods made it too much of a pain. Might be different now though.