Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can't rely on the class or the id attribute of the Send button because they change everytime a compose field is created, and may even change per user. What do we know about the button then?</p> <ul> <li>It's innerHTML is "Send" (See footnote)</li> <li>It has a role attribute with the value "button"</li> <li>It's inside a "Compose New Message" container, that also has other labels as "From", "To" (See footnote 2)</li> </ul> <p>With that information, we can use a nice Xpath query in native Javascript that retrieves our button.</p> <pre><code>var xpath = '//div[@role="button" and text()="Send"]' var query = document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); var button = query.iterateNext(); </code></pre> <p>If you are using a library as jQuery, feel free to use it with the css selectors <code>[role="button"]</code> and a filter function provided in a previous answer.</p> <p>This could be improved with the tables information appending it as a parent, but this should be enough (you can read more about it in the <a href="https://developer.mozilla.org/en/docs/Introduction_to_using_XPath_in_JavaScript" rel="nofollow">Developer Mozilla Introduction to Xpath</a>). Hopefully the answer provides a good performance.</p> <p><strong>UPDATE:</strong> It seems Gmail does stick with some classes for some things (classes like <code>T-I J-J5-Ji</code> seem to be shared between the old and new version). Yet, I wouldn't use it, if other developer stumbles upon that class and you are getting the button through only that, there's no way he or she can understand what you are getting, specially if there are no comments and a poor variable name is used.</p> <p>A solution that actually retrieves the answer uses a CSS query selector that looks like <code>div &gt; div &gt; div &gt; div &gt; table div</code>; I wouldn't use it neither for the same reason. Plus, it reminds me of a poor descriptive <a href="http://theprofoundprogrammer.com/post/28623204754/text-regular-expressions-photograph-of-a" rel="nofollow">Regular Expression</a>. You are not only coding for you, but also to the future yourself that may have no idea about the project.</p> <hr> <p><sup>Footnote: OR the equivalent to the user's language. I'm not sure if all the users have their Gmail in english, but I wouldn't make that assumption. If I were you, I would create a dictionary with the locales of the most common keywords for "Send" and iterate evaluate through most of them. The amount of languages you could iterate through could be limited to languages <a href="http://support.google.com/mail/bin/answer.py?hl=en&amp;answer=17091" rel="nofollow">Gmail support</a>.</sup></p> <p><sup>Footnote 2: Actually, when you press "Compose" it adds the labels "From" and "To", but if you don't fill them right away, they dissapear, showing just some divs with the innerHTML's "Recipients" and "Subject"</sup></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload