# Context menu items are context-aware and only appear when relevant

- **Pattern:** `ab-001378` (`extension-ux-performance.browser-ux.context-menus-aware`)
- **Severity:** info
- **Lifecycle:** active
- **Last modified:** 2026-04-18
- **Canonical URL:** https://auditbuffet.com/patterns/ab-001378
- **License:** CC-BY-4.0 — attribute to AuditBuffet Pattern Catalog (https://auditbuffet.com/patterns/ab-001378)

## Why it matters

Context menu items registered with `contexts: ['all']` appear on every right-click — blank page areas, the New Tab page, chrome:// settings, PDFs — cluttering the menu with options that cannot function in those contexts. Users right-clicking to copy text wade past irrelevant extension entries, and Chrome Web Store reviewers flag menu-spamming extensions as a policy concern under the single-purpose and minimal-permission guidelines. On pages where the extension has no host permission, clicking the item produces a silent no-op and looks broken.

## Severity rationale

Info because over-broad contexts degrade polish and discoverability without breaking core functionality.

## Remediation

Scope every `chrome.contextMenus.create` call to the narrowest context that makes sense: `['selection']` for text actions, `['image']` for image actions, `['link']` for link actions. Add `documentUrlPatterns` to exclude pages where the extension cannot operate. In `background.js`:

```js
chrome.contextMenus.create({
  id: 'search-selection',
  title: 'Search "%s"',
  contexts: ['selection'],
  documentUrlPatterns: ['https://*/*', 'http://*/*'],
});
```

## Detection

- **ID:** `context-menus-aware`
- **Severity:** `info`
- **What to look for:** Examine `chrome.contextMenus.create` calls. For each menu item, check the `contexts` array — does it specify the narrowest context in which the item makes sense? For example, a "Search selected text" item should use `contexts: ['selection']`, not `contexts: ['all']`. An "Open image in extension" item should use `contexts: ['image']`. Check whether menu items are conditionally shown or hidden based on page context (URL patterns via `documentUrlPatterns`, or dynamic visibility updates). Count all instances found and enumerate each.
- **Pass criteria:** All context menu items specify appropriate `contexts` arrays that limit their appearance to when they are actually usable. Image-specific actions use `contexts: ['image']`. Text-action items use `contexts: ['selection']`. Items not relevant to the current page are hidden via `documentUrlPatterns` or dynamic `updateProperties` calls. At least 1 implementation must be confirmed.
- **Fail criteria:** Context menu items use `contexts: ['all']` when a more specific context applies, causing them to appear in all right-click menus everywhere regardless of relevance. Extension adds many menu items that clutter the context menu on all pages regardless of whether the extension can do anything useful there.
- **Skip (N/A) when:** The extension does not register any context menu items (no `chrome.contextMenus.create` calls and no `contextMenus` permission in manifest).
- **Detail on fail:** Describe the over-broad contexts. Example: `"'Analyze Image' context menu item uses contexts:['all'] — appears on every right-click including on blank page areas and text, where it serves no purpose"` or `"3 extension menu items appear on all pages including the browser's New Tab page and settings pages where the extension cannot function."`
- **Remediation:** Narrow context menu item scope with specific `contexts` and `documentUrlPatterns`:

  ```js
  chrome.contextMenus.create({
    id: 'analyze-image',
    title: 'Analyze with MyExtension',
    contexts: ['image'],  // Only on right-clicked images
  });

  chrome.contextMenus.create({
    id: 'search-selection',
    title: 'Search "%s" in MyExtension',
    contexts: ['selection'],  // Only when text is selected
    documentUrlPatterns: ['https://*/*', 'http://*/*'],
  });
  ```

---

Taxons: user-experience

HTML version: https://auditbuffet.com/patterns/ab-001378
