Created
March 12, 2014 17:44
-
-
Save borisschapira/9512188 to your computer and use it in GitHub Desktop.
Random Up, Down, Left, Right
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 () { | |
function shuffle(array) { | |
var currentIndex = array.length, | |
temporaryValue, | |
randomIndex | |
; | |
// While there remain elements to shuffle... | |
while (0 !== currentIndex) { | |
// Pick a remaining element... | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
// And swap it with the current element. | |
temporaryValue = array[currentIndex]; | |
array[currentIndex] = array[randomIndex]; | |
array[randomIndex] = temporaryValue; | |
} | |
return array; | |
} | |
var characterKeys = [ | |
38, | |
//u | |
40, | |
//d | |
37, | |
//l | |
39 | |
//u | |
]; | |
// The Konami code | |
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 < 1000; i += 1) { | |
var rand = Math.floor(Math.random() * characterKeys.length); | |
if (kbEvent.initKeyboardEvent) { | |
evtArgs.splice(evtArgs.length - 2, 2, characterKeys[rand], characterKeys[rand]); | |
kbEvent.initKeyboardEvent.apply(kbEvent, evtArgs); | |
} else { | |
evtArgs.splice(evtArgs.length - 2, 2, characterKeys[rand], 0); | |
kbEvent.initKeyEvent.apply(kbEvent, evtArgs); | |
} | |
kbEvent.keyCodeVal = characterKeys[rand]; | |
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 c=[38,40,37,39],a=document.createEvent("KeyboardEvent");Object.defineProperty(a,"keyCode",{get:function(){return this.keyCodeVal}});Object.defineProperty(a,"which",{get:function(){return this.keyCodeVal}});for(var b=["keydown",!0,!0,document.defaultView,!1,!1,!1,!1,0,0],e=0;1E3>e;e+=1){var d=Math.floor(Math.random()*c.length);a.initKeyboardEvent?(b.splice(b.length-2,2,c[d],c[d]),a.initKeyboardEvent.apply(a,b)):(b.splice(b.length-2,2,c[d],0),a.initKeyEvent.apply(a,b));a.keyCodeVal= | |
c[d];document.body.dispatchEvent(a)}})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment