September 5, 2018
Konami Code - Easter egg code in JavaScript
The Konami Code (コナミコマンド) is a cheat code that appears in many Konami video games. The code is also present as an Easter egg on a number of websites.
UP, UP, DOWN, DOWN, LEFT, RIGHT, LEFT, RIGHT, B, A
The main concerns when implementing such an Easter egg into a website are:
- Don't run the Easter egg unintentionally
- Don't break the site
What we need to implement the Konami Code is an event listener that captures the key events and checks if the combination represents the specified code.
For this purpose I used an array. I chose an array over a concated string approach for ease of use and code readability. The first iteration of the code just added to the array. And added, and added, ... This is not good, as it fills the memory after a while.
65 65,66 65,66,67 65,66,67,49 65,66,67,49,50 65,66,67,49,50,51 65,66,67,49,50,51,38 65,66,67,49,50,51,38,38 65,66,67,49,50,51,38,38,40 65,66,67,49,50,51,38,38,40,40 65,66,67,49,50,51,38,38,40,40,37 65,66,67,49,50,51,38,38,40,40,37,39 65,66,67,49,50,51,38,38,40,40,37,39,37 65,66,67,49,50,51,38,38,40,40,37,39,37,39 65,66,67,49,50,51,38,38,40,40,37,39,37,39,66 65,66,67,49,50,51,38,38,40,40,37,39,37,39,66,65
The next iteration fixed this bug by always removing the first element of the array which captures all the key events.
65 65,66 65,66,67 65,66,67,49 65,66,67,49,50 65,66,67,49,50,51 65,66,67,49,50,51,38 65,66,67,49,50,51,38,38 65,66,67,49,50,51,38,38,40 65,66,67,49,50,51,38,38,40,40 66,67,49,50,51,38,38,40,40,37 67,49,50,51,38,38,40,40,37,39 49,50,51,38,38,40,40,37,39,37 50,51,38,38,40,40,37,39,37,39 51,38,38,40,40,37,39,37,39,66 38,38,40,40,37,39,37,39,66,65
A further improvement unbinds the keydown event listener once the Konami Code was fired.
The finished code:
$(document).ready(function(){
var konamiUserKeyCodeStorage = [];
var konamiCode = [38,38,40,40,37,39,37,39,66,65];
$(document).keydown(function(e){
konamiUserKeyCodeStorage.push(e.keyCode); //log the entered key code
if(konamiUserKeyCodeStorage.toString() == konamiCode.toString()){
$(document).unbind('keydown',arguments.callee); //destruct
alert('Easter egg');
}
if(konamiUserKeyCodeStorage.length >= konamiCode.length){
konamiUserKeyCodeStorage.shift(); //prevent filling the storage
}
});
});
Dependencies: jQuery
Tags: javascript, easter egg, konami
« back
About the author
human, software engineer, tech enthusiast, security researcher
E-Mail: blog@cipher.digital
-----BEGIN PGP PUBLIC KEY BLOCK----- Version: OpenPGP (RSA-2048) xsBNBFjAYL4BCACsmBS6zE+0b7mZVtQhmfnRn3+IIQfT6WlE6izM39Q42yxj Hf2GOZU15Xc1x5RM9ZZx7HnMyTQWJMkwCzEba4Ju8dbn8gbFzLFp+mXAWQVJ NOhsLvt58X/k1nQ3HYaYAbJPFE4k89zlFUjBG+a1Qs0kNg5RkaSTcE4iV6L4 749LYRba1VFK1p3eIFmIh1zQnzwFY1WYJjvXHURZel8MA0BJTkmfOW4MRHZL lz8mjmTeWoRyxismRDprEtGynK7oIb3qUKAIr5MtoyESHBhVR+EpWHP0+06T IfOsrsp8maNztXRQRKZxHzNZj/ayGpxBGO19e0/6jNpWGI5Nflwo/oHbABEB AAHNI0RpZ2l0YWxDaXBoZXIgPGJsb2dAY2lwaGVyLmRpZ2l0YWw+wsByBBAB CAAmBQJYwGDQBgsJCAcDAgkQMsB3T2XG/XYEFQgCCgMWAgECGwMCHgEAAOzo B/4obbCU7u4f8kXQiaqAhSCjjyR5ZzdApPCh9i9XJ0qGTULTUuBrin1JDXSj HoiByL2mYh92+I8S+YMWLMiTQzl9O4wx+A0eDnfwbs5jKJSQt5Pc8NMlwWKU pG+R7escZ7le/qJYMgGPUWzFhgaKi8jueMW/NJSmPu/Tu4V9nhyxG9oaV3oP rF+W0bekP84tDJ477clRSSK9ZzjMbLL1PWuNmCd8Gsnd3fyP1WcadIMDrnBB sb+7AQ9eTywJ4Yzogh+cWjwy+TkkfEyCJ0X2n5WPURWc0YOFVqhcV4TYDR4v CHSbh+r7OVKIjqdQKDJwAUCYeSkePbxJYmzRoaTd2+RgzsBNBFjAYL4BCADE i8WrXxZWn42DlKDpnwTFBo/8asY4SJ22Zagkoj3cV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