Created
December 18, 2009 13:02
-
-
Save gf3/259478 to your computer and use it in GitHub Desktop.
KONAMI cheat code for your site!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example | |
document.observe("lol:konami", function() { | |
$(document.body).insert(new Element('h1').update("KONAMI!!!").setStyle({fontWeight: "bold", color: "red"})); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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