Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

send_message events not working #601

Closed
aksswami opened this issue Dec 18, 2019 · 17 comments
Closed

send_message events not working #601

aksswami opened this issue Dec 18, 2019 · 17 comments

Comments

@aksswami
Copy link

I am trying to create some sample extension using the library. But send_message event don't seem to fire for 'on' and 'before' either.

Here is sample I am trying

console.log("Extension loading...");
require('bootstrap');
require('bootstrap/dist/css/bootstrap.css');
const jQuery = require("jquery");
const $ = jQuery;
const GmailFactory = require("gmail");
const gmail = new GmailFactory.Gmail($);
window.gmail = gmail;

gmail.observe.on("load", () => {
    const userEmail = gmail.get.user_email();
    console.log("Hello, " + userEmail + ". This is your extension talking!");

    gmail.observe.on("view_email", (domEmail) => {
        console.log("Looking at email:", domEmail);
        const emailData = gmail.new.get.email_data(domEmail);
        console.log("Email data:", emailData);
    });

    var isComposing = false;
    var isEnabled = false;
    gmail.observe.on("compose", (compose, composeType) => {
        if (composeType !== "compose") {
            console.log("Reply or Forward type email");
        }

        console.log("Composing Email", compose);
        isComposing = true;

        var composeBody = compose.dom("subjectbox");
        // insert a new element into the menu
        $(`<div class="custom-control custom-checkbox">
            <input type="checkbox" class="custom-control-input" id="customCheckDisabled1">
            <label class="custom-control-label" for="customCheckDisabled1">My Button</label>
        </div>`)
            .insertAfter(composeBody);
        
        $('.customCheckDisabled1').prop('indeterminate', true);

        $('#customCheckDisabled1').change(function() {
            isEnabled = true;
        });
    });

    gmail.observe.on("recipient_change", (domRecipients) => {
        console.log("Recipient Change", domRecipients);
    });

    gmail.observe.on("upload_attachment")

    gmail.observe.before('send_message', function (url, body, data, xhr) {
        console.log("Before send --> ", body);
        var body_params = xhr.xhrParams.body_params;
      
        // lets cc this email to someone extra if the subject is 'Fake example'
        if(isEnabled) {
          body_params.cc.push('test@test.com');
        }
      
        // now change the subject
        body_params.subject = 'Added!! '+body_params.subject;
        console.log("sending message, url:", url, 'body', body, 'email_data', data, 'xhr', xhr);
    });

    gmail.observe.on("send_message", function (url, body, data, xhr) {
        console.log("Send tapped send --> ", body);
        var body_params = xhr.xhrParams.body_params;
      
        // lets cc this email to someone extra if the subject is 'Fake example'
        if (isEnabled) {
            body_params.cc.push('test@test.com');
        }
      
        // now change the subject
        body_params.subject = 'ADDED!! ' + body_params.subject;
        console.log("sending message, url:", url, 'body', body, 'email_data', data, 'xhr', xhr);
    });
});
@josteink
Copy link
Collaborator

That is correct. After Google launched new Gmail, we've only been able to semi-reliably support the observe.after("send_message", ...)-event.

@aksswami
Copy link
Author

Maybe we should create a list of events which are working with new Gmail UI, so that these kind of similar questions can be avoided.

@josteink
Copy link
Collaborator

That’s a good idea. We should definitely update our documentation.

Not sure when I’ll have time, but I do agree this needs to be done.

@josteink
Copy link
Collaborator

josteink commented Dec 22, 2019

I think this function here is pretty indicative of what has been implemented:

gmail.js/src/gmail.js

Lines 1118 to 1153 in f5f5474

api.tools.check_event_type = function(threadObj) {
const apply_label = "^x_";
const action_map = {
// "" : "add_to_tasks",
"^a": "archive",
"^k": "delete",
// "" : "delete_message_in_thread",
// "" : "delete_forever",
// "" : "delete_label",
// "" : "discard_draft",
// "" : "expand_categories",
// "" : "filter_messages_like_these",
"^x_" : "label",
// "^io_im^imi": "mark_as_important",
// "^imn": "mark_as_not_important",
// "" : "mark_as_not_spam",
// "" : "mark_as_spam",
// "" : "move_label",
// "" : "move_to_inbox",
// "" : "mute",
"^u^us": "read",
// "" : "save_draft",
// "" : "send_message",
// "" : "show_newly_arrived_message",
// "^t^ss_sy": "star",
// "" : "undo_send",
// "" : "unmute",
"^u" : "unread",
// "^t^ss_sy^ss_so^ss_sr^ss_sp^ss_sb^ss_sg^ss_cr^ss_co^ss_cy^ss_cg^ss_cb^ss_cp": "unstar",
"^us" : "new_email",
// "" : "poll",
// "" : "refresh",
// "" : "restore_message_in_thread",
"^o": "open_email",
// "" : "toggle_threads"
};

If anyone wants to take a stab at this, PRs are definitely welcome.

@aksswami
Copy link
Author

@josteink, Thanks for pointing out above code. Will try if I can resolve some of these and create PR.

@josteink
Copy link
Collaborator

Great! 🍾

That list may not be a complete, but it should be a start. If you check where this function is used, you should be able to find the rest of the events we support and how we detect them.

PRs are always welcome!

@yunhanw5
Copy link

yunhanw5 commented Sep 8, 2020

Hi there, has this issue been fixed? I need to do something after sending the email, how can I do that now? Thanks!

@josteink
Copy link
Collaborator

josteink commented Sep 9, 2020

The gmail.observe.after("send_message") event-handlers are working fine, so you should be able to use that.

It's .before() and .on() which is broken for the send_message event.

@yunhanw5
Copy link

yunhanw5 commented Sep 9, 2020

Thanks @josteink . I tried this gmail.observe.after("send_message", function(url, body, data, response, xhr) { console.log("message sent", "url:", url, 'body', body, 'email_data', data, 'response', response, 'xhr', xhr); }) but I don't see the console log.

@josteink
Copy link
Collaborator

josteink commented Sep 9, 2020

Have you tried waiting up to 2 minutes? (The default Gmail cancellalable send length)?

@yunhanw5
Copy link

yunhanw5 commented Sep 9, 2020

yes, I did wait for 2 minutes but still didn't see the console log.

@young-healer
Copy link

Hi there, could you please tell me, is after("send_message") was fixed? because I have waited around 30 minutes and nothing happens. Somebody know about this issue?

@josteink
Copy link
Collaborator

I'm using it in a production extension and it work for me.

Could it be an error in your extension implementation? You are using this from a injected script right, and not from the content-script? Are you seeing any errors in your web-console which could explain the event not firing correctly?

@young-healer
Copy link

I'm using it in the observe.on('load') function in my index.js file, where I'm also use recipient_change method and it works well.

@josteink
Copy link
Collaborator

Is index.js a content-script or an injected script?

@young-healer
Copy link

It's injected script.

@josteink
Copy link
Collaborator

Then I honestly don't know why it's not working, and I'm working blindly here. Right now I have nothing to go by, and from what I can tell nothing is broken in gmail.js, since things are working in my extensions.

If you can provide a minimal example of a extension (or just use the boilerplate-repo?) which demonstrates how this fails for you, I can test that and see if I can reproduce, but if not, I'm not sure there's much more I can do on my end.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants