This documentation relates to Opera's now deprecated .oex Extension API framework for Opera versions <= 12.15 and also provided by our OEX2NEX shim library.
For the latest Opera Extensions API documentation for Opera versions > 12.15 please consult the latest Opera Extensions API documentation online.
MessagePort
to the tab in which a MenuEvent was fired.DOMNode
object on which a MenuEvent was fired.src
attribute on which a MenuEvent was activated.The Context Menu API enables an extension to add an item to the context menu, otherwise known as the right-click menu. An extension may only add one item to the context menu, but this item may contain various layers of sub-items, similar to a folder/sub-folder structure. You can specify whether a context menu item should only appear on certain domains or on certain types of element, e.g. links only, images only, etc. When a context menu item is clicked, it is possible to get data such as the URL of the current element or any text the user has selected.
Menu items can only be created within an extension's background process, but the opera.contexts.menu
object is also accessible from injected scripts, for example using a click event listener. It's also possible to pass messages between a MenuItem
and an injected script using the MenuEvent
's source
attribute. See the examples below and in the individual method/attribute pages.
To enable the context menu, the opera:contextmenus
feature needs to be added as a feature element to the extension's config.xml file.
What follows are some examples designed to make clear the major use cases of the Context Menu API.
In this example, a menu item is added to the context menu for any domain. When the menu item is clicked, the current page opens in a new private tab.
<!--
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. '/background.js').
//
window.addEventListener('DOMContentLoaded', function() {
// 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: 'Privatize',
onclick: function(event) {
// Create a tab properties object
var tabProps = {
url: event.pageURL,
private: true
};
// Create a tab with the specified properties
var tab = opera.extension.tabs.create(tabProps);
}
}
// Create a menu item with the specified properties
var item = menu.createItem(itemProps);
// Add the menu item to the context menu
menu.addItem(item);
} else {
console.error('Context Menu API not supported.');
}
}, false);
In this example, a menu item is added to the context menu for editable elements only. When the menu item is clicked, a message is sent to the current tab's injected script. The message data (an email address) is then inserted into the editable element.
<!--
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. '/background.js').
//
window.addEventListener('DOMContentLoaded', function() {
// Check the Context Menu API is supported
if (opera.contexts.menu) {
var menu = opera.contexts.menu;
var mail = 'operafan@example.com';
// Create a menu item properties object
var itemProps = {
contexts: ['editable'],
title: 'Mail autofill',
onclick: function(event) {
// Send a message to the injected script in the originating tab
event.source.postMessage({message: mail});
}
}
// Create a menu item with the specified properties
var item = menu.createItem(itemProps);
// Add the menu item to the context menu
menu.addItem(item);
}
}, false);
//
// An injected script (e.g. '/includes/injected.js').
//
(function() {
// Listen for the context menu being clicked
opera.contexts.menu.onclick = function(menuEvent) {
// Listen for a message from the background process
opera.extension.addEventListener('message', function(event) {
// Insert message from the event.data object into the source element
menuEvent.srcElement.value = event.data.message;
}, false);
};
})();