Context Menus
Context Menus
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.
Version note:
Before Chrome 14,
your items could appear only in documents with http://
or https:// URLs.
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
Methods
create
integer
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
)
-
Undocumented.
-
-
type
(
optional
enumerated
string
["normal", "checkbox", "radio", "separator"]
)
- The type of menu item. Defaults to 'normal' if not specified.
-
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
string
["all", "page", "frame", "selection", "link", "editable", "image", "video", "audio"]
)
- List of contexts this menu item will appear in. Defaults to ['page'] if not specified.
-
onclick
(
optional
function
)
- A function that will be called back when the menu item is clicked.
-
Parameters
-
info
- Information about the item clicked and the context where the click happened.
-
tab
- The details of the tab where the click took place.
-
parentId
(
optional
integer
)
- 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.
-
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.
Returns
-
(
integer
)
- The ID of the newly created item.
Callback function
If you specify the callback parameter, it should
specify a function that looks like this:
function() {...};
remove
chrome.contextMenus.remove(integer
menuItemId, function
callback)
Removes a context menu item.
Parameters
-
menuItemId
(
integer
)
- 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() {...};
update
chrome.contextMenus.update(integer
id, object
updateProperties, function
callback)
Updates a previously created context menu item.
Parameters
-
id
(
integer
)
- 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"]
)
-
Undocumented.
-
title
(
optional
string
)
-
Undocumented.
-
checked
(
optional
boolean
)
-
Undocumented.
-
contexts
(
optional
array of
string
["all", "page", "frame", "selection", "link", "editable", "image", "video", "audio"]
)
-
Undocumented.
-
onclick
(
optional
function
)
-
Undocumented.
-
parentId
(
optional
integer
)
- Note: You cannot change an item to be a child of one of its own descendants.
-
documentUrlPatterns
(
optional
array of
string
)
-
Undocumented.
-
targetUrlPatterns
(
optional
array of
string
)
-
Undocumented.
-
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() {...};
Types
OnClickData
(
object
)
Information sent when a context menu item is clicked.
-
menuItemId
(
integer
)
- The ID of the menu item that was clicked.
-
parentMenuItemId
(
optional
integer
)
- 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
(
string
)
- The URL of the page where the menu item was clicked.
-
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
(
string
)
- A flag indicating whether the element is editable (text input, textarea, etc.).