Robin Carlo Catacutan
Robin Carlo Catacutan

Reputation: 13679

Create context menu on browserAction onclick

Its been hours now that I've been stuck in this problem, does anyone know how to show a context menu every time the user clicks on the google chrome extension. I managed to display the menu with my default_popup, but I found out that I should implement this on a context menu.

Here's the image:

I have this following code

manifest.json

{
    "name": "Hello Toolbar",
    "version": "1.0",
    "manifest_version": 2,
    "description": "A Toolbar that helps.",
    "browser_action": {
        "default_icon": "icon.png"
    },
    "background": {
        "scripts": ["background.js"]
    },
    "permissions": [
        "cookies",
        "http://*/*",
        "tabs",
        "contextMenus"
    ]
}

background.js

chrome.browserAction.onClicked.addListener( function(tab){
    chrome.contextMenus.create({"title": "Test parent item", "contexts": ['page']}); 
});

but it doesn't work.

Upvotes: 1

Views: 1358

Answers (1)

Rob W
Rob W

Reputation: 348962

I think that your expectation of the chrome.contextMenus API is wrong. It can never be used to open a context menu. It can only tell Chrome to show context menu entries when some custom conditions have been met (see the documentation of the create method for to learn about defining these conditions).

In the screencast below, I show the expected behaviour for your code.

  • No context menu shows up unless the user right-clicks.
  • After clicking the browser action button, the context menu entry shows up when opening the context menu within the page.

If you want a "popup" to appear on click, use the "default_popup" entry in the "browser_action" section of the manifest file. Then, the following shows up:

You can use CSS to get the popup to look like a "context menu". The Browser action documentation provides an some examples.

Upvotes: 4

Related Questions