-
-
Save fermion/259496 to your computer and use it in GitHub Desktop.
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. | |
document.observe('keyup', (function(element){ | |
var cache = [], code = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13], 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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment