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 35.
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 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", "page_action", or "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. If an error occurs during creation, it may not be detected until the creation callback fires; details will be in runtime.lastError.

Parameters
object createProperties
ItemType (optional) type

The type of menu item. Defaults to normal.

string (optional) id

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 display in the item; this is required unless type is separator. When the context is selection, 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 button: true for selected, false for unselected. Only one radio button can be selected at a time in a given group.

array of ContextType (optional) contexts

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

boolean (optional) visible

Since Chrome 62.

Whether the item is visible in the menu.

function (optional) onclick

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

Parameters
object info

Information about the item clicked and the context where the click happened.

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.

integer (optional) frameId

The ID 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

The details of the tab where the click took place. This parameter is not present for platform apps.

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

Restricts the item to apply only to documents or frames whose URL matches one of the given patterns. For details on pattern formats, see Match Patterns.

array of string (optional) targetUrlPatterns

Similar to documentUrlPatterns, filters based on the src attribute of img, audio, and video tags and the href attribute of a tags.

boolean (optional) enabled

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 an error occurs during creation, details will be available in 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 contextMenus.create function.

ItemType (optional) type
string (optional) title
boolean (optional) checked
array of ContextType (optional) contexts
boolean (optional) visible

Since Chrome 62.

Whether the item is visible in the menu.

function (optional) onclick
Parameters
object info

Since Chrome 44.

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.

integer (optional) frameId

The ID 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.

The details of the tab where the click took place. This parameter is not present for platform apps.

integer or string (optional) parentId

The ID of the item to be made this item's parent. Note: You cannot set an item to become a child of its own descendant.

array of string (optional) documentUrlPatterns
array of string (optional) targetUrlPatterns
boolean (optional) enabled
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

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 about the item clicked and the context where the click happened.

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.

integer (optional) frameId

The ID 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 (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.