chrome.contextMenus

Description: Use the chrome.contextMenus API to add items to Google Chrome's context menu. You can choose what types of objects your context menu additions apply to, such as images, hyperlinks, and pages.
Availability: Since Chrome 6.
Permissions: "contextMenus"

Usage

Context menu items can appear in any document (or frame within a document), even those with file:// or chrome:// URLs. To control which documents your items can appear in, specify the documentUrlPatterns field when you call the create() or update() method.

You can create as many context menu items as you need, but if more than one from your extension is visible at once, Google Chrome automatically collapses them into a single parent menu.

Manifest

You must declare the "contextMenus" permission in your extension's manifest to use the API. Also, you should specify a 16x16-pixel icon for display next to your menu item. For example:

      {
        "name": "My extension",
        ...
        "permissions": [
          "contextMenus"
        ],
        "icons": {
          "16": "icon-bitty.png",
          "48": "icon-small.png",
          "128": "icon-large.png"
        },
        ...
      }
      

Examples

You can find samples of this API on the sample page.

Summary

Types
ContextType
ItemType
Properties
ACTION_MENU_TOP_LEVEL_LIMIT
Methods
create integer or string chrome.contextMenus.create(object createProperties, function callback)
update chrome.contextMenus.update(integer or string id, object updateProperties, function callback)
remove chrome.contextMenus.remove(integer or string menuItemId, function callback)
removeAll chrome.contextMenus.removeAll(function callback)
Events
onClicked

Types

ContextType

The different contexts a menu can appear in. Specifying 'all' is equivalent to the combination of all other contexts except for 'launcher'. The 'launcher' context is only supported by apps and is used to add menu items to the context menu that appears when clicking on the app icon in the launcher/taskbar/dock/etc. Different platforms might put limitations on what is actually supported in a launcher context menu.
Enum
"all", "page", "frame", "selection", "link", "editable", "image", "video", "audio", "launcher", "browser_action", or "page_action"

ItemType

The type of menu item.
Enum
"normal", "checkbox", "radio", or "separator"

Properties

6 chrome.contextMenus.ACTION_MENU_TOP_LEVEL_LIMIT

Since Chrome 38.

The maximum number of top level extension items that can be added to an extension action context menu. Any items beyond this limit will be ignored.

Methods

create

integer or string chrome.contextMenus.create(object createProperties, function callback)

Creates a new context menu item. Note that if an error occurs during creation, you may not find out until the creation callback fires (the details will be in chrome.runtime.lastError).

Parameters
object createProperties
ItemType (optional) type

The type of menu item. Defaults to 'normal' if not specified.

string (optional) id

Since Chrome 21.

The unique ID to assign to this item. Mandatory for event pages. Cannot be the same as another ID for this extension.

string (optional) title

The text to be displayed in the item; this is required unless type is 'separator'. When the context is 'selection', you can use %s within the string to show the selected text. For example, if this parameter's value is "Translate '%s' to Pig Latin" and the user selects the word "cool", the context menu item for the selection is "Translate 'cool' to Pig Latin".

boolean (optional) checked

The initial state of a checkbox or radio item: true for selected and false for unselected. Only one radio item can be selected at a time in a given group of radio items.

array of ContextType (optional) contexts

List of contexts this menu item will appear in. Defaults to ['page'] if not specified.

function (optional) onclick

A function that will be called back when the menu item is clicked. Event pages cannot use this; instead, they should register a listener for chrome.contextMenus.onClicked.

Parameters
object info

Information sent when a context menu item is clicked.

integer or string menuItemId

Since Chrome 35.

The ID of the menu item that was clicked.

integer or string (optional) parentMenuItemId

Since Chrome 35.

The parent ID, if any, for the item clicked.

string (optional) mediaType

Since Chrome 35.

One of 'image', 'video', or 'audio' if the context menu was activated on one of these types of elements.

string (optional) linkUrl

Since Chrome 35.

If the element is a link, the URL it points to.

string (optional) srcUrl

Since Chrome 35.

Will be present for elements with a 'src' URL.

string (optional) pageUrl

Since Chrome 35.

The URL of the page where the menu item was clicked. This property is not set if the click occured in a context where there is no current page, such as in a launcher context menu.

string (optional) frameUrl

Since Chrome 35.

The URL of the frame of the element where the context menu was clicked, if it was in a frame.

string (optional) selectionText

Since Chrome 35.

The text for the context selection, if any.

boolean editable

Since Chrome 35.

A flag indicating whether the element is editable (text input, textarea, etc.).

boolean (optional) wasChecked

Since Chrome 35.

A flag indicating the state of a checkbox or radio item before it was clicked.

boolean (optional) checked

Since Chrome 35.

A flag indicating the state of a checkbox or radio item after it is clicked.

tabs.Tab tab

The details of the tab where the click took place. Note: this parameter only present for extensions.

integer or string (optional) parentId

The ID of a parent menu item; this makes the item a child of a previously added item.

array of string (optional) documentUrlPatterns

Lets you restrict the item to apply only to documents whose URL matches one of the given patterns. (This applies to frames as well.) For details on the format of a pattern, see Match Patterns.

array of string (optional) targetUrlPatterns

Similar to documentUrlPatterns, but lets you filter based on the src attribute of img/audio/video tags and the href of anchor tags.

boolean (optional) enabled

Since Chrome 20.

Whether this context menu item is enabled or disabled. Defaults to true.

function (optional) callback

Called when the item has been created in the browser. If there were any problems creating the item, details will be available in chrome.runtime.lastError.

If you specify the callback parameter, it should be a function that looks like this:

function() {...};

update

chrome.contextMenus.update(integer or string id, object updateProperties, function callback)

Updates a previously created context menu item.

Parameters
integer or string id

The ID of the item to update.

object updateProperties

The properties to update. Accepts the same values as the create function.

ItemType (optional) type
string (optional) title
boolean (optional) checked
array of ContextType (optional) contexts
function (optional) onclick
Parameters
object info

Since Chrome 44. Warning: this is the current Dev channel. Learn more.

Information sent when a context menu item is clicked.

integer or string menuItemId

The ID of the menu item that was clicked.

integer or string (optional) parentMenuItemId

The parent ID, if any, for the item clicked.

string (optional) mediaType

One of 'image', 'video', or 'audio' if the context menu was activated on one of these types of elements.

string (optional) linkUrl

If the element is a link, the URL it points to.

string (optional) srcUrl

Will be present for elements with a 'src' URL.

string (optional) pageUrl

The URL of the page where the menu item was clicked. This property is not set if the click occured in a context where there is no current page, such as in a launcher context menu.

string (optional) frameUrl

The URL of the frame of the element where the context menu was clicked, if it was in a frame.

string (optional) selectionText

The text for the context selection, if any.

boolean editable

A flag indicating whether the element is editable (text input, textarea, etc.).

boolean (optional) wasChecked

A flag indicating the state of a checkbox or radio item before it was clicked.

boolean (optional) checked

A flag indicating the state of a checkbox or radio item after it is clicked.

tabs.Tab tab

Since Chrome 44. Warning: this is the current Dev channel. Learn more.

The details of the tab where the click took place. Note: this parameter only present for extensions.

integer or string (optional) parentId

Note: You cannot change an item to be a child of one of its own descendants.

array of string (optional) documentUrlPatterns
array of string (optional) targetUrlPatterns
boolean (optional) enabled

Since Chrome 20.

function (optional) callback

Called when the context menu has been updated.

If you specify the callback parameter, it should be a function that looks like this:

function() {...};

remove

chrome.contextMenus.remove(integer or string menuItemId, function callback)

Removes a context menu item.

Parameters
integer or string menuItemId

The ID of the context menu item to remove.

function (optional) callback

Called when the context menu has been removed.

If you specify the callback parameter, it should be a function that looks like this:

function() {...};

removeAll

chrome.contextMenus.removeAll(function callback)

Removes all context menu items added by this extension.

Parameters
function (optional) callback

Called when removal is complete.

If you specify the callback parameter, it should be a function that looks like this:

function() {...};

Events

onClicked

Since Chrome 21.

Fired when a context menu item is clicked.

addListener

chrome.contextMenus.onClicked.addListener(function callback)
Parameters
function callback

The callback parameter should be a function that looks like this:

function(object info, tabs.Tab tab) {...};
object info

Information sent when a context menu item is clicked.

integer or string menuItemId

Since Chrome 35.

The ID of the menu item that was clicked.

integer or string (optional) parentMenuItemId

Since Chrome 35.

The parent ID, if any, for the item clicked.

string (optional) mediaType

Since Chrome 35.

One of 'image', 'video', or 'audio' if the context menu was activated on one of these types of elements.

string (optional) linkUrl

Since Chrome 35.

If the element is a link, the URL it points to.

string (optional) srcUrl

Since Chrome 35.

Will be present for elements with a 'src' URL.

string (optional) pageUrl

Since Chrome 35.

The URL of the page where the menu item was clicked. This property is not set if the click occured in a context where there is no current page, such as in a launcher context menu.

string (optional) frameUrl

Since Chrome 35.

The URL of the frame of the element where the context menu was clicked, if it was in a frame.

string (optional) selectionText

Since Chrome 35.

The text for the context selection, if any.

boolean editable

Since Chrome 35.

A flag indicating whether the element is editable (text input, textarea, etc.).

boolean (optional) wasChecked

Since Chrome 35.

A flag indicating the state of a checkbox or radio item before it was clicked.

boolean (optional) checked

Since Chrome 35.

A flag indicating the state of a checkbox or radio item after it is clicked.

tabs.Tab (optional) tab

The details of the tab where the click took place. If the click did not take place in a tab, this parameter will be missing.