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
}
}));