簡體   English   中英

我正在嘗試使用 javascript 在 WhatsApp 上發送短信?

[英]I am trying to send text- messages on WhatsApp with javascript?

我正在嘗試在 chrome 上的 whatsapp 網絡版本上發送短信。 ( www.web.whatsapp.com )

這是代碼:

 document.getElementsByClassName("input")[1].innerHTML="This message was written via JS script! "; var input = document.getElementsByClassName("icon btn-icon icon-send"); input[0].click();

但問題是,最初當沒有文本出現時,輸入框如下所示:在此處輸入圖片說明

只有當我實際寫一些文本時,它才會更改為:

在此處輸入圖片說明

現在我的腳本才起作用,因為它需要Send text button

我嘗試使用 Jquery 代碼通過以下函數模擬 $('.input) 處的按鍵操作:

 function pressKey() { var e = jQuery.Event("keypress"); e.which = 32; // # space $(".input").trigger(e)[1]; e.which = 91; $(".input").trigger(e)[1]; e.which = 32; // # space $(".input").trigger(e)[1]; e.which = 32; // # space $(".input").trigger(e)[1]; }

它沒有用。

如何通過腳本獲取Send text按鈕?

這是屏幕錄制

所有選項都不適合我。 這就是我所做的。

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();
}

在打開對話時試試這個片段:

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()

正如 Khalid Lafi 所說,這是正確的腳本。 他的代碼確實會在執行時返回錯誤

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();

希望這可以幫助。

以下是更新后的腳本。 希望這可以幫助。

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);

這在 2019 年 12 月有效。 Shubham 的原始片段由 Cami Rodriguez 修改(見上面的評論)。

 function write_in_chat(text) { var input = document.querySelector('#main [contenteditable~=true]'); setTimeout(() => { input.innerHTML = text; input.dispatchEvent(new Event('input', {bubbles: true})); var button = document.querySelector('button>span[data-icon="send"]').parentElement; button.click(); }, 500); }

$(".input").on("keypress",function(e){ 
 if(e.which == 32 ||  e.which == 13){ alert('msg sent')};

});

您必須在分配時比較 ==

使用 KeyPress 將事件觸發到輸入中實際上不會填充文本區域,因為填充是本機事件的一部分。

如果您打算填充文本字段,則只需設置 val。 在 jQuery 中,這可以通過.val()來完成,例如:

function pressKey() {
  $(".input").val('test');
}

WhatsApp 輸入框可能有一個等待 keyup 事件的事件偵聽器,如果該字段已填充,則將其切換到發送按鈕。 如果是這種情況,那么您可以手動觸發一個事件,該事件將觸發 WhatsApp 的(非本地)代碼。

function pressKey() {
  $(".input").val('test').trigger($.Event('keyup'));
}

這是工作腳本:

  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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM