Search code examples
javascriptgreasemonkeykeypress

How to simulate a keypress in a Greasemonkey script?


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?


Solution

  • 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:

    document.body.dispatchEvent(ev);
    

    Or send it to a specific node:

    var targetNode  = document.querySelector ("#content textarea"); // Etc.
    targetNode.dispatchEvent (ev);
    


    Or, since you are using jQuery:

    var ev = $.Event('keypress');
    ev.which = 13; // Carriage-return (Enter)
    $('body').trigger(ev);