Scala.js: CSS styled console logs

Viewed 395

With javascript I can print styled logs on the console like e.g. this:

console.log('%c Oh my heavens! ', 'background: #222; color: #bada55');

which will result in:

styled console log

Is there any way I can do the same using Scala.js? The standard println does not seem to have this functionality.

*Edit: Of course one could always use scala.scalajs.js.eval for such cases but I'd prefer a more "scala native" way:

js.eval("""
 console.log('%c Oh my heavens! ', 'background: #222; color: #bada55');
 """)
2 Answers

scala-js-dom project provides type safe bindings for this and most other browser features:

import org.scalajs.dom

dom.console.log("%c Oh my heavens! ", "background: #222; color: #bada55")

No need to use js.Dynamic

You can use console.log using, for example, the dynamically typed API:

js.Dynamic.global.console.log("%c Oh my heavens!",
    "background: #222; color: #bada55")
Related