Adding children to context menu links

笑着哭i 提交于 2019-12-11 02:12:20

问题


I am trying to add children for my context menu, but for some reason it is not working for me. Here is my code:

var parent1 = chrome.contextMenus.create({"title": "Trigger Rank", "contexts":["link"]});

var child1 = chrome.contextMenus.create
(
    {"title": "Rank 1", "parentId": parent1, "onclick": genericOnClick}
);
var child2 = chrome.contextMenus.create
(
    {"title": "Rank 2", "parentId": parent1, "onclick": genericOnClick}
);
var child2 = chrome.contextMenus.create
(
    {"title": "Rank 3", "parentId": parent1, "onclick": genericOnClick}
);



var parent2 = chrome.contextMenus.create({"title": "Target Rank", "contexts":["link"]});
var child1 = chrome.contextMenus.create
(
    {"title": "Rank 1", "parentId": parent2, "onclick": genericOnClick}
);
var child2 = chrome.contextMenus.create
(
    {"title": "Rank 2", "parentId": parent2, "onclick": genericOnClick}
);
var child3 = chrome.contextMenus.create
(
    {"title": "Rank 3", "parentId": parent2, "onclick": genericOnClick}
);

Seems pretty straight forward but it's my first chrome extension, and first time writing in javascript. So might be that I am missing something.

Currently when right clicking a link, ONLY the parents are shown (and not the children).

Thanks in advance and sorry if my question is too "newbie". :)


回答1:


Your parent items have contexts set to ["link"], but your children items have the default contexts of ["page"]. Therefore:

  • when you right-click on a link, the parents show up, but the children do not, because they're not set to show up on links.
  • when you right-click on something besides a link, the parents don't show up, so the children aren't shown either.

You must set your children's contexts values to match the parents' ["link"] value.




回答2:


I just ran into this with a similar but not exactly the same case.

I am creating a menu that needs to support multiple contexts (link, page, selection) and the child menus only showed up for page.

The answer above is correct in that you have to define your contexts because it's an optional param for create. Even though I read the doc carefully this was a painful discovery: http://developer.chrome.com/extensions/contextMenus.html#method-create

contexts ( optional array of enum of "all", "page", "frame", "selection", "link", "editable", "image", "video", "audio", or "launcher" ) List of contexts this menu item will appear in. Defaults to ['page'] if not specified. Specifying ['all'] is equivalent to the combination of all other contexts except for 'launcher'. The 'launcher' context is only supported by apps and is used to add menu items to the context menu that appears when clicking on the app icon in the launcher/taskbar/dock/etc. Different platforms might put limitations on what is actually supported in a launcher context menu.

To answer explicitly, here's your code with the solution:

var child1 = chrome.contextMenus.create
(
    {"title": "Rank 1", "parentId": parent1, "onclick": genericOnClick, "contexts":["link"]}
);
var child2 = chrome.contextMenus.create
(
    {"title": "Rank 2", "parentId": parent1, "onclick": genericOnClick, "contexts":["link"]}
);
var child2 = chrome.contextMenus.create
(
    {"title": "Rank 3", "parentId": parent1, "onclick": genericOnClick, "contexts":["link"]}
);

In my own case, I was supporting multiple contexts and all needed to be accounted for in the create (e.g. "contexts": ["page", "link", "selection"])



来源:https://stackoverflow.com/questions/14818714/adding-children-to-context-menu-links

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!