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 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
)
-
Undocumented.
-
-
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
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. Event pages cannot use this; instead, they should register a listener for chrome.contextMenus.onClicked.
-
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 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.
Returns
-
(
integer or string
)
- 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 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() {...};
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"]
)
-
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 or string
)
- 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.
-
enabled
(
optional
boolean
)
-
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() {...};
Events
onClicked
chrome.contextMenus.onClicked.addListener(function(OnClickData info, tabs.Tab tab) {...});
Fired when a context menu item is clicked.
Listener 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.
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
(
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
(
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.