简体   繁体   English

在Enter键模拟时触发默认行为

[英]Triggering default behavior on enter key press simulation

So (just to start with "so") I want to create a greasemonkey script for a qwebirc platform. 因此(仅从“ so”开始),我想为qwebirc平台创建一个油脂猴子脚本。 I need to send replies automatically based on some events that happen. 我需要根据发生的一些事件自动发送回复。 For those who don't know, once logged in the qwebirc, there is an input text field at the bottom of the screen where you can write your reply and then, by pressing enter, the reply is sent to the server. 对于那些不知道的人,登录qwebirc后,在屏幕底部会出现一个输入文本字段,您可以在其中输入答复,然后按Enter键,将答复发送到服务器。 The input field is contained in a form element, but the form has no submit button. 输入字段包含在表单元素中,但是表单没有提交按钮。

I can populate the input field (no problem) by writing to its "value" attribute, but I found no way to "submit" the reply to the server. 我可以通过写入输入字段的“值”属性来填充输入字段(没有问题),但是我找不到将响应“提交”到服务器的方法。 I tried calling the .submit() method on the container form element, but that simply reloads the page (so it's sending to itself - and that makes sense, 'cause there's no "action" attribute on the form element). 我尝试在容器form元素上调用.submit()方法,但这只是重新加载页面(因此它正在向自身发送-这很有意义,因为form元素上没有“ action”属性)。 It seems to me that the only explanation is that once the enter key is pressed in the input field there's a method that's called somewhere on the qwebirc.js. 在我看来,唯一的解释是,一旦在输入字段中按下Enter键,就会在qwebirc.js的某处调用一种方法。

I can't find the code responsible for this action (though I worked that in Chrome, I set an Event Listener Breakpoint on the Keyboard - Input, and that stopped me when I pressed the enter key and pointed me to the beginning of some function) because the script is minified and there's no way for me to know what those "m", "n", "p", "t" etc mean. 我找不到负责此操作的代码(尽管我在Chrome中工作,但我在键盘-输入上设置了事件监听器断点,当我按下Enter键并指向某个功能的开头时,这使我停了下来),因为脚本已缩小,我无法知道“ m”,“ n”,“ p”,“ t”等含义。 If I had the uncompressed version, I could scan it and, maybe, find it out. 如果我有未压缩的版本,则可以扫描它,也许可以找到它。 I also tried downloading the qwebirc project, but there are loads of files there - whereas on the server I'm trying to write the greasemonkey script there's a single script. 我也尝试下载qwebirc项目,但是那里有很多文件-而在服务器上,我试图编写单个的脚本润滑脂脚本。

The main idea here is that I guess the enter key press simulation should trigger the sending function - wherever and whichever that may be. 这里的主要思想是,我想Enter键模拟应该触发发送功能-无论何时何地。

In order to do this I found on stackoverflow some questions with the same subject - the simulation of enter key press. 为了做到这一点,我在stackoverflow上发现了一些与同一主题有关的问题-Enter键模拟。 I tried that code: 我尝试了该代码:

function simulate() {
    var evt = document.createEvent('KeyboardEvent'),
        input = document.querySelector('.keyboard-input');

    evt.initKeyboardEvent(
        'keydown', 
        true, 
        true, 
        window, 
        false, 
        false, 
        false, 
        false, 
        13, 
        0
    );

    input.dispatchEvent(evt);
}

The event is created (I can see it in my DOM panel in FireBug), but no action whatsoever is taken. 事件已创建(我可以在FireBug的DOM面板中看到它),但是不执行任何操作。 I mean why doesn't the .dispatchEvent() behave just like a real enter key press ? 我的意思是为什么.dispatchEvent()的行为不像真正的Enter键?

Thanks in advance. 提前致谢。

For the purposes of this answer, I'm going to assume that the message input pseudo-form is like others - ie the message can be sent by either pressing Enter or clicking a Send button. 对于这个答案的目的,我会假设该消息输入伪形式是和别人一样-即消息可以通过按Enter键单击发送按钮发送。

You've tried the former; 您已经尝试过前者; it didn't work. 它没有用。 It's possible that the page is waiting for the Send button to be clicked (which may be triggered by an Enter keypress (though no, I don't know why your event isn't working - I'm unfamiliar with event intricacies)). 页面可能正在等待单击“发送”按钮(这可能由Enter键触发(尽管不,我不知道您的事件为何不起作用-我不熟悉事件的复杂性))。 Instead, then, try that. 而是尝试一下。

Also, take note : IE uses fireEvent not dispatchEvent - so if you're in IE, this may be the problem. 另外, 请注意 :IE使用fireEvent而不是dispatchEvent因此,如果您使用的是IE,则可能是问题所在。

To fire a click on the Send button: 要触发发送按钮,请执行以下操作:

var e = document.createEvent("MouseEvents");
e.initEvent("click", true, true);
e.target = "button#send";

if(document.fireEvent) {
    document.fireEvent(e);
}
else {
    document.dispatchEvent(e);
}

Change the string assigned to e.target to the CSS selector that uniquely identifies the Send button. 将分配给e.target的字符串更改为唯一标识“发送”按钮的CSS选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM