Gmail.js
Gmail JavaScript API
Install / Use
/learn @KartikTalwar/Gmail.jsREADME
Gmail.js - JavaScript API for Gmail
What Gmail.js is and isn't
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
TL;DR Summary
- Lots of API methods to work with gmail. See documentation below.
- Easy to use API. Data & DOM.
- Reasonably complete TypeScript-support.
- Compatible with both WebExtension Manifest V2 and V3.
- Many methods are contextual and will work with whatever is on screen when no arguments are given.
- Obtain email data, react to event, etc. No need for OAuth!
- Main methods allow you to observe certain events with
gmail.observe.on('lots_of_actions_here', callback())or similargmail.observe.before(...)andgmail.observe.after(...) - Create an issue/pull request for feedback, requests and fixes. See CONTRIBUTING.md for more details.
Using Gmail.js
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:
- GmailJS Node Boilerplate - Example for how to create a browser-extension using GmailJS and modern javascript with NodeJS and script-bundling for instant load-times.
Typescript
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";
Methods
Summary (click for more info)
GET
- gmail.get.user_email()
- gmail.get.manager_email()
- gmail.get.current_page()
- gmail.get.new.email_id()
- gmail.get.new.email_data()
- gmail.get.new.thread_id()
- gmail.get.new.thread_data()
- gmail.get.email_subject()
- gmail.get.compose_ids()
- gmail.get.email_source_async(identifier=undefined, callback, error_callback, preferBinary)
- gmail.get.email_source_promise(identifier=undefined, preferBinary)
- gmail.get.search_query()
- gmail.get.unread_emails()
- gmail.get.last_active()
- gmail.get.storage_info()
- gmail.get.loggedin_accounts()
- gmail.get.beta()
- gmail.get.localization()
GET (deprecated methods)
- gmail.get.thread_id()
- gmail.get.email_id()
- gmail.get.email_ids()
- gmail.get.email_data(email_id=undefined)
- gmail.get.email_data_async(email_id=undefined, callback)
- gmail.get.displayed_email_data()
- gmail.get.displayed_email_data_async(callback)
- gmail.get.selected_emails_data()
- gmail.get.visible_emails()
- gmail.get.visible_emails_async(callback)
CHECK
- gmail.check.is_new_data_layer()
- gmail.check.is_new_gui()
- gmail.check.is_thread()
- gmail.check.is_inside_email()
- gmail.check.is_plain_text()
- gmail.check.is_preview_pane()
- gmail.check.is_multiple_inbox()
- gmail.check.is_horizontal_split()
- gmail.check.are_shortcuts_enabled()
- gmail.check.is_vertical_split()
- gmail.check.is_tabbed_inbox()
- gmail.check.is_right_side_chat()
- gmail.check.should_compose_fullscreen()
- gmail.check.is_conversation_view()
- gmail.check.is_google_apps_user()
- gmail.check.is_priority_inbox()
- gmail.check.is_rapportive_installed()
- gmail.check.is_streak_installed()
- gmail.check.is_anydo_installed()
- gmail.check.is_boomerang_installed()
- gmail.check.is_xobni_installed()
- gmail.check.is_signal_installed()
CHAT
COMPOSE
OBSERVE
- gmail.observe.http_requests()
- gmail.observe.actions()
- gmail.observe.register(action, class/args, parent) - registers a custom DOM observer
- gmail.observe.off(action,type)
- gmail.observe.on(action, callback)
- XHR observers
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 archivedmove_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 discardedsend_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 composed- DOM observers
compose- When a new compose window is
