I have found lots of info online about how to use the initEvent
and dispatchEvent
functions, but I can't for the life of me get them to work in practice.
I'm trying to get a script to press the Enter key every 5 seconds. My userscript code (minus irrelevant metadata) is below:
// ==UserScript==
// @namespace http://userscripts.org/scripts/show/153134
// @require https://ajax.googleapis.com/ajax/libs/jquery/1.6.0/jquery.min.js
//
// @grant unsafeWindow
//
// ==/UserScript==
$(function(){
window.setInterval(function(){
var ev = document.createEvent("KeyboardEvent");
ev.initKeyEvent("keypress", true, false, window, 0, 0, 0, 0, 13, 13);
window.dispatchEvent(evt);
}, 5000);
});
Checkout my script on userscript to see how poorly it works (add a user include domain and test it on any <textarea>
). Is Greasemonkey just not letting it through, or do I need to do something differently?
The only way I've been able to send keystrokes to the document and have them bubble properly is by creating an additional script insertion within the GM script. This example uses jQuery to simulate the keystroke event, it detects when the key "x" is pressed and sends "enter" to the document.
(edit: fixed equals/equivalency error)
There is a copy-paste error in that code.
Don't use
window.dispatchEvent(evt);
;use
window.dispatchEvent(ev);
Sending the event to
window
may not be what you need either. (Or it could be. Link to the target page.)Maybe send the event to the document:
Or send it to a specific node:
Or, since you are using jQuery: