Fork me on GitHub

Egg.js

Egg.js is a simple JS library that has no prerequisites and allows you to easily add web easter eggs by watching the user's key strokes.

Type the Konami code (up, up, down, down, left, right, left, right, b, a) to test it out.

Example

It's really easy to use. Just include the egg.js file on the page...

<script type="text/javascript" src="https://cdn.rawgit.com/mikeflynn/egg.js/master/egg.min.js"></script>

...then use the addCode() function to add in your easter eggs. You don't need to know the special Javascript key codes, as you can use plain English to define your egg (but you can use key codes if you want to). You'll also need a function to trigger when it happens, and then an optional set of metadata, which can be anything from a string to an object.

var egg = new Egg();
egg
  .addCode("up,up,down,down,left,right,left,right,b,a", function() {
    jQuery('#egggif').fadeIn(500, function() {
      window.setTimeout(function() { jQuery('#egggif').hide(); }, 5000);
    });
  })
  .addHook(function(){
    console.log("Hook called for: " + this.activeEgg.keys);
    console.log(this.activeEgg.metadata);
  }).listen();
    

You can also add a hook, as shown above using addHook(), that will run after any egg code is triggered. You could use it to fire a Google Analytics event or send out a tweet that someone finally found your easter egg. Hooks get access to the whole Egg.js object so you can pull information about the easter egg that fired via this.activeEgg

Even more simply, you can just add an easter egg via the constructor:

var egg = new Egg("up,up,down,down,left,right,left,right,b,a", function() {
  jQuery('#egggif').fadeIn(500, function() {
    window.setTimeout(function() { jQuery('#egggif').hide(); }, 5000);
  });
}).listen();

Why?

I put an easter egg in pretty much everything I make and after copying the same basic code over and over again I figured I should make it in to a simple library for my own use.

Credits

Created by Mike Flynn (@thatmikeflynn) and Rob McVey (@negative_sleep)