opera.contexts.menu.addItem() and MenuItem.addItem()

By Opera Software

Description

This method adds a MenuItem to the context menu or a context menu item. The optional before parameter can be specified to indicate where in the menu the item should be added. To create a MenuItem see the createItem() method.

Parameters:

  • item: The MenuItem to be added.
  • before (optional): The item before which the new MenuItem should be added.

Syntax:

void addItem (<MenuItem> item, optional <MenuItem> before)

void addItem (<MenuItem> item, optional long before)

Example:

The example below creates and adds an item to the context menu. When it's clicked, the current page's URL is shown in the console.

<!-- 
  The configuration file ('config.xml').
-->
<?xml version='1.0' encoding='utf-8'?>
<widget xmlns="http://www.w3.org/ns/widgets">
    ...
    <feature name="opera:contextmenus"/>
    ...
</widget>
//
// The background process (e.g. index.html)
//

// Check the Context Menu API is supported
if (opera.contexts.menu) {
  var menu = opera.contexts.menu;
  // Create a menu item properties object
  var itemProps = {
    title: 'Context Menu Example',
    onclick: function(event) {
      console.log('Menu item clicked on ' + event.pageURL);
    }
  }

  // Create a menu item with the specified properties
  var item = menu.createItem(itemProps);
  // Add the menu item to the context menu
  menu.addItem(item);
}

This article is licensed under a Creative Commons Attribution 3.0 Unported license.

Comments

  • photo

    JanGen

    Friday, October 19, 2012

    Be aware that you can add only one menu item per extension, but that can be a folder/submenu in which you can add more items. (type=folder)
    @Jayesh, only in that case you can influence the position.
    AFAICS at the moment an extension item will be always added at the bottom of a menu which is a bit of a bogus.
No new comments accepted.