Re: Adding labels to mail before sending

446 views
Skip to first unread message
Message has been deleted

Maximilian Paju

unread,
Oct 17, 2016, 6:31:52 AM10/17/16
to InboxSDK
Bump!

Surely there must be someone who's done this?



On Tuesday, October 4, 2016 at 6:56:59 PM UTC+2, Maximilian Paju wrote:
Hi guys!

Judging from the InboxSDK API documentation, I see no way to utilize it for adding a label to an email before sending it. So at the moment I'm looking into using the Gmail API to do that. Has anyone else used Gmail API with their extension, if so, is there a good way to do this? I actually noticed that the code is already loaded into the frontend inside Gmail - however I am not a JavaScript god and have yet to find a way to break outside the scope of my extension... 

Any help on this would be most appreciated! 

Max

Omar Ismail

unread,
Oct 17, 2016, 1:54:00 PM10/17/16
to Maximilian Paju, InboxSDK
Are you looking for help 'breaking out of extension scope' or with using Gmail API?

--
You received this message because you are subscribed to the Google Groups "InboxSDK" group.
To unsubscribe from this group and stop receiving emails from it, send an email to inboxsdk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/inboxsdk/92537eb5-2285-43c0-b1cf-2c91f9fa2b3e%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Maximilian Paju

unread,
Oct 17, 2016, 1:56:37 PM10/17/16
to InboxSDK, maximil...@gmail.com
Sorry, not very well formulate from my part.

My idea is that I want to use Gmail API inside my extension to add labels to the emails that are sent. However, I have issues using the Gmail API JavaScript client inside the InboxSDK scope. Do you guys at Streak have any examples with using Gmail API inside the extension?


On Monday, October 17, 2016 at 7:54:00 PM UTC+2, Omar Ismail wrote:
Are you looking for help 'breaking out of extension scope' or with using Gmail API?
On Mon, Oct 17, 2016 at 3:31 AM, Maximilian Paju <maximil...@gmail.com> wrote:
Bump!

Surely there must be someone who's done this?



On Tuesday, October 4, 2016 at 6:56:59 PM UTC+2, Maximilian Paju wrote:
Hi guys!

Judging from the InboxSDK API documentation, I see no way to utilize it for adding a label to an email before sending it. So at the moment I'm looking into using the Gmail API to do that. Has anyone else used Gmail API with their extension, if so, is there a good way to do this? I actually noticed that the code is already loaded into the frontend inside Gmail - however I am not a JavaScript god and have yet to find a way to break outside the scope of my extension... 

Any help on this would be most appreciated! 

Max

--
You received this message because you are subscribed to the Google Groups "InboxSDK" group.
To unsubscribe from this group and stop receiving emails from it, send an email to inboxsdk+u...@googlegroups.com.

Chris Cowan

unread,
Oct 17, 2016, 2:07:06 PM10/17/16
to InboxSDK
The InboxSDK mostly operates on the UI, and in general can only make the same modifications to the user's data that the user could. There's no way in the Gmail UI as a user to label a message as it's being sent, so neither can the InboxSDK.
You can use the InboxSDK to retrieve the sent message's new message id, which you can then pass off to the Gmail API:

InboxSDK.load(1, 'appidhere').then(sdk => {
  inboxSDK.Compose.getComposeView().then(composeView => {
    composeView.on('sent', event => {
      console.log('messageID', event.messageID);
      // ...
    });
  });
});



A lot of Google javascript libraries are really painful to use within extensions because they load by injecting script tags which load in the page's world instead of the extension's isolated world. You have several options:
* Talk to your own server instead which uses the Gmail API itself. That's what we do at Streak.
* Make the requests to the Gmail API yourself without using Google's javascript library. The only real pain-point is dealing with the auth yourself, which I don't have much experience with doing with the Gmail API.
* Inject code into the page's world and use the gapi global from there. You can inject code into the page's world by creating a script element, setting its text property to some code, appending it to `document.head`, and then removing it. The easiest way is to get the javascript to inject from a request:

fetch('injected.js')
  .then(response => {
    if (response.ok) {
      return response.text();
    } else {
      throw new Error(response.statusText);
    }
  })
  .then(text => {
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.text = text;
    document.head.appendChild(script).remove();
  })
  .catch(err => {
    console.error(err);
  });


Then in the injected script you can refer to the gapi object. Try to keep as little logic in the page-injected script as possible; it's shared with the page (and other extensions that are injecting code into the page world) so it's possible that global variables are set to values you don't expect. Try to avoid setting or depending on non-browser and non-Gmail global variables. You can communicate between the extension script and the page-injected script through DOM CustomEvents. Put a listener in the page-injected script:

document.addEventListener('gmailSetLabel', event => {
  console.log('messageID', event.detail.messageID);
  // gapi stuff ...
});


and you can generate the events from the extension script like this:


document.dispatchEvent(new CustomEvent('gmailSetLabel', {
  bubbles: false, cancelable: false,
  detail: {
    messageID: messageID
  }
}));

Omar Ismail

unread,
Oct 17, 2016, 2:20:06 PM10/17/16
to Chris Cowan, InboxSDK
Instead of using an injected script my suggestions is to use a background page.

You can use the Gmail API from a background script and it's not a pain at all. There's also a built in communication mechanism between extension content scripts and background pages. Lastly, Chrome has built in Oauth solution that you can use.

--
You received this message because you are subscribed to the Google Groups "InboxSDK" group.
To unsubscribe from this group and stop receiving emails from it, send an email to inboxsdk+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/inboxsdk/e4922a5d-3480-446a-9759-07c43b3b9046%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages