contextMenus

The contextMenus API allows extensions to add items to Google Chrome's right-click context menu.

What it does

  • Create, update, and remove menu items.
  • Add items that appear for different contexts, such as on a page, for a selection of text, for an image, or for a link.
  • Create different types of menu items, including normal items, checkboxes, radio buttons, and separators.
  • Add submenus by creating a menu item that is the parent of another.

When to use it

Use this API to provide functionality that is relevant to the user's current context without cluttering the browser's UI.

Examples:

  • A search extension that adds a "Search on [My Site] for '%s'" item when text is selected (where %s is the selected text).
  • An image-saving extension that adds a "Save image to [My Service]" item when a user right-clicks an image.
  • A translation extension that can translate a selected block of text.

Manifest Declaration

{
  "name": "My Context Menu Extension",
  "version": "1.0",
  "manifest_version": 3,
  "permissions": [
    "contextMenus"
  ],
  "background": {
    "service_worker": "background.js"
  }
}

Security & Privacy

Why is it not risky?

This is a very safe permission. It only allows an extension to add new options to the menu that appears when you right-click on a page (the "context menu").

For example, it might add a "Search for this image on..." option. By itself, this permission does not give the extension any ability to read the page you are on or access your personal data.

API Usage Example

This example creates a context menu item that only appears when the user has selected text on a page. When clicked, it logs the selected text.

// background.js

// Create the context menu item upon installation.
chrome.runtime.onInstalled.addListener(() => {
  chrome.contextMenus.create({
    id: "logSelection",
    title: "Log selected text",
    contexts: ["selection"] // Only show when text is selected
  });
});

// Add a listener for when a context menu item is clicked.
chrome.contextMenus.onClicked.addListener((info, tab) => {
  if (info.menuItemId === "logSelection") {
    // 'info.selectionText' contains the text that was selected.
    if (info.selectionText) {
      console.log("Selected text:", info.selectionText);
    }
  }
});

In a real extension, instead of just logging the selected text, you might send it to a remote service, search it on a specific website, or use it to perform some other context-aware action.

Extensions with the contextMenus permission

Here are some popular browser extensions that use the "contextMenus" permission. To explore more, try our Advanced search.

Permission Metrics

Popularity

Security Risk


Usage by Platform