Simulate enter key pressed via Go/ GopherJS, i.e., programmatically fire enter
key event in browsers without user intervention.
JavaScript
From we know JavaScript code for firing enter keyevent would be:
const ke = new KeyboardEvent("keyup", {keyCode: 13});
document.body.dispatchEvent(ke);
GopherJS
The above code in Go/GopherJS is as follows:
import (
"github.com/gopherjs/gopherjs/js"
)
option := js.Global.Get("Object").New()
option.Set("keyCode", 13)
ke := js.Global.Get("KeyboardEvent").New("keyup", option)
js.Global.Get("document").Get("body").Call("dispatchEvent", ke)
GopherJS + godom
To make your code more readable, we can prettify the above code with godom:
import (
. "github.com/siongui/godom"
)
option := Window.Get("Object").New()
option.Set("keyCode", 13)
ke := Window.Get("KeyboardEvent").New("keyup", option)
Document.Get("body").Call("dispatchEvent", ke)
References: