Being implemented behind the --enable-experimental-extension-apis flag so we can collect developer feedback. Should be landing in the tree relatively soon.

Overview

Provide a way for extensions to add items to the context (aka "right click") menu for web pages.

Use cases

Many features that extensions want to expose are only applicable to certain kinds of content. Some examples include saving selected text or link urls to a extension-local or web based notepad, removing particular images from a page, filling in form fields, and looking up words in an online dictionary.

Could this API be part of the web platform?

Probably not, since many actions people would like to add are logically "browser-level" functionality.

Do you expect this API to be fairly stable?

Yes, once we get some experience with an experimental version.

What UI does this API expose?

New menu items & submenus in context menus.

How could this API be abused?

Someone could create menu items with innocuous-sounding or misleading names (or even duplicates of built-in menu items) to trick users to click on them.

How would you implement your desired features if this API didn't exist?

You could (and perhaps some extensions do?) inject script into all pages to intercept onmousedown and create your own custom html menu implementation. But that is a terrible user experience since it prevents the browser-native menu from appearing.

Draft API spec

Initially these functions will only be callable from an extension's background page. This will likely not be sufficient, since a frequent use case for context menus is to operate on the specific link/image/etc. that the menu was activated on. See the "Future Improvements" section below for ideas about this.

Note: because this will be initially available as an experiment, the API methods will at first be chrome.experimental.contextMenu.methodname

Creates a new context menu item. The onCreatedCallback function should have a signature like function(id) {}, and will be passed the integer id of the newly created menu item.

The properties parameter can contain:

* title (optional string) - Text for this menu item. This is required for all types except 'SEPARATOR'. The special string %s in a title will be replaced with the currently selected text, if any.

* type (optional string) - One of the strings 'NORMAL', 'CHECKBOX', 'RADIO', or 'SEPARATOR'. Defaults to 'NORMAL' if not specified.

* checked (optional boolean) - For items of type CHECKBOX or RADIO, should this be selected (RADIO) or checked (CHECKBOX)? Only one RADIO item can be selected at a time in a given group of RADIO items, with the last one to have checked == true winning.

* contexts (string) - Which contexts should this menu item appear for? An array of one or more of the following strings:

Defaults to PAGE, which means "none of the rest" are selected (no link, no image, etc.). If 'ALL' is in the array, the item will appear in all contexts regardless of the rest of the items in the array.

* enabledContexts (string) - An array of strings similar to the contexts property. This serves to limit the contexts where this item will be enabled (ie not greyed out). If omitted, it defaults to the same set as the contexts property.

* parentId (integer) - If this should be a child item of another item, this is the id

* onclick (function(object info, Tab tab)) - A function that will be called when this menu item is clicked on. The info parameter is an object containing the following properties:

The following code would live in your background page, and you would need to have an entry in the permissions section of your manifest which allowed you to run chrome.tabs.executeScript on the page where the image had its context menu activated. This example highlights one of the limitations of the initial proposal, which is that you don't have any way to determine the actual unique node that was clicked on, so it removes any instances of the image from the page.

- Add a removeAll() method so you don't have to keep track of id's if you don't want to.

-Add a way to limit your menu items to specific tabs or frame urls (by a given match pattern, or perhaps automatically limit to only sites you have content script permissions for)

-Some people have asked for a onBeforeShow() event to fire before the menu items are displayed, but this may be infeasible given chrome's multiprocess architecture and desire to keep the UI very responsive.