我正在尝试通过Firefox的Greasemonkey向聊天室提交一条消息。网页上没有按钮,必须使用enter键发送消息。检查聊天输入字段,将显示以下内容:
<input type="text" id="chatcli" placeholder="type chat message here" onkeyup="if(event.keyCode == 13) ChatClient.sendLine();">
我可以使用document.getElementById('chatcli').value = "/bot command here"+x编辑文本框,但不知道如何实际发送它。
我注意到,如果我在控制台中键入ChatClient.sendLine();,消息就会发送。我尝试将该代码放入我的Greasemonkey脚本中,但它不起作用。我也试过document.ChatClient.sendLine();,什么都没有。
必须有一种将控制台命令发送到网页的方法。我遗漏了什么?如果有人能给我指出正确的方向,我将不胜感激。
发布于 2019-11-07 17:55:12
您可以使用正确的keyCode调度键盘事件。
document.querySelector("#btn").addEventListener("click",function(){
var input = document.querySelector("#chatcli");
input.value = "/bot command here";
input.dispatchEvent(new KeyboardEvent('keyup',{'keyCode':13}));
});<input type="text" id="chatcli" placeholder="type chat message here" onkeyup="if(event.keyCode == 13) console.log('enter pressed. textbox value:' + this.value);">
<button id="btn">click me to test</button>
https://stackoverflow.com/questions/58745911
复制相似问题