npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@morev/console-css

v0.0.5

Published

Style browser console messages with CSS

Downloads

1

Readme

@morev/console-css

The library to style browser console messages.

Stability of "master" branch License: MIT Last commit Release version GitHub Release Date Keywords

Why?

¯\_(ツ)_/¯

Sometimes you may want to warn users of your site like Facebook does:

An example of using styled messages in the console on the Facebook website

Perhaps you want to make a prominent message to the developers like BBC does:

An example of using styled messages in the console on the BBC website

Maybe you want to enchance some of your custom dev utilities, etc.

BUT

If you want to print BBC News logotype in the console, you need to write smth like that:

console.log('%cB%c %cB%c %cB%c %cNEWS       %c', 'font-style:bold;font-size:40px;padding:4px 16px;background:#000;color:#fff;', 'background:none', 'font-style:bold;font-size:40px;padding:4px 16px;background:#000;color:#fff;', 'background:none', 'font-style:bold;font-size:40px;padding:4px 16px;background:#000;color:#fff;', 'background:none', 'font-style:bold;font-size:40px;padding:4px 16px;background:#b80000;color:#fff;', 'background:none');

Copy-paste into you browser console and press Enter to see the result.
The code doesn't look friendly, huh?

But there is another way:

import ConsoleCSS from '@morev/console-css';

ConsoleCSS.add(`
  .letter {
    font-style: bold;
    font-size: 40px;
    padding: 4px 16px;
    background: #000000;
    color: #ffffff;
  }
  .bg-red {
    background: #ff0000;
  }
`);

ConsoleCSS.styled.log(`
  <span class="letter">B</span>
  <span class="letter">B</span>
  <span class="letter">C</span>
  <span class="letter bg-red">News</span>
`)

Installation

Using yarn

yarn add @morev/console-css

Using npm

npm install @morev/console-css

Using pnpm

pnpm add @morev/console-css

Usage

The package exports the ConsoleCSS class instance that considered singleton.
If used directly in HTML without any bundler - it's available via window.ConsoleCSS.

Important: The package only works in browser. It doesn't fall but does nothing on server side.

Default styles

By default, the package styles only well-known HTML tags:

  • <b> - bold text
  • <i> - italic text
  • <u> - underlined text
  • <s> - ~~strikethrough~~ text

Inline style

You may use inline styles:

import ConsoleCSS from '@morev/console-css';

ConsoleCSS.styled.log(`<b style="color: red;">Red bold text</b>`);

Adding custom CSS tokens

The core class has a method .add(rules: string) that accepts a CSS-like string with needed tokens.
You can use .add() method multiple times.

This input string isn't validated, so make sure your CSS syntax is correct.

import ConsoleCSS from '@morev/console-css';

ConsoleCSS.add(`
  .block { color: red; text-decoration: underline; }
`);

ConsoleCSS.styled.log('<span class="block">Red underlined text</span>');

Override the default window.console object

That might be annoying to remember about custom styled messages class, so the library provides a way to replace global window.console

import ConsoleCSS from '@morev/console-css';

// No matters before or after `.override()` call
ConsoleCSS.add(`
  .block { color: red; text-decoration: underline; }
`);

ConsoleCSS.override();

console.log('Wow, <span class="block">a styled message</span> using native `console.log`!');

// Restore the original `window.console`
ConsoleCSS.restore();

console.log('No longer <span class="block">styled</span> messages :(');