javascriptclickgmailsimulate

Simulate click on GMail div button with JS


I want to simulate click on GMail COMPOSE button using JS without JQuery.

Here is button:

<div class="T-I J-J5-Ji T-I-KE L3" role="button" tabindex="0" gh="cm" 
style="-webkit-user-select: none;">COMPOSE</div>

Here is my js:

var element = document.getElementsByClassName('T-I-KE')[0];
element.click();

Result:undefined in all browsers

Image: https://i.sstatic.net/Mkscf.png

Already tried that:

var event = document.createEvent("MouseEvent");
event.initEvent("click",true,true);
var element=document.getElementsByClassName("T-I-KE")[0];
element.dispatchEvent(event);

Result:true. But nothing happens.

Image: https://i.sstatic.net/H3KSH.png


Solution

  • After two days i am finally got an answer!

    That button listens mouseDown and mouseUP events. Working code:

    var down = new MouseEvent('mousedown');
    var up = new MouseEvent('mouseup');
    var elem = document.getElementsByClassName("T-I-KE")[0];
    elem.dispatchEvent(down);
    elem.dispatchEvent(up);