InboxSDK Documentation

website·Indexed 30 days ago

https://inboxsdk.github.io/inboxsdk-docs/

A library for building Gmail extensions. It provides namespace methods and types to integrate custom UI elements into Gmail, including the Compose UI, conversations, toolbars, navigation menus, and search behavior. Key features include handlers for ThreadView and MessageView, custom routing, keyboard shortcuts, and native Gmail widgets like ModalView and DrawerView.

Tokens
20.7K
Snippets
110
Records
174
Agent score
72%

What's inside InboxSDK

  1. Overview of InboxSDK

    The InboxSDK is a library designed for building browser extensions that interact with and extend the Gmail UI. It allows multiple extensions to coexist on a single page without conflicts. It is the foundation for extensions like Streak, though it does not provide access to Streak-specific user data.
  2. Overview of Gmail Router namespace

    The Router namespace allows developers to define and manage 'Routes' to create custom content pages within Gmail. Routes define a full page of content and an associated URL space. You can create your own custom routes or listen for navigation to built-in Gmail routes (e.g., Inbox, Sent, Drafts). Route IDs are path-like strings that can include named parameters, such as "myroute/:someParamMyRouteNeeds". Because Gmail's internal URLs can change, you should never construct them manually; instead, use createLink or goto.
  3. Use RouteView to retrieve Gmail page metadata

    The RouteView class represents a page within Gmail. It can be a "custom" route registered by the developer or a "builtin" route (e.g., "Sent", "Drafts", or "Inbox"). It provides metadata about the current route and tracks its lifecycle via the destroyed property and destroy event.
    // Example usage of RouteView methods
    const routeId = routeView.getRouteID(); // Returns string (matches NativeRouteIDs)
    const routeType = routeView.getRouteType(); // Returns string (matches RouteTypes)
    const params = routeView.getParams(); // Returns Object of URL parameters
  4. Understand the MessageViewLinkDescriptor structure

    The MessageViewLinkDescriptor describes a link found within a message, providing metadata about its text, HTML representation, the actual DOM element, and whether it resides in a quoted area.
    interface MessageViewLinkDescriptor {
      text: string; // Anchor text
      html: string; // HTML string of the link
      element: HTMLElement; // The actual DOM element
      href: string; // The URL of the link
      isInQuotedArea: boolean; // True if link is in the quoted area
    }
  5. Extract data from a ThreadRowView

    Use the following ThreadRowView methods to retrieve information about a specific email thread row:

    • getSubject(): Returns the thread subject (String).
    • getDateString(): Returns the localized timestamp of the most recent message (String).
    • getThreadIDAsync(): Returns a Promise resolving to the Gmail Thread ID (String).
    • getThreadIDIfStableAsync(): Returns a Promise resolving to the Thread ID only if it is stable; otherwise returns null. Use this when using the ID as a key to avoid issues with Drafts.
    • getDraftID(): Returns a Promise resolving to the draft ID if the row is a single draft.
    • getVisibleDraftCount(): Returns the estimated number of visible draft messages (Number).
    • getVisibleMessageCount(): Returns the number of visible messages based on the numeric marker (Number).
    • getContacts(): Returns an Array<Contact> of visible contacts on the row.
    • getElement(): Returns the raw HTMLElement of the row for direct DOM access.
  6. Handle DrawerView auto-close events

    The DrawerView fires a preautoclose event when it is about to close due to a user clicking outside the drawer or pressing the Escape key. This event can be canceled to prevent the drawer from closing (e.g., to show a confirmation dialog). The event provides the type ('outsideInteraction' or 'escape') and the original DOM cause event.
    drawerView.addEventListener('preautoclose', (event) => {
      if (event.type === 'escape') {
        console.log('User pressed escape');
      }
      
      // Prevent the drawer from closing
      event.cancel();
    });
  7. Register a handler for Gmail Compose views

    Use sdk.Compose.registerComposeViewHandler(handler) to execute a function for every existing and future ComposeView (both new compose windows and inline reply widgets). This is the preferred method for adding application elements to all compose areas. The method returns a function that can be called to remove the registration.
    InboxSDK.load(1, 'YOUR_APP_ID_HERE').then(function(sdk){
      sdk.Compose.registerComposeViewHandler(function(composeView){
        console.log("compose view exists!");
      });
    });
  8. Use Reactive Streams with InboxSDK

    Some InboxSDK API methods accept reactive streams as arguments. A reactive stream represents a series of values that can resolve repeatedly over time. To use streams, you must include a compatible library in your extension. Supported libraries include Kefir, Bacon.js, and RxJS (Reactive Extensions for Javascript).
    // Example using the Kefir library
    var stream = Kefir.stream(function(emitter) {
        var timer = setInterval(function() {
        emitter.emit('foo');
        }, 1000);
    
        // Return a function to be called when the stream is unsubscribed from
        return function() {
        clearInterval(timer);
        };
    });
    
    stream.take(5).onValue(function(value) {
        console.log("received", value);
    });
    // Prints "received foo" five times each one second apart, and then stops.
  9. Handle asynchronous values with Promises in InboxSDK

    Several InboxSDK API methods use Promises to handle values that are not immediately available. Promises provide a consistent interface for writing asynchronous code and are natively supported in modern browsers.
    var promise = new Promise(function(resolve, reject) {
        setTimeout(function() {
        resolve('foo');
        }, 1000);
    });
    
    promise.then(function(value) {
        console.log("received", value);
    });
    // Prints "received foo" after one second.
  10. Use InboxSDK with Chrome MV3

    The InboxSDK is available on NPM in a Chrome MV3-compatible form. This version does not use remote code loading, making it compliant with MV3 security policies.
  11. Add a custom item to the Gmail App Menu

    Use AppMenu.addMenuItem(appMenuItemDescriptor) to add a custom item to the Gmail app menu panel. Because the AppMenu may not always be visible (it typically requires Chat or Meet to be enabled), you must call AppMenu.isShown() first to verify visibility. If insertOrder is provided in the descriptor, the item is placed at that index; otherwise, it is added after Gmail's default icons.
    if (AppMenu.isShown()) {
      const menuItemView = AppMenu.addMenuItem({
        name: 'My App Item',
        iconUrl: { lightTheme: 'https://example.com/icon.png' },
        onClick: () => console.log('Menu item clicked!')
      });
    }