KartikTalwar / gmail.js

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

send_message event stopped working #722

Closed huksley closed 1 year ago

huksley commented 1 year ago

It is still something to be confirmed, but I can see that both send_message and send_scheduled_message does not work anymore.

These hooks are now never triggered:

this.gmail.observe.before("send_message", (url: string, body: string, data: Mail) => {});
this.gmail.observe.before("send_scheduled_message", (url: string, _body: string, data: Mail) => {});

Browser differences

I can see there is different parameters passed in URL - Chrome v105

image

Firefox v104

image

I am trying to investigate if it is payload or something...

huksley commented 1 year ago

I can see the payloads send in Chrome vs Firefox are different

image
MykhailoDev commented 1 year ago

have same problem

josteink commented 1 year ago

This may be a duplicate of https://github.com/KartikTalwar/gmail.js/issues/721

Line format for XHR seems to have changed slightly.

huksley commented 1 year ago

I decoded payload of msg I think it looks different from #721, not sure?

interface RequestBodyMail {
  0: string; // messageId,
  1: PayloadAddress; // from
  2: PayloadAddress[]; // to
  3: PayloadAddress[]; // cc
  4: PayloadAddress[]; // bcc
  6: number; // timestamp
  7: string; // subject
  8?: {
    1?: {
      0: number;
      1: string; // body piece
    }[];
  }; // body structure
  10: string[]; // smart labels
}
josteink commented 1 year ago

@huksley : If you can create a PR for the event-stuff, I'll be happy to review that and create another release.

I must admit that when it comes to the events themselves, I don't feel that confident in the parsing code as the email-parser (which I pretty much wrote from scratch myself).

MykhailoDev commented 1 year ago

@huksley are you trying to solve the problem?

huksley commented 1 year ago

Fixed, but need to have both code paths tested - I can't because my Firefox also started working with new APIs

josteink commented 1 year ago

Not seeing this feedback, I've created a patch which I think fixes this issue. That is now published as v1.1.2 on npmjs.

If you still think there's more work which needs to be done, please do let me know :smile:

MykhailoDev commented 1 year ago

@josteink perfect, it work!

huksley commented 1 year ago

Some users reports that older algorithm is back and older version of my extension works fine...

huksley commented 1 year ago

@josteink I see you've made your own changes, btw these might not work:

        const sent_email_content_html = api.tools.parse_sent_message_html_payload(sent_email);
        const sent_attachments = api.tools.parse_sent_message_attachments(sent_email["11"]);

Because HTML payload format and attachments in a different format now.

josteink commented 1 year ago

Some users reports that older algorithm is back and older version of my extension works fine...

Google please make up your mind 😮

huksley commented 1 year ago

I think it is a rollout algorithm or something..

josteink commented 1 year ago

@josteink I see you've made your own changes, btw these might now work:

        const sent_email_content_html = api.tools.parse_sent_message_html_payload(sent_email);

        const sent_attachments = api.tools.parse_sent_message_attachments(sent_email["11"]);

Because HTML payload format and attachments in a different format now.

Do you mean may NOT work?

If so, PRs for getting them fixed are welcome 😉

huksley commented 1 year ago

https://github.com/KartikTalwar/gmail.js/issues/722

josteink commented 1 year ago

Released v1.1.3 with @huksley's fixes too.

MykhailoDev commented 1 year ago

@josteink is it work for both format?

josteink commented 1 year ago

Only for new formats.

huksley commented 1 year ago

I would think we need to support both versions for now.

josteink commented 1 year ago

Then we should probably introduce some parser class/interface to do the parsing to clean up the code somewhat.

Something like this?

interface IGmailParser {
    canParse(obj): boolean;
    parse(obj): array;
}

The code we have now is starting to have a somewhat desperate need for at least a minimal amount of architecture.

As for both versions… are you talking about the events only? Or also the data APIs?

huksley commented 1 year ago

I added the is_email_new() condition, and it triggers old and new parser as needed. But since older code also checks now for new email format, it probably will not work with older APIs...

Then we should probably introduce some parser class/interface to do the parsing to clean up the code somewhat.

I would think there should be a parsers (or handlers) for different functionality of Gmail, versioned. And users can provide flags which enable (or disable specific handlers) - this will be perfect for MW3 and future versions.

interface GmailHandler {
   enable(gmail: GmailJS): void
} 

// Parses properly when you press Send in compose window (new format of send payloads)
class GmailSendPayloadHandler20220913 extends GmailHandler {
}

enabled = ["GmailNewHandler"]
handlers = [new GmailNewHandler(), new GmailSendPayloadHandler20220913()].filter(h => enabled.includes(h.constructor.name)
handlers.forEach(handler => handler.enable(this))
onestep commented 1 year ago

Hello! It seems that parse_sent_message_payload is fixed as well now, and parse_sent_message_payload_new has incorrect subject field index (should be 7, but there is 8).

I believe that parse_sent_message_payload_new could be removed in favor of recently updated parse_sent_message_payload.

UPD: just discovered that incorrect index was fixed in #726. 🙂 But I still think that removing duplicated code is good, as long as parse_send_message_payload was fixed in 9d4ff11327df06363df3576c12368d4e762b7017.