Skip to content

Instantly share code, notes, and snippets.

@gf3
Created December 18, 2009 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gf3/259478 to your computer and use it in GitHub Desktop.
Save gf3/259478 to your computer and use it in GitHub Desktop.
KONAMI cheat code for your site!
// Example
document.observe("lol:konami", function() {
$(document.body).insert(new Element('h1').update("KONAMI!!!").setStyle({fontWeight: "bold", color: "red"}));
});
// Fires "lol:konami" event on the document when the cheat is entered.
// NOTE: This version fires after the konami cheat sequence is detected (w/o ENTER)
document.observe('keyup', (function(element){
var cache = [], code = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65], checkTimer, clearTimer, i;
function clearCache() {
cache = [];
}
function checkCache() {
// Set inactivity timeout
if (clearTimer) clearTimeout(clearTimer);
clearTimer = setTimeout(clearCache, 500);
// Check code
if (cache.length != code.length) return;
i = cache.length;
while (--i) {
if (code[i] != cache[i]) return;
}
clearTimeout(clearTimer);
clearCache();
element.fire("lol:konami");
}
// Observer
return function(event) {
// Check cache
cache.push(event.keyCode ? event.keyCode : event.charCode);
// Check when done typing
if (clearTimer) clearTimeout(clearTimer);
if (checkTimer) clearTimeout(checkTimer);
checkTimer = setTimeout(checkCache, 500);
};
})(document));
// Fires "lol:konami" event on the document when the cheat is entered.
// NOTE: This version fires after the ENTER key is hit
document.observe('keyup', (function(element){
var cache = [], code = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65], clearTimer, i;
function clearCache() {
cache = [];
}
function checkCache() {
clearTimeout(clearTimer);
// Check code
if (cache.length != code.length) return;
i = cache.length;
while (--i) {
if (code[i] != cache[i]) return;
}
element.fire("lol:konami");
}
// Observer
return function(event) {
// Set inactivity timeout
if (clearTimer) clearTimeout(clearTimer);
clearTimer = setTimeout(clearCache, 500);
// Check cache
var key = event.keyCode ? event.keyCode : event.charCode;
if (key == 13) {
checkCache();
clearCache();
}
else cache.push(key);
};
})(document));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment