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: Stable 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

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

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
enum of "normal", "checkbox", "radio", or "separator" (optional) type The type of menu item. Defaults to 'normal' if not specified.
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 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 enum of "all", "page", "frame", "selection", "link", "editable", "image", "video", "audio", or "launcher" (optional) contexts List of contexts this menu item will appear in. Defaults to ['page'] if not specified. 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.
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
contextMenusInternal.OnClickData info Information about the item clicked and the context where the click happened.
tabs.Tab tab The details of the tab where the click took place.
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 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.
enum of "normal", "checkbox", "radio", or "separator" (optional) type
string (optional) title
boolean (optional) checked
array of enum of "all", "page", "frame", "selection", "link", "editable", "image", "video", "audio", or "launcher" (optional) contexts
function (optional) onclick
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
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

addListener

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

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

function() {...};