Vitalio
Vitalio

Reputation: 31

Click on Gmail's button using JS or jQuery

I'm trying to click on "close" button in Gmail compose dialog using JS from chrome console. This button have a class "Ha". Classes are static and will not be changed after the refresh.

Screenshot

enter image description here

I've tried to use this code, but nothing happens:

document.querySelector(".Ha").dispatchEvent(new MouseEvent("mousedown"));

Upvotes: 1

Views: 412

Answers (2)

Vitalio
Vitalio

Reputation: 31

Here is the solution:

var test = document.getElementsByClassName("Ha")[0];
var e = document.createEvent('Event');
e.initEvent("mouseup", true, true);
test.dispatchEvent(e);

Upvotes: 2

Chris Rutherfurd
Chris Rutherfurd

Reputation: 1687

To answer the basic jQuery issue what you need is...

$( ".Ha" ).trigger( "click" );

to trigger a click event on the dom object in question. While when you click on an object using the mouse a mousedown, a mouseup, and a click event are all all triggered, by triggering through code you need to specify which specific event is triggered base on which event is monitored for by the web app. You also need to be careful about using class based selectors as classes can exist in more than one place in the dom structure and you could wind up triggering click events on the wrong dom object or on multiple dom objects which can cause unexpected results, the best way is to use an object's id tag if it exists.

As a side note be careful with the use case for what you are trying to do as you could be violating Google's terms of use by writing code to go around their UI and their own coding.

Upvotes: 0

Related Questions