KartikTalwar / gmail.js

Gmail JavaScript API
MIT License
3.74k stars 456 forks source link

Getting Unread Emails: Cannot read properties of undefined (reading 'title') #755

Open becomingmountains opened 1 year ago

becomingmountains commented 1 year ago

Calling gmail.get.unread_emails() gives me the following error:

gmail.js:649 Uncaught TypeError: Cannot read properties of undefined (reading 'title')
    at Gmail.api.helper.get.navigation_count (gmail.js:649:23)
    at Gmail.api.get.unread_inbox_emails (gmail.js:610:31)
    at Gmail.api.get.unread_emails (gmail.js:672:37)
    at <anonymous>:1:11

Getting this for all unread functions (gmail.get.unread_draft_emails(), gmail.get.unread_inbox_emails()). Calling inside

gmail.observe.on("load", () => { 
    const unreadEmails = gmail.get.unread_emails(); // Error here
    console.log(unreadEmails);
    ...
}

Browser: Brave Browser Version 1.46.144 Chromium: 108.0.5359.128 (Official Build) unknown (64-bit) Extension Version: 3 gmail-js version: ^1.1.1

josteink commented 1 year ago

I believe many of the "complicated" or specialized calls in the gmail.get-namespace used for "old Gmail" is currently broken, because Gmail may no longer be using the protocol they used to determine this.

I believe the best fix for that would be to create new, corresponding implementations in the gmail.new.get-namespace.

stevenirby commented 5 months ago

I went digging into this today. This breaks here:

    api.helper.get.navigation_count = function(i18nName) {
      const title = api.tools.i18n(i18nName);
      const dom = $("div[role=navigation]").find("[title*='" + title + "']");
      if (dom || dom.length > 0) {
        if (dom[0].title.indexOf(title) !== -1) {
          const value = parseInt(dom[0].attributes["aria-label"].value.replace(/[^0-9]/g, ""));
          if (!isNaN(value)) {
            return value;
          }
        }
      }
      return 0;
    };

This line no longer works: $("div[role=navigation]").find("[title*='" + title + "']")

I'm happy to write open a PR sometime this week to fix this. I don't know how or where to test this. Just looking at my Gmail inbox may not be enough.

josteink commented 5 months ago

Sounds good. If you can come up with a PR which works for you, I would be willing to test it :)

stevenirby commented 5 months ago

@josteink great! Thank you so much. I opened a PR here: https://github.com/KartikTalwar/gmail.js/pull/776