Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我正在尝试用javascript在WhatsApp上发送文本消息?_Javascript_Jquery - Fatal编程技术网

我正在尝试用javascript在WhatsApp上发送文本消息?

我正在尝试用javascript在WhatsApp上发送文本消息?,javascript,jquery,Javascript,Jquery,我试图在chrome上的whatsapp网络版上发送短信。 () 代码如下: document.getElementsByClassName(“输入”)[1].innerHTML=“此消息是通过JS脚本编写的!”; var input=document.getElementsByClassName(“图标btn图标发送”); 输入[0]。单击() 您必须比较==在分配=时,使用按键将事件触发到输入中不会实际填充文本区域,因为填充是本机事件的一部分 如果要填充文本字段,只需设置val即可。在jQ

我试图在chrome上的whatsapp网络版上发送短信。 ()

代码如下:

document.getElementsByClassName(“输入”)[1].innerHTML=“此消息是通过JS脚本编写的!”;
var input=document.getElementsByClassName(“图标btn图标发送”);
输入[0]。单击()

您必须比较==在分配=

时,使用按键将事件触发到输入中不会实际填充文本区域,因为填充是本机事件的一部分

如果要填充文本字段,只需设置val即可。在jQuery中,可以使用
.val()
,例如:

function pressKey() {
  $(".input").val('test');
}
WhatsApp输入框可能有一个事件监听器等待keyup事件,如果该字段已填充,则将其切换到send按钮。如果是这种情况,那么您可以手动触发一个事件,该事件将触发WhatsApp的(非本机)代码


打开对话时,请尝试以下片段:

function dispatch(target, eventType, char) {
   var evt = document.createEvent("TextEvent");    
   evt.initTextEvent (eventType, true, true, window, char, 0, "en-US");
   target.focus();
   target.dispatchEvent(evt);
}


dispatch(document.querySelector("#compose-input div"), "textInput", "hello!");

function triggerClick() {
  var event = new MouseEvent('click', {
    'view': window,
    'bubbles': true,
    'cancelable': true
  });
  document.querySelector(".icon.btn-icon.icon-send").dispatchEvent(event)
}
triggerClick()

这是工作脚本:

  function dispatch(target, eventType, char) {
            var evt = document.createEvent("TextEvent");
            evt.initTextEvent (eventType, true, true, window, char, 0, "en-US");
            target.focus();
            target.dispatchEvent(evt);
        }


        dispatch(document.querySelector(".input-container > .input-emoji .input"), "textInput", "hello!");

        function triggerClick() {
            var event = new MouseEvent('click', {
                'view': window,
                'bubbles': true,
                'cancelable': true
            });
            document.querySelector(".icon.btn-icon.icon-send").dispatchEvent(event)
        }
        triggerClick();

正如哈立德·拉菲所说,这是正确的剧本。他的代码执行时将返回一个错误

dispatch(document.querySelector("#compose-input div"), "textInput", "hello!");
这是因为您应该使用“input.div”而不是“#compose input div”。以下脚本适用于我

function dispatch(target, eventType, char) {
    var evt = document.createEvent("TextEvent");    
    evt.initTextEvent (eventType, true, true, window, char, 0, "en-US");
    target.focus();
    target.dispatchEvent(evt);
}


dispatch(document.querySelector("div.input"), "textInput", "hello!");

function triggerClick() {
var event = new MouseEvent('click', {
  'view': window,
  'bubbles': true,
  'cancelable': true
 });
document.querySelector(".icon.btn-icon.icon-send").dispatchEvent(event);
}

triggerClick();

希望这有帮助。

所有选项对我都不起作用。我就是这么做的

function sendMessage(message) {
    var evt = new Event('input', {
        bubbles: true
    });

    var input = document.querySelector("div.input");
    input.innerHTML = message;
    input.dispatchEvent(evt);

    document.querySelector(".icon-send").click();
}
var input = document.querySelector('.block-compose .input');
setTimeout(function(){
    for(var j = 0; j < 1; j++) {
        input.innerHTML = "Hello";
        input.dispatchEvent(new Event('input', {bubbles: true}));
        var button = document.querySelector('.block-compose button.icon-send');
        button.click();
    }
},1000);

以下是更新后的脚本。希望这有帮助

var input = document.querySelector('.block-compose .input');
setTimeout(function(){
    for(var j = 0; j < 1; j++) {
        input.innerHTML = "Hello";
        input.dispatchEvent(new Event('input', {bubbles: true}));
        var button = document.querySelector('.block-compose button.icon-send');
        button.click();
    }
},1000);
var input=document.querySelector('.block compose.input');
setTimeout(函数(){
对于(var j=0;j<1;j++){
input.innerHTML=“你好”;
dispatchEvent(新事件('input',{bubbles:true}));
var button=document.querySelector('.block compose button.icon send');
按钮。单击();
}
},1000);

这将于2019年12月生效。Shubham的原始代码片段由Cami Rodriguez修改(见上面的评论)

功能写入聊天(文本){
var input=document.querySelector(“#main[contenteditable~=true]”);
setTimeout(()=>{
input.innerHTML=文本;
dispatchEvent(新事件('input',{bubbles:true}));
var button=document.querySelector('button>span[data icon=“send”]”)。parentElement;
按钮。单击();
}, 500);

}
通过单击enter键进行检查,它会提醒您,您的输入框中应该有class=“input”没有回答我的问题。我想模拟击键效果,以使
发送文本
按钮不使用js函数来检测击键。它抛出未捕获的TypeError:无法读取null(…)的属性“focus”而且我认为他们已经改变了它,因为#compose input div不存在anymore@khalid-lafi这里什么是TextEvent?类型错误:evt.initTextEvent不是函数initTextEvent-->不适用于firefox。Mozilla站点声明它未实现。获取此错误
uncaughtTypeError:无法读取null(…)的属性“dispatchEvent”
此操作仍然有效吗?我收到一个错误:
Uncaught TypeError:无法读取null(…)的属性“dispatchEvent”
当我调用
triggerClick
仍在工作时,仅在输入和文档中将选择器更改为“#main[contenteditable~=true]”。querySelector('button>span[data icon=“send”]”)Button中的.parentElement我不理解代码的其余部分,但您能解释这一行的作用吗?dispatchEvent(新事件('input',{bubbles:true}));我不理解代码的其余部分,但你能解释一下这行代码的作用吗<代码>输入.dispatchEvent(新事件('input',{bubbles:true}))。这里解释一下。
var input = document.querySelector('.block-compose .input');
setTimeout(function(){
    for(var j = 0; j < 1; j++) {
        input.innerHTML = "Hello";
        input.dispatchEvent(new Event('input', {bubbles: true}));
        var button = document.querySelector('.block-compose button.icon-send');
        button.click();
    }
},1000);