KartikTalwar / gmail.js

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

Fix the navigation_count function and selector #776

Closed stevenirby closed 4 months ago

stevenirby commented 5 months ago

This fixes two of the methods api.helper.get.navigation_count and api.get.storage_info. It returns a fair amount of data back:

{storage_info: 
{used: 14.32, total: 15, percent: 95},
unread_draft_emails: 152
unread_emails: {inbox: 882, drafts: 152, spam: 0, forum: 0, update: 0, …}
unread_forum_emails: 0
unread_inbox_emails: 882
unread_promotion_emails: 7
unread_social_emails: 0
unread_spam_emails: 0
unread_update_emails: 0}

I'm trying to target the aria labels and accessibility tags, as I think it's a better move to try to parse the page like https://testing-library.com/ library does testing. Looking at the page like a real user or a screen reader would, rather than targeting class names or such, which is a cat-and-mouse game.

I'm also doing console.warns when things aren't found for easier debugging. Throwing an error or console.error seemed a bit too much.

I'm totally open to any advice or feedback!

josteink commented 5 months ago

Ive given the PR a test-run on a few different accounts:

Results below.

Regular gmail.com:

Unread-fix confirmed working. Storage_info() doesn't work though. (Output is all zeros) Tested with English US locale.

OG, grandfathered "Google Apps" account.

Unread-fix confirmed working. Storage_info() doesn't work though. (Output is all zeros) Tested with Norwegian and English UK locale.

Paid, company GSuite account:

Unread-fix partially working. Better than before, but produces console-warnings for all counters not Inbox, and provides 0 as result, even for labels which has unreads. Storage_info() doesn't work though. (Output is all zeros)

josteink commented 5 months ago

I see storage_info() is implemented using a regexp on text values.

Below are the texts Ive seen for my accounts:

Based on the regexp used, Id say the reason for the failure is assuming "X of Y" format and assuming percentage at the end.

Maybe you can relax the conditions slightly?

stevenirby commented 5 months ago

@josteink thank you so much for testing!

One big issue is that a lot of data isn't in the DOM until the page is fully loaded. After gmail.observe.on('load', () => {}) is done. I believe the correct fix is to add more watchers to wait until more of the DOM is loaded?

Unread-fix partially working. Better than before, but produces console-warnings for all counters not Inbox, and provides 0 as result, even for labels which has unreads.

Do you think it should return undefined instead? I didn't want to break any scripts out there using this.

Based on the regexp used, Id say the reason for the failure is assuming "X of Y" format and assuming percentage at the end. Maybe you can relax the conditions slightly?

Will do.

josteink commented 5 months ago

What might help make things easier for you is to separate data-fetching from data-processing into 2 different functions.

That way you can unit-test the processing and just add my real world examples above as test-cases.

That may sound complicated at first, but I think it will help you solve this faster and better.