Easy way to extract mail data from Gmail in browser

Say I have Chrome open to a Gmail single-message view—so the URL is something like https://mail.google.com/mail/u/0/#inbox/12345678901234567890

Is there a simple way for me to get email data like Sender, Subject, and Body into Keyboard Maestro variables?

(I don't want to use a different email app if I can help it.)

Here's the basic script for a Execute a JavaScript in Browser action that worked for me:

subjectStr = document.getElementById(':lu').innerText
oSender = document.querySelector('span.gD');
senderStr = oSender.innerText + ' <' + oSender.getAttribute('email') + '>'
bodyStr = document.getElementById(':nd').innerText

For production use, I would prefix each of those with a "var " and put in a function to ensure no conflicts.

You will need to test in your setup to see if the web page IDs and Classes will work for you.
If you want to save each JS variable as a KM variable, then the easiest approach is to put each statement in a separate Execute a JavaScript in Browser action, like this:


'use strict';
(function myMain() {  // this will auto-run when script is executed

var subjectStr = document.getElementById(':lu').innerText;
return subjectStr;

}  // END of function myMain()


Belatedly, thanks for this, but Gmail uses different two-character class names for me, and they change from day to day. So I think there's no way to do this with JavaScript.

You may be right, but maybe this will work.
Try this JavaScript for the subject element:

var subjectStr = document.querySelector('div.ha h2.hP').innerText;

Hey Gabriel,

Gmail?  Simple?


That said – if you want to work hard enough it looks like the code is amenable to parsing.

How much hard work and how amenable is for the user to discover.