Last active
January 4, 2016 00:39
-
-
Save borisschapira/8543238 to your computer and use it in GitHub Desktop.
Konami code IIFE, reproduces a Konami Code on both Firefox and Chrome
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
(function () { | |
// The Unicode keys | |
var characterKeys = { | |
u: 38, | |
d: 40, | |
l: 37, | |
r: 39, | |
a: 65, | |
b: 66 | |
}; | |
// The Konami code | |
var keys = ["u", "u", "d", "d", "l", "r", "l", "r", "b", "a"]; | |
var kbEvent = document.createEvent('KeyboardEvent'); | |
// Chromium Hack | |
Object.defineProperty(kbEvent, 'keyCode', { | |
get: function () { | |
return this.keyCodeVal; | |
} | |
}); | |
Object.defineProperty(kbEvent, 'which', { | |
get: function () { | |
return this.keyCodeVal; | |
} | |
}); | |
// Creating the event | |
var evtArgs = ["keydown", true, true, document.defaultView, false, false, false, false, 0, 0]; | |
// Here's the magic ! | |
for (var i = 0; i < keys.length; i += 1) { | |
if (kbEvent.initKeyboardEvent) { | |
evtArgs.splice(evtArgs.length - 2, 2, characterKeys[keys[i]], characterKeys[keys[i]]); | |
kbEvent.initKeyboardEvent.apply(kbEvent, evtArgs); | |
} else { | |
evtArgs.splice(evtArgs.length - 2, 2, characterKeys[keys[i]], 0); | |
kbEvent.initKeyEvent.apply(kbEvent, evtArgs); | |
} | |
kbEvent.keyCodeVal = characterKeys[keys[i]]; | |
document.body.dispatchEvent(kbEvent); | |
} | |
})(); |
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
(function(){var e={u:38,d:40,l:37,r:39,a:65,b:66},d="uuddlrlrba".split(""),a=document.createEvent("KeyboardEvent");Object.defineProperty(a,"keyCode",{get:function(){return this.keyCodeVal}});Object.defineProperty(a,"which",{get:function(){return this.keyCodeVal}});for(var c=["keydown",!0,!0,document.defaultView,!1,!1,!1,!1,0,0],b=0;b<d.length;b+=1)a.initKeyboardEvent?(c.splice(c.length-2,2,e[d[b]],e[d[b]]),a.initKeyboardEvent.apply(a,c)):(c.splice(c.length-2,2,e[d[b]],0),a.initKeyEvent.apply(a,c)), | |
a.keyCodeVal=e[d[b]],document.body.dispatchEvent(a)})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment