【问题标题】:How to send a console command to a webpage in JS?如何在 JS 中向网页发送控制台命令?
【发布时间】:2020-03-03 20:49:14
【问题描述】:

我正在尝试通过 Greasemonkey for Firefox 向聊天室提交消息。网页上没有按钮,消息必须用回车键发送。检查聊天输入字段,出现:

<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();,没有。

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

【问题讨论】:

  • 如果在沙箱中运行,显式引用窗口变量:window.ChatClient.sendLine()(或unsafeWindow.ChatClient.sendLine(),不确定需要哪个),或者将代码注入 DOM,或者禁用沙盒,其中之一可能会工作
  • unsafeWindow.ChatClient.sendLine() 工作。谢谢! @CertainPerformance

标签: javascript chat bots greasemonkey chatbot


【解决方案1】:

您可以使用正确的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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 2012-08-14
    • 2014-08-24
    • 2020-08-27
    • 2021-07-24
    • 1970-01-01
    相关资源
    最近更新 更多