0

chrome.contextMenus supports creating context menu items in extension. I am trying to run it in console window (F12) to experiment creating a new item in context menu for the current web page, but it doesn't work. Are the context menu APIs not supported in console windows and why if so?

Makyen
  • 31,849
  • 12
  • 86
  • 121
Thomson
  • 20,586
  • 28
  • 90
  • 134
  • 1
    Are you trying to create a context menu item *for* the DevTools, or are you trying to use `chrome.contextMenus.*` from *within* the DevTools console to create/update the context menu for web pages? – Makyen Dec 16 '16 at 04:07
  • @Makyen I am trying to test chrome.contextMunus.* in DevTools before applying it to extension. Seems this is not the correct way to test it. – Thomson Dec 16 '16 at 06:28
  • Unfortunately, I am still unclear on what you are wanting to do. Are you wanting to add to the context menu that exists within the DevTools? Or the context menu that exists within normal pages? – Makyen Dec 16 '16 at 06:36
  • @Makyen I want to add context menu to normal web page via the DevTools associated with the page (F12). From your answer seems this is not the right instance of DevTools and I need open background page for one extension to experiment it. – Thomson Dec 16 '16 at 07:06

1 Answers1

2

Chrome has multiple different consoles/DevTools which you can open for an extension. This answer describes how you can open the various DevTools which are available. However, you will never be able to access/execute the chrome.contextMenu.* API from the F12 DevTools, because the only extension context with which you can interact from that DevTools is the content script context and content scripts do not have access to the chrome.contextMenu.* API. That API is only available in the background context.

Thus, you will need to use the DevTools available for your background page1. In addition, you will, of course, need to have declared the contextMenus permission in your manifest.json. The answer linked above will show/tell you how to open the DevTools for your background page. From there, you should be able to execute chrome.contextMenu.* API calls from the JavaScript console.


  1. The consoles for the other pages which are in the background context (popup, options pages, panels, extension pages opened in tabs, etc.) do not appear to provide access to the chrome.* APIs. Testing in those consoles indicated that chrome was undefined.
Community
  • 1
  • 1
Makyen
  • 31,849
  • 12
  • 86
  • 121
  • It seems `chrome.*` are defined in web page context but with very few properties included. – Thomson Dec 16 '16 at 07:10
  • Yes, Chrome does define `chrome` with a few properties in the web page context. Some of them even overlap Chrome extension APIs (e.g. `runtime`). – Makyen Dec 16 '16 at 07:22