Skip to content

Instantly share code, notes, and snippets.

@CVertex
Last active August 26, 2019 00:56
Show Gist options
  • Save CVertex/82a982cfcba5eb1db5079a16d5051d1b to your computer and use it in GitHub Desktop.
Save CVertex/82a982cfcba5eb1db5079a16d5051d1b to your computer and use it in GitHub Desktop.
async function getLoggedInEmail() {
return new Promise((resolve, reject) => {
try {
// header element works on gmail
// .ongoogle-material-minibar works on slides UI
let header = document.querySelector('header');
if (header === null) {
// Try mini-bar
header = document.querySelector('.onegoogle-material-minibar')
}
header
.querySelectorAll('a[aria-label]')
.forEach((el) => {
let pattern = /Google Account:.*\n\((.*@.*)\)/
let ariaLabel = el.getAttribute('aria-label');
if (pattern.test(ariaLabel)) {
const email = pattern.exec(ariaLabel)[1]
// console.log(`Logged in email is : ${email}`);
resolve(email);
}
});
} catch (error) {
reject(error);
}
});
}
getLoggedInEmail().then(email =>console.log(`Logged in email is : ${email}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment