Gmail.js is meant to be used for creating WebExtension-based browser-extensions, for Chrome, Firefox and other compatible browsers.
It cannot be used server-side with Node, or from another web-app to interface with Gmail.
Note: This is not an official Gmail API, and isn't affiliated with Google.
Note: Gmail.js requires jQuery to work
gmail.observe.on('lots_of_actions_here', callback()) or similar gmail.observe.before(...) and gmail.observe.after(...)If you know how to create WebExtensions-based extensions for Firefox and Chrome, you can get started by pulling Gmail.js like this:
npm install gmail-js
Note: Please ensure that Gmail.js is injected into the regular DOM.
Content-scripts which launch injected script must be configured with "run_at": "document_start".
It's recommended to split injected script to have only gmail.js load first because the size of the injected script impacts the loading time. Gmail.js must be injected and loaded before Gmail loads embedded data.
Gmail.js does not work as a content-script.
For a ready to use example/boilerplate repo, look no further:
In earlier advice given w.r.t. deployment of GmailJS, where scripts were injected one by one, with cumbersome loading and probing mechanisms, CSP could be an problem causing your extension to fail if GmailJS was injected incorrectly.
If you use modern javascript and script-bundling in your extension (like in the boilerplate example), CSP will not interfere with loading of your extension, nor GmailJS.
If you have any issues with CSP, the general advice is to build your extension using script-bundling and eliminate the cause of the error all together.
While you may be able to make it work, legacy loading is no longer considered supported by GmailJS.
Using gmail-js with TypeScript is relatively easy, but if you use normal import syntax it
will fail. Instead you need to use require-syntax to load it:
const GmailFactory = require("gmail-js");
const gmail = new GmailFactory.Gmail() as Gmail;
// working on the gmail-object now provides type-safety.
You will also have to import the types somewhere, like in a file called types.d.ts in your project:
import "gmail-js";
load - When the gmail interface has finished loadinghttp_event - When gmail any CRUD operation happens on gmailpoll - When gmail automatically polls the server to check for new emails every few secondsnew_email - When a new email appears in the inboxopen_email - When an email is opened from the inbox viewrefresh - When you click the refresh buttonunread - When a conversation(s) is marked unreadread - When a conversation(s) is marked readdelete - When a conversation(s) is deleteddelete_message_in_thread - When a conversation(s) is deleted inside a threadmark_as_spam - When a conversation(s) is marked as spammark_as_not_spam - When a conversation(s) is unchecked as spamlabel - When a conversation(s) get applied a labelarchive - When a conversation(s) is archievedmove_to_inbox - When a conversation(s) is moved to the inboxdelete_forever - When a conversation(s) is deleted foreverstar - When a conversation(s) is starredunstar - When a conversation(s) is unstarredundo_send - When the Undo Send button is clicked after trying to send a new emailmark_as_important - When a conversation(s) is marked as importantmark_as_not_important - When a conversation(s) is marked as not importantfilter_messages_like_these - When a filter button is triggered for a conversationmute - When a conversation(s) is mutedunmute - When a conversation(s) is unmutedadd_to_tasks - When an item is added to google tasksmove_label - When a conversation(s) is moved to a label foldersave_draft - When a draft is saveddiscard_draft - When a draft is dicardedsend_message - When a message is sent (except scheduled messages)send_scheduled_message - When a message is scheduled for sending (but not actually sent)expand_categories - When a category is expanded from the left nav sidebarrestore_message_in_thread - When a deleted message is restored inside a threaddelete_label - When a label is deletedshow_newly_arrived_message - When inside an email and a new email arrives in the threadupload_attachment - When an attachment is being uploaded to an email being composedcompose - When a new compose window is opened, or a message is replied to or forwardedrecipient_change - When an email being written (either new compose, reply or forward) has its to, cc or bcc recipients updatedview_thread - When a conversation thread is opened to readview_email - When an individual email is loaded within a conversation thread.
It's worth noting this event is only triggered when the email is actually rendered in the DOM. Gmail tends to cache the rendered emails, so it should not be expected to fire reliably for every viewing of the same email. It will most likely fire once, for the initial and possibly only rendering.load_email_menu - When the dropdown menu next to the reply button is clickedgmail.observe.onThese methods return the DOM data itself
gmail.dom.compose objects representing any open compose windowsThese are some helper functions that the rest of the methods use. See source for input params
$ claude mcp add gmail.js \
-- python -m otcore.mcp_server <graph>