From the mind of Kon Pik…

Hopeless without the internet

UPDATED: Coding Contest: JavaScript Konami Code

Jump to Update from 8/8/2012

Everyone is familiar with the Konami Code – so as I was reading an older article which brought up the Konami Code pranks done by ESPN and Facebook, I thought to myself – what is the smallest amount of JavaScript you need to attach the Konami Code to any site?

For simplicity, I used jQuery and therefore for this contest you will as well.

For convenience, the Konami Code is replicated here: ↑↑↓↓←→←→BA

Contest Rules & Clarifications

  1. Konami Code needs to be executed on site over and over again so once it executes once you should allow Konami Code to be entered in multiple times
  2. For minifications, the Closure Compiler will be used on Advanced setting – you should test your minified sources to ensure it works (especially if there are errors/warnings!)
  3. You must submit a jsFiddle of your unminified source and provide the length of the minified source – and I will verify
  4. The following baseline jsFiddle should be used as your starting point to fork – it has the initial event hooks – if someone has a good reason why this isn’t the best way, then I’ll be happy to change this rule
  5. The code must run as expected on IE9, Chrome, and FireFox (latest up-to-date stable versions)
  6. Note that the button sequence is not SHIFT+a or SHIFT+b key – it is simply the “A” or “B” key
  7. The code must be not discriminate between A/a and B/b (e.g. if caps lock is pressed it should not make a difference)
I will post my solution soon, as I don’t want it to influence anyone else’s creativity. However, I can say that so far I have been able to get my solution down to Compiled Size: 160 bytes (147 bytes gzipped) after running it through the Closure Compiler on Advanced.
If there are any questions or clarifications needed, do not hesitate to post them here.

 

Update 8/8

Here’s the code chunk, as promised
(function($) {
    $['fn']['konami'] = function(callback) {
        $.konamiCount = 0;
        $(window)['keydown'](function(e) {
            var code = "&&((%'%'BA";
            $.konamiCount = code[$.konamiCount].charCodeAt(0) == e.keyCode ? $.konamiCount + 1 : 0;

            if ($.konamiCount > 9)
                callback();
            $.konamiCount %= 10;
        });
    };
})(jQuery);

And minified we get

var a=jQuery;a.fn.konami=function(b){a.a=0;a(window).keydown(function(c){a.a="&&((%'%'BA"[a.a].charCodeAt(0)==c.keyCode?a.a+1:0;9



Perhaps I’ll update with an explanation later.

Tags: , , , , , , , , ,