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> |
Great idea !
And maybe the document.
and window.
are for the compatibility.
Fair enough about the compat.
Thanks for reminding me the syntax of the eval('"\\x'...+'"')
trick. Good idea to use a /...^$/.test(...)
thus saving the .slice(-10)
. I tried using which
instead of keyCode
but IE8 didn't seem too happy.
Replacing window.
by self.
saves another 2 bytes ;)
I also have to thank you for reminding me the "codes to characters" and the self.
tricks :)
eval('"\\x'+...+'"')
is rather big. Without it, we have
function(f,a){document.onkeyup=function(e){/38384040373739396566$/.test(a+=''+(e||self.event).keyCode)&&f()}}
saving another 4 bytes - Though I have to admit it is not near as precise as the conversion.
Yes, it is more prone to false positive but who cares ? Finding and entering such false positive should be rewarded too!
3 bytes shorter ;)
function(f,a){document.onkeyup=function(e){/113302022928$/.test(a+=''+((e||self.event).keyCode-37))&&f()}}
Nice.
Maybe there are more shorter versions through this way.
Three more bytes saved by not using the regexp:
function(f,a){document.onkeyup=function(e){2200806046112155E4^(a=a*94+((e||self.event).keyCode))||f()}}
Btw, you forgot to change the regexp in the annotated version.
@atk: Unless I'm missing something, this implementation will only work once, and only if the konami code is the first thing the user enters.
A shorter approach I thought of but found compatibility issues with was:
function(f,a){document.onkeyup="/113302022928$/.test(a+=''+(self.event.keyCode-37))&&"+f+"()"}
You're right - I'll have to work on that. But this way, "f" cannot be an anonymous function, but needs to be somewhere in the global scope.
I think we should not sacrifice the compatibility in this case...
@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)
126 bytes version
function(f,a){document.onkeyup=function(e){(a+=String.fromCharCode((e||window.event).keyCode)).slice(-10)=="&&((%'%'BA"&&f()}}
Do we actually need the
document.
andwindow.
?