chrome.contextMenus
The context menus module allows you
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.
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.
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.
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.
API Reference: chrome.contextMenus
Types
OnClickData
( object )
Information sent when a context menu item is clicked.
-
menuItemId
(
integer or string
)
-
The ID of the menu item that was clicked.
-
parentMenuItemId
(
optional
integer or string
)
-
The parent ID, if any, for the item clicked.
-
mediaType
(
optional
string
)
-
One of 'image', 'video', or 'audio' if the context menu was activated on one of these types of elements.
-
linkUrl
(
optional
string
)
-
If the element is a link, the URL it points to.
-
srcUrl
(
optional
string
)
-
Will be present for elements with a 'src' URL.
-
pageUrl
(
optional
string
)
-
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.
-
frameUrl
(
optional
string
)
-
The URL of the frame of the element where the context menu was clicked, if it was in a frame.
-
selectionText
(
optional
string
)
-
The text for the context selection, if any.
-
editable
(
boolean
)
-
A flag indicating whether the element is editable (text input, textarea, etc.).
-
wasChecked
(
optional
boolean
)
-
A flag indicating the state of a checkbox or radio item before it was clicked.
-
checked
(
optional
boolean
)
-
A flag indicating the state of a checkbox or radio item after it is clicked.
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.extension.lastError).
Parameters
- createProperties ( object )
-
- type ( optional enumerated string ["normal", "checkbox", "radio", "separator"] )
- The type of menu item. Defaults to 'normal' if not specified.
- id ( optional string )
- The unique ID to assign to this item. Mandatory for event pages. Cannot be the same as another ID for this extension.
- title ( optional string )
- 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". - checked ( optional boolean )
- 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.
- contexts ( optional array of enumerated string ["all", "page", "frame", "selection", "link", "editable", "image", "video", "audio", "launcher"] )
- 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.
- onclick ( optional function )
- 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
- info ( OnClickData )
- Information about the item clicked and the context where the click happened.
- tab ( tabs.Tab )
- The details of the tab where the click took place.
- parentId ( optional integer or string )
- The ID of a parent menu item; this makes the item a child of a previously added item.
- documentUrlPatterns ( optional array of string )
- 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.
- targetUrlPatterns ( optional array of string )
- Similar to documentUrlPatterns, but lets you filter based on the src attribute of img/audio/video tags and the href of anchor tags.
- enabled ( optional boolean )
- Whether this context menu item is enabled or disabled. Defaults to true.
- callback ( optional function )
- Called when the item has been created in the browser. If there were any problems creating the item, details will be available in chrome.extension.lastError.
Callback function
If you specify the callback parameter, it should specify 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
- id ( integer or string )
- The ID of the item to update.
- updateProperties ( object )
- The properties to update. Accepts the same values as the create function.
-
- type ( optional enumerated string ["normal", "checkbox", "radio", "separator"] )
- title ( optional string )
- checked ( optional boolean )
- contexts ( optional array of enumerated string ["all", "page", "frame", "selection", "link", "editable", "image", "video", "audio", "launcher"] )
- onclick ( optional function )
- parentId ( optional integer or string )
- Note: You cannot change an item to be a child of one of its own descendants.
- documentUrlPatterns ( optional array of string )
- targetUrlPatterns ( optional array of string )
- enabled ( optional boolean )
- callback ( optional function )
- Called when the context menu has been updated.
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function() {...};
remove
chrome.contextMenus.remove(integer or string menuItemId, function callback)
Removes a context menu item.
Parameters
- menuItemId ( integer or string )
- The ID of the context menu item to remove.
- callback ( optional function )
- Called when the context menu has been removed.
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function() {...};
removeAll
chrome.contextMenus.removeAll(function callback)
Removes all context menu items added by this extension.
Parameters
- callback ( optional function )
- Called when removal is complete.
Callback function
If you specify the callback parameter, it should specify a function that looks like this:
function() {...};
Events
onClicked
Fired when a context menu item is clicked.
Listener Parameters
- info ( OnClickData )
- Information about the item clicked and the context where the click happened.
- tab ( optional tabs.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.
Sample Extensions that use chrome.contextMenus
Context Menus Sample –
Shows some of the features of the Context Menus API
Context Menus Sample (with Event Page) –
Shows some of the features of the Context Menus API using an event page
Imageinfo –
Get image info for images, including EXIF data