Skip to content

Instantly share code, notes, and snippets.

@fermion
Forked from gf3/prototype.konami.js
Created December 18, 2009 13:29
Show Gist options
  • Save fermion/259496 to your computer and use it in GitHub Desktop.
Save fermion/259496 to your computer and use it in GitHub Desktop.
// 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.
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