`
xitongyunwei
  • 浏览: 926335 次
文章分类
社区版块
存档分类
最新评论

Mozilla Addons学习笔记(4)

 
阅读更多

3.1.3.context-menu

该API允许用户向浏览器的右键菜单中添加选项。

介绍:

Context-menu API允许用户在上下文菜单(右键菜单,context menu)中添加一个菜单项(item),当点击时执行一个动作,或者添加一个子菜单或菜单分离器。

使用该API,只需要将一个菜单项和一个页面上的上下文(context)绑定,就像事件的监听器和事件之间的绑定一样。这样在特定的上下文出现、并且用户调用上下文菜单时,菜单项就会自动地被添加到快捷菜单中。开发者不需要手动地删除一个菜单项,除非希望该菜单项永远不再出现。

指定菜单项的上下文

正如其名,上下文菜单的菜单项需要对应于一个特定的上下文。上下文可以使页面的内容或者页面本身,但不能是页面以外的东西。

开发者需要为一个菜单项指定一个用以绑定的上下文。缺省情况下的上下文是page context。page-context当用户在页面上的一个非交互部分调用了上下文菜单时出现。page-context可以作用于整个页面,当用户在链接、图片、非文本节点,或者一个选择区域上调用上下文菜单时,page-context不会出现。

除此以外,开发者还可以通过如下方式,在创建菜单项时指定一些简单的、说明的上下文:

var cm = require("context-menu");
cm.Item({
  label: "My Menu Item",
  context: cm.URLContext("*.mozilla.org")
});

context-menu对象还有如下的构造方法,可以用来获取上下文:

构造器 描述
PageContext() page context.
SelectionContext() 当用户调用上下文菜单时有选择区域时出现.
SelectorContext(selector) This context occurs when the menu is invoked on a node that either matches selector, a CSS selector, or has an ancestor that matches. selector may include multiple selectors separated by commas, e.g., "a[href], img".
URLContext(matchPattern) This context occurs when the menu is invoked on pages with particular URLs. matchPattern is a match pattern string or an array of match pattern strings. When matchPattern is an array, the context occurs when the menu is invoked on a page whose URL matches any of the patterns. These are the same match pattern strings that you use with the page-mod include property. Read more about patterns.
array An array of any of the other types. This context occurs when all contexts in the array occur.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics