Create a bookmarklet with the following javascript:
”URI“:
javascript: (function() { const checkMailStr = 'Check mail now'; const settings = { waitUntilDone: false, goToInbox: false }; const errMessages = { nonGmailWindow: 'You have to run the bookmarklet from a Gmail window.', strNotFound: `Could not find the string "${checkMailStr}".` }; const gmailWindow = window; if (gmailWindow.location.href.indexOf('https://mail.google.com/') === -1) { alert(errMessages.nonGmailWindow); return; } const url = gmailWindow.location.href; const inboxUrl = url.split('#')[0] + '#inbox'; const settingsUrl = url.split('#')[0] + '#settings/accounts'; function maybeChangeLocation() { const targetUrl = (settings.goToInbox || !url.includes('#')) ? inboxUrl : url; if (targetUrl === settingsUrl) { return; } if (targetUrl === url) { history.back(); } else { gmailWindow.location.assign(targetUrl); } } function getSelectedNodeElements() { const xpath = `//span[text()='${checkMailStr}']`; return gmailWindow.document.evaluate(xpath, gmailWindow.document, null, XPathResult.ANY_TYPE, null); } if (url !== settingsUrl) { gmailWindow.location.assign(settingsUrl); } let maxRemainingTimeouts = 30; let nAccounts = 0; const refreshAccounts = () => { const selectedNodeElements = getSelectedNodeElements(); let currentNode = selectedNodeElements.iterateNext(); if (currentNode === null) { if (maxRemainingTimeouts > 0) { setTimeout(refreshAccounts, 100); --maxRemainingTimeouts; } else { alert(errMessages.strNotFound); } } else { currentNode.scrollIntoView(); while (currentNode) { nAccounts++; currentNode.click(); currentNode = selectedNodeElements.iterateNext(); } if (settings.waitUntilDone) { maxRemainingTimeouts = 200; setTimeout(waitUntilDone, 1000); } else { setTimeout(maybeChangeLocation, 0); } } }; let retrievingNotYetStarted = true; const waitUntilDone = () => { const selectedNodeElements = getSelectedNodeElements(); let n = 0; while (selectedNodeElements.iterateNext()) { ++n; } if (retrievingNotYetStarted) { if (n < nAccounts) { retrievingNotYetStarted = false; } } else if (n >= nAccounts) { setTimeout(maybeChangeLocation, 1000); return; } if (maxRemainingTimeouts > 0) { setTimeout(waitUntilDone, 100); --maxRemainingTimeouts; } }; setTimeout(refreshAccounts, 100); })();
If your Gmail language isn't english, change the string Check mail now
at the beginning.
There are also some settings right after the Check mail now
string:
waitUntilDone
: Set this to true
if you want the script to wait until Gmail finished checking your mail before navigating back or to the inbox.
goToInbox
: Set this to true
if you want the script to navigate to your inbox instead of going back (to where you were before).
Additional features of this script, compared to the original bookmarklet:
- works with non-zero Gmail account numbers (
https://mail.google.com/mail/u/${accountNumber}/
)
- works with trailing ”search“ parts inside the URL (
?...
), e.g. ?shva=1
- it scrolls down to the
Check mail now
part of the settings so you can watch the links being clicked
- avoid infinite loops if the
Check mail now
string is not found
This is the human-readable code inside the bookmarklet:
(function() {
const checkMailStr = 'Check mail now';
const settings = {
waitUntilDone: false,
goToInbox: false
};
const errMessages = {
nonGmailWindow: 'You have to run the bookmarklet from a Gmail window.',
strNotFound: `Could not find the string "${checkMailStr}".`
};
const gmailWindow = window;
if (gmailWindow.location.href.indexOf('https://mail.google.com/') === -1) {
alert(errMessages.nonGmailWindow);
return;
}
const url = gmailWindow.location.href;
const inboxUrl = url.split('#')[0] + '#inbox';
const settingsUrl = url.split('#')[0] + '#settings/accounts';
function maybeChangeLocation() {
const targetUrl = (settings.goToInbox || !url.includes('#')) ? inboxUrl : url;
if (targetUrl === settingsUrl) {
return;
}
if (targetUrl === url) {
history.back();
} else {
gmailWindow.location.assign(targetUrl);
}
}
function getSelectedNodeElements() {
const xpath = `//span[text()='${checkMailStr}']`;
return gmailWindow.document.evaluate(xpath, gmailWindow.document, null, XPathResult.ANY_TYPE, null);
}
if (url !== settingsUrl) {
gmailWindow.location.assign(settingsUrl);
}
let maxRemainingTimeouts = 30;
let nAccounts = 0;
const refreshAccounts = () => {
const selectedNodeElements = getSelectedNodeElements();
let currentNode = selectedNodeElements.iterateNext();
if (currentNode === null) {
if (maxRemainingTimeouts > 0) {
setTimeout(refreshAccounts, 100);
--maxRemainingTimeouts;
} else {
alert(errMessages.strNotFound);
}
} else {
currentNode.scrollIntoView();
while (currentNode) {
nAccounts++;
currentNode.click();
currentNode = selectedNodeElements.iterateNext();
}
if (settings.waitUntilDone) {
maxRemainingTimeouts = 200;
setTimeout(waitUntilDone, 1000);
} else {
setTimeout(maybeChangeLocation, 0);
}
}
};
let retrievingNotYetStarted = true;
const waitUntilDone = () => {
const selectedNodeElements = getSelectedNodeElements();
let n = 0;
while (selectedNodeElements.iterateNext()) {
++n;
}
if (retrievingNotYetStarted) {
if (n < nAccounts) {
retrievingNotYetStarted = false;
}
} else if (n >= nAccounts) {
setTimeout(maybeChangeLocation, 1000);
return;
}
if (maxRemainingTimeouts > 0) {
setTimeout(waitUntilDone, 100);
--maxRemainingTimeouts;
}
};
setTimeout(refreshAccounts, 100);
})();