One of the famous easter eggs. http://en.wikipedia.org/wiki/Konami_code
-
-
Save tsaniel/1188477 to your computer and use it in GitHub Desktop.
function( | |
a, // callback function | |
b // placeholder for key stack | |
){ | |
document.onkeyup = function(e){ | |
/113302022928$/.test( // check if the latest pressed codes are Konami Code | |
b += [((e||self.event).keyCode-37)]) // push the code it into the stack | |
) && a() // callback if pressed Konami Code | |
} | |
} |
function(f,a){document.onkeyup=function(e){/113302022928$/.test(a+=[((e||self.event).keyCode-37)])&&f()}} |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
{ | |
"name": "KonamiCodeEasterEgg", | |
"description": "The famous Konami Code easter egg.", | |
"keywords": [ | |
"Konami Code", | |
"easter egg", | |
"fun" | |
] | |
} |
<!DOCTYPE html> | |
<title>Konami Code easter egg</title> | |
<div>Expected value: <b>Try to press (↑ ↑ ↓ ↓ ← → ← → B A)</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
// write a small example that shows off the API for your example | |
// and tests it in one fell swoop. | |
var myFunction = function(f,a){document.onkeyup=function(e){/113302022928$/.test(a+=[((e||self.event).keyCode-37)])&&f()}}; | |
myFunction(function(){alert('onKonamiCode triggered!')}) | |
</script> |
@atk: IINM there's no problem with f
being anonymous. What is "injected" in the string set to document.onkeyup
is the string representation of f
Also it should be possible to get your approach working repeatedly using the correct magic 32bits mask of the Konami code. Multiplying a
and adding the last keyCode will make the oldest keyCodes overflow. That would bring us down to ~97 bytes with something like:
function(f,a){document.onkeyup=function(e){0x12345678^(a=a*1337+((e||self.event).keyCode))||f()}}
I take it the correct values for 0x12345678 and 1337 are yet to be determined?
Correct.
http://jsbin.com/ofitoj/edit#preview works fine in IE8, Chrome Canary, Opera 12 and Firefox Aurora. I think it's safe to get rid of the self.
The magic limit is 4294967296, so we'll need to fit the 10 keycodes 38, 38, 40, 40, 37, 37, 39, 39, 65, 66 inside it.
Thanks to the string coercion with array literal trick, we save another byte.
Just 78 bytes:
function(f,a){onkeyup=function(e){/113302022928$/.test(a+=[e.which-37])&&f()}}
If you set a as 0, you can get away with
function(f,a){document.onkeyup=function(e){(a+=[(e||self.event).keyCode-37])%1e12==113302022928&&f()}}
// as in
(function(f,a){document.onkeyup=function(e){(a+=[(e||self.event).keyCode-37])%1e12==113302022928&&f()}})(callback, 0)
I think we should not sacrifice the compatibility in this case...