Monitor Events and Function Calls via Console

By  on  

Despite having worked on the very complex Firefox for a number of years, I'll always love plain old console.log debugging. Logging can provide an audit trail as events happen and text you can share with others. Did you know that chrome provides monitorEvents and monitor so that you can get a log each time an event occurs or function is called?

Monitor Events

Pass an element and a series of events to monitorEvents to get a console log when the event happens:

// Monitor any clicks within the window
monitorEvents(window, 'click')

// Monitor for keyup and keydown events on the body
monitorEvents(document.body, ['keyup', 'keydown'])

You can pass an array of events to listen for multiple events. The logged event represents the same event you'd see if you manually called addEventListener.

Monitor Function Calls

The monitor method allows you to listen for calls on a specific function:

// Define a sample function
function myFn() { }
// Monitor it
monitor(myFn)

// Usage 1: Basic call
myFn()
// function myFn called

// Usage 2: Arguments
myFn(1)
// function myFn called with arguments: 1

I really like that you can view the arguments provided, which is great for inspecting.

I usually opt for logpoints instead of embedding console statements in code, but monitor and monitorEvents provide an alternative to both.

Recent Features

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

Incredible Demos

  • By
    Parallax Sound Waves Animating on Scroll

    Scrolling animations are fun. They are fun to create and fun to use. If you are tired of bootstrapping you might find playing with scrolling animations as a nice juicy refreshment in your dry front-end development career. Let's have a look how to create animating...

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

Discussion

  1. Oh my chickens, this is amazing!! I can’t tell you how many times I’ve tried to poke around in the Chrome dev tools sources and elements tab trying to find way to listen for events. This is going completely change everything!!

  2. Steve

    It needs to be noted that these are not (yet) universal methods, this only works in Chromium based browsers.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!