如何在JS中将控制台命令发送到网页?

我正在尝试通过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();,什么也没做。

必须有一种方法可以将控制台命令发送到网页。我想念什么? 如果有人能指出我正确的方向,我将不胜感激。

lmzxd 回答:如何在JS中将控制台命令发送到网页?

您可以使用正确的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://www.f2er.com/3146032.html

大家都在问