Easy way to extract mail data from Gmail in browser


#1

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
or
https://mail.google.com/mail/u/0/#search/keyboard+maestro/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.)


#2

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:

image

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

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

}  // END of function myMain()
)();

Questions?