Skip to content
AnswerWell geo Documentation

Research · 6 min read · Answerwell

Extension contextMenus: Permissions, Context Types, and Menu Items

How browser extensions add right-click menu items: contextMenus permission, contexts, URL patterns, create/update, and click handling in service workers.

This page summarizes Chrome Extensions documentation on the contextMenus API (also exposed as chrome.contextMenus and browser.contextMenus). Extensions use it to add entries to the browser’s context menu—the menu that opens on alternate click (often called right-click)—for pages, links, images, selections, and other targets.

contextMenus API: what extension developers use it for

Section titled “contextMenus API: what extension developers use it for”

The contextMenus API lets an extension register custom actions on the browser context menu. You choose which kinds of UI targets your items apply to, such as images, hyperlinks, and whole pages.

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.

— Google Chrome, contextMenus

The Extensions user interface overview describes the same idea in plainer terms: a context menu appears for alternate click, and you define menus with the Context Menus API. For step-by-step setup, see Build a context menu; member-by-member reference is on browser.contextMenus and the Manifest V2 mirror chrome.contextMenus.

contextMenus permission: manifest and icons

Section titled “contextMenus permission: manifest and icons”

You must declare the contextMenus permission before calling the API. The Build a context menu guide starts by adding that permission; Design the user interface shows the same pattern for a “Global Google Search”–style extension.

You must declare the “contextMenus” permission in your extension’s manifest to use the API. Also, you should specify a 16 by 16-pixel icon for display next to your menu item.

— Google Chrome, contextMenus

A minimal permission block:

"permissions": [
"contextMenus"
]

If you want an icon beside a menu entry, provide a 16×16 asset under icons in the manifest (for example "16": "icon-bitty.png"). The context menu tutorial notes that the Global Google Search sample uses a 16×16 icon next to its menu item.

Where menu items appear: documents, frames, and URL patterns

Section titled “Where menu items appear: documents, frames, and URL patterns”

Context menu items can appear in any document or frame, including file:// and chrome:// URLs, unless you restrict them.

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() methods.

— Google Chrome, contextMenus

Use documentUrlPatterns on create() or update() to limit items to matching page or frame URLs. Use targetUrlPatterns to filter by href on links or src on img, audio, and video elements. Pattern syntax is documented elsewhere on Chrome’s site; the cluster only points to “Match Patterns” by name without a URL, so follow the linked reference pages when you implement patterns.

Section titled “ContextType: right-click on page, link, selection, image, and more”

Each item’s contexts property is a list of ContextType values. If you omit it, the default is ['page'].

The different contexts a menu can appear in. Specifying ‘all’ is equivalent to the combination of all other contexts except for ‘launcher’ and ‘tab’.

— Google Chrome, contextMenus

Context Typical user action
page Right-click empty area or non-specific hit on the page (default)
frame Right-click inside a nested frame
selection Right-click with text selected
link Right-click a hyperlink
editable Right-click in an input or editable region
image, video, audio Right-click media
action Menu on the extension’s toolbar action icon
tab Menu on a tab in the tab strip
launcher App launcher icon (apps only; platform limits may apply)
all Combination of the main web contexts (not launcher or tab)

For selection context, put %s in title to insert the selected text—for example Translate '%s' to Pig Latin becomes Translate 'cool' to Pig Latin when the user selected “cool”.

contextMenus.create and update: ids, types, and submenus

Section titled “contextMenus.create and update: ids, types, and submenus”

Call contextMenus.create() from your extension service worker (or background script in older samples), typically inside runtime.onInstalled. The Build a context menu example loops locales and creates one item per TLD:

browser.runtime.onInstalled.addListener(async () => {
for (let [tld, locale] of Object.entries(tldLocales)) {
browser.contextMenus.create({
id: tld,
title: locale,
type: 'normal',
contexts: ['selection'],
});
}
});

Creates a new context menu item. If an error occurs during creation, it may not detect until the creation callback fires; details will be in runtime.lastError.

— Google Chrome, contextMenus

id must be unique within your extension and is mandatory for event pages. Use parentId to nest an item under another. type defaults to normal; separators omit title. Checkbox and radio items use checked; only one radio in a group may be selected.

Use contextMenus.update(id, updateProperties) to change title, visibility, enabled state, contexts, or patterns. You cannot set parentId so an item becomes a child of its own descendant.

When Chrome collapses multiple extension menus

Section titled “When Chrome collapses multiple extension menus”

If more than one menu item from your extension would show at once, Chrome collapses them under a single parent menu—shown in Build a context menu and Design the user interface.

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.

— Google Chrome, contextMenus

Plan labels and hierarchy so collapsed grouping still makes sense to users.

Click handling: onclick vs contextMenus.onClicked

Section titled “Click handling: onclick vs contextMenus.onClicked”

In a service worker, the onclick property on create() is not available. Register contextMenus.onClicked instead.

A function that is called back when the menu item is clicked. This is not available inside a service worker; instead, you should register a listener for contextMenus.onClicked.

— Google Chrome, contextMenus

The handler receives OnClickData (menuItemId, pageUrl, linkUrl, srcUrl, selectionText, mediaType, frame fields, checkbox state, and more) and usually a tab object for the tab where the user clicked. Use those fields rather than re-querying the page when possible.

webview.contextMenus: menus inside embedded webviews

Section titled “webview.contextMenus: menus inside embedded webviews”

Chrome Apps’ webview expose a parallel contextMenus surface for content inside a <webview>, with similar create, update, remove, and removeAll methods and the same style of contexts, patterns, and parentId. The webview can fire onShow before the menu appears; calling event.preventDefault() suppresses it. This applies to embedded webview UI, not ordinary extension pages in a normal tab.

Common mistakes when adding extension context menus

Section titled “Common mistakes when adding extension context menus”
  • Omitting contextMenus in the manifest, so create() never succeeds.
  • Using onclick in a service worker instead of onClicked.
  • Expecting separate top-level entries when Chrome will collapse multiple items from one extension.
  • Forgetting %s in title when using selection context.
  • Relying on pageUrl in launcher contexts where it is not set.
  • Duplicate id values across items in the same extension.

Official samples: the docs point to the contextMenus API example in the chrome-extension-samples repository (contextMenus reference); the UI context menu page links to context menu samples for hands-on learning.

  1. Add contextMenus to manifest.json, load the unpacked extension from chrome://extensions, and confirm no manifest errors.
  2. Open a normal HTTPS page, right-click in contexts you configured (page, link, selection, etc.), and confirm items appear or stay hidden per your contexts and URL patterns.
  3. Open the extension service worker inspector, trigger a menu click, and verify your contextMenus.onClicked handler receives expected OnClickData fields.
  4. Create several sibling items and confirm Chrome collapses them under one parent when all would be visible, matching Build a context menu.
  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact passage you are asking about.