Skip to content

Instantly share code, notes, and snippets.

@keang
Created Mar 12, 2014
Embed
What would you like to do?
//Only tested in Firefox with firebug
// -> Install the firebug addon
// -> Open firebug console / press F12
// -> Copy and paste this code
// -> Click run
//To change strategy, uncomment one and comment out the other:
//setInterval(moveRandom, 100);
setInterval(moveOneRound, 250);
var keydownEvent = document.createEvent("KeyboardEvent");
var keyupEvent = document.createEvent("KeyboardEvent");
var keyleftEvent = document.createEvent("KeyboardEvent");
var keyrightEvent = document.createEvent("KeyboardEvent");
var initMethod = typeof keydownEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";
keydownEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
40, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
keyupEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
38, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
keyleftEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
37, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
keyrightEvent[initMethod](
"keydown", // event type : keydown, keyup, keypress
true, // bubbles
true, // cancelable
window, // viewArg: should be window
false, // ctrlKeyArg
false, // altKeyArg
false, // shiftKeyArg
false, // metaKeyArg
39, // keyCodeArg : unsigned long the virtual key code, else 0
0 // charCodeArgs : unsigned long the Unicode character associated with the depressed key, else 0
);
function sleep(millis, callback) {
setTimeout(function()
{ callback(); }
, millis);
}
function moveOneRound(){
if(document.getElementsByClassName('game-over').length==0){
document.dispatchEvent(keyupEvent);
document.dispatchEvent(keyrightEvent);
document.dispatchEvent(keydownEvent);
document.dispatchEvent(keyleftEvent);
}
}
function moveRandom(){
var move = Math.floor((Math.random()*4)+1);
if(move==1)
document.dispatchEvent(keyupEvent);
else if(move==2)
document.dispatchEvent(keyrightEvent);
else if(move==3)
document.dispatchEvent(keydownEvent);
else if(move==4)
document.dispatchEvent(keyleftEvent);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment