Gmail Check Mail Now shortcut for External Accounts
Asked Answered
C

3

13

On https://mail.google.com/mail/u/0/#settings/accounts there is an option to Check email for other accounts. Normally Gmail checks those accounts for new mail every 60 minutes. With the 'Check mail now' button you can manually trigger this.

Does anyone know if this button can be triggered via a url / shortcut / api? I would like to create a button/script for myself to push when I want to check for new mail. screenshot of gmail accounts settings

Caprifoliaceous answered 27/3, 2020 at 21:31 Comment(0)
D
6

I solve it from this link: tutorial link

Now I have an Bookmarklet with a javascript command that does automatically the 4 clicks to reach the "Check Email Now".

Hope that it help you too.

This is the code in javascript:

 (function () { const gmailWindow = window;
 if(gmailWindow.location.href.indexOf("https://mail.google.com/") === -1){ 
   alert('É preciso estar no Gmail para utilizar este recurso.');
   return;
  } 
gmailWindow.location.assign('https://mail.google.com/mail/u/0/#settings/accounts');
 const xpath = "//span[text()='Ver correio agora']";
 const refreshAccounts = () => { const selectedNodeElements = gmailWindow.document.evaluate(xpath, gmailWindow.document, null, XPathResult.ANY_TYPE, null);
    let currentNode = selectedNodeElements.iterateNext();
if (currentNode === null) {
       setTimeout(refreshAccounts, 100); 
} else { 
    while (currentNode) { currentNode.click(); currentNode = selectedNodeElements.iterateNext();

 }; 

gmailWindow.location.assign('https://mail.google.com/mail/u/0/#inbox'); }; }; 
setTimeout(refreshAccounts, 100); })();
Durning answered 12/5, 2022 at 14:25 Comment(1)
Thx! Sorry for answering late, didn't check stackoverflow for a while. The link in your answer is really helpfull and it does the job.Caprifoliaceous
D
0

The below code will check on any gmail tab. Be it user 0 1 2

javascript:(function() { const gmailWindow = window; if (gmailWindow.location.href.indexOf("https://mail.google.com/") === -1) { alert('You have to run the bookmarklet from a Gmail window'); return; } const accountNumberMatch = gmailWindow.location.href.match(/\/u\/(\d+)\//); if (!accountNumberMatch) { alert('Unable to determine the account number'); return; } const accountNumber = accountNumberMatch[1]; const settingsUrl = `https://mail.google.com/mail/u/${accountNumber}/#settings/accounts%60; const inboxUrl = %60https://mail.google.com/mail/u/${accountNumber}/#inbox%60; gmailWindow.location.assign(settingsUrl); const xpath = "//span[text()='Check mail now']"; const refreshAccounts = () => { const selectedNodeElements = gmailWindow.document.evaluate(xpath, gmailWindow.document, null, XPathResult.ANY_TYPE, null); let currentNode = selectedNodeElements.iterateNext(); const clickWithDelay = () => { const clickPromises = []; while (currentNode) { const promise = new Promise((resolve) => { currentNode.click(); resolve(); }); clickPromises.push(promise); currentNode = selectedNodeElements.iterateNext(); } Promise.all(clickPromises) .then(() => { setTimeout(() => { gmailWindow.location.assign(inboxUrl); }, 4000); }); }; setTimeout(clickWithDelay, 0); }; setTimeout(refreshAccounts, 1000); })();
Disposed answered 21/1 at 10:25 Comment(1)
Consider adding some line-breaks to your code.Fruitarian
R
0

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);
})();
Rosemare answered 19/2 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.