Skip to content

Instantly share code, notes, and snippets.

@jsguy
Created October 4, 2013 07:09
Show Gist options
  • Save jsguy/6822077 to your computer and use it in GitHub Desktop.
Save jsguy/6822077 to your computer and use it in GitHub Desktop.
jQuery konamify plugin... why not, it's friday.
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
<p>
U, U, D, D, L, R, L, R, B, A
</p>
<script>
;(function( $ ) {
$.konamify = function( callback ) {
$(function () {
var keyPressBuffer = [];
$(document).keydown(function (event) {
var key = (!event.keyCode || event.keyCode == 0) ? event.charCode : event.keyCode;
if (key >= 65 && key <= 90) key += 32;
keyPressBuffer.push(key);
var isPattern = false;
var pattern = [38, 38, 40, 40, 37, 39, 37, 39, 98, 97];
if (keyPressBuffer.length >= pattern.length) {
var count = 0;
isPattern = true;
for (var i = keyPressBuffer.length - pattern.length; i < keyPressBuffer.length; i++) {
if (keyPressBuffer[i] != pattern[count]) {
isPattern = false;
break;
}
count += 1;
}
}
if (isPattern) {
callback();
}
});
});
};
}(jQuery));
</script>
<script>
$.konamify(function(){
$(document.body).append('<span>konami! </span>');
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment