kspearrin
kspearrin

Reputation: 10768

Change Extension Top-most Context Menu Title

Currently the top-most context menu for my extension uses the manifest.json name as the title. The name is rather long and creates a large context menu to fit it. Is it possible to change the title that the context menu uses? I would prefer it use the manifest.json short_name.

I was not able to find anything in the docs that mentioned doing this.

Example:

manifest.json

{
    "name": "My extension - And a blurb",
    "short_name": "My extension"
    ...
    "permissions": [
      "contextMenus"
    ],
    "icons": {
      "16": "icon-bitty.png",
      "48": "icon-small.png",
      "128": "icon-large.png"
    },
    ...
  }

background.js

chrome.contextMenus.create({"title": "Test item"});

Results in a context menu that looks like:

My extension - And a blurb
|
--- Test item

I want it to be:

My extension
|
--- Test item

Upvotes: 0

Views: 374

Answers (1)

Xan
Xan

Reputation: 77541

This behavior happens only if you have more than 1 menu item and don't take care of your own hierarchy.

If you declare your own "root" item, and all other items are explicitly inside it, this won't happen.

chrome.contextMenus.removeAll();
chrome.contextMenus.create({title: "How fancy", id: "root"});
chrome.contextMenus.create({title: "Indeed", id: "sub1", parentId: "root"});
chrome.contextMenus.create({title: "Indubitably", id: "sub2", parentId: "root"});

will result in

[your 16x16 icon] How fancy
|
+- Indeed
|
+- Indubitably

In general, make sure you assign IDs to your context menus. It's useful for many purposes.

Note: technically, contextMenus is an asynchronous API and one should, ideally, chain the calls instead of simply sequencing them, but a sequence of calls to the same asynchronous API seems to work fine in this case. If you need to run code after all menus were added, do it in a callback of last create.

Upvotes: 1

Related Questions