Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
chat annoyer
//must define a string and textarea id prior to running
//will simulate keydown events to make it look like you're always typing
//i need to do something useful with my life :|
var dispatchMouseEvent = function(target, var_args) {
var e = document.createEvent("MouseEvents");
//change to array
e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
};
var dispatchKeyboardEvent = function(target, initKeyboradEvent_args) {
var e = document.createEvent("KeyboardEvents");
e.initKeyboardEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
};
var dispatchTextEvent = function(target, initTextEvent_args) {
var e = document.createEvent("TextEvent");
e.initTextEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
};
var dispatchSimpleEvent = function(target, type, canBubble, cancelable) {
var e = document.createEvent("Event");
e.initEvent.apply(e, Array.prototype.slice.call(arguments, 1));
target.dispatchEvent(e);
};
var canceled = !dispatchKeyboardEvent(element,
'keydown', true, true, // type, bubbles, cancellable
null, // window
'h', // key
0, // location: 0=standard, 1=left, 2=right, 3=numpad, 4=mobile, 5=joystick
'' // space-sparated Shift, Control, Alt, etc.
);
//if you want to fake text too
//for effect only. Not necessary because a key press event is separate from
//changing the value of a textfield
var fakeType = function(string, element, i) {
//base case
if (string.length == i) {
element.value = string;
return;
}
i += 1;
element.value = string.substring(0, i);
//repeat every 200 ms.
//setTimeout because it's recursive call
setTimeout(function() {
fakeType(string, element, i);
}, 200);
}
//simulate mouse click
dispatchMouseEvent(element, 'mouseover', true, true);
dispatchMouseEvent(element, 'mousedown', true, true);
dispatchMouseEvent(element, 'click', true, true);
dispatchMouseEvent(element, 'mouseup', true, true);
//type something (for effect only)
fakeType(string, element, 0);
//simulate keyboard action
setInterval(function() {
dispatchKeyboardEvent(element, 'keypress', true, true, null, 'h', 0, '');
if (!canceled) {
if (dispatchTextEvent(element, 'textInput', true, true, null, 'h', 0)) {
element.value += 'h';
dispatchSimpleEvent(element, 'input', false, false);
// not supported in Chrome yet
// if (element.form) element.form.dispatchFormInput();
dispatchSimpleEvent(element, 'change', false, false);
// not supported in Chrome yet
// if (element.form) element.form.dispatchFormChange();
}
}
dispatchKeyboardEvent(element, 'keyup', true, true, null, 'h', 0, '');
}, 5000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment