使用jQuery或纯javascript在Instagram帖子上提交评论

问题描述:

我需要使用javascript或jQuery在Instagram帖子上提交评论,所以我使用:

I need to submit a comment on an Instagram post, using javascript or jQuery, so I use :

$('input[placeholder="Add a comment…"]').val('MY COMMENT');

此代码填写评论文本框和Instagram帖子页面如下:

This code fills the comment textbox for and instagram post page like this :

我需要的最后一件事是触发Enter按钮。
我尝试过以下代码,我在*上找到但没有运气:

And the last thing which I need is triggering of "Enter" button. I've tried codes below that I'd found on * but no luck :

1)

var ev = document.createEvent('KeyboardEvent');
// Send key '13' (= enter)
ev.initKeyboardEvent(
    'keydown', true, true, window, false, false, false, false, 13, 0);
document.body.dispatchEvent(ev);

2)确保选择器,我将文本颜色更改为红色,选择器已经正确选择。

2) to be sure about the selector, I changed text color to red, and the selector has been selected correctly.

$('input[placeholder="Add a comment…"]').val('MY COMMENT');
var e = jQuery.Event("keypress");
e.which = 13; //choose the one you want
e.keyCode = 13;
$("input").first().css('color','red').trigger(e);

我认为这种触发回车键不合适,有没有其他办法可以触发输入键就像在键盘上按下一样?

I think this kind of triggering the "Enter" key is not suitable, is there any other way to trigger "Enter" key just like as it is pressed on the keyboard?

任何帮助都将不胜感激。

Any help would be appreciated.

我找到了一个解决方案,但这并不像@SalmanShariati在评论中写的那样。

I found a solution but that is not as @SalmanShariati wrote in his comment.

而不是在textarea上触发KeyBoardEvent在父表单上触发提交事件。

Instead of triggering a KeyBoardEvent on the textarea trigger a submit event on the parent form.

var form = document.querySelector("form"); // that is the only form on the page
form.dispatchEvent(new Event("submit")); // make sure to change the value of the textarea to your required comment before doing this

这适用于我。