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.
In this article we give you an overview of what is included inside an Opera Extension, and we'll have a brief look at some of the APIs. You'll find more extensive Opera Extensions API reference documentation.
Opera extensions are based on the W3C Widgets specification (e.g. for features like the config.xml
). An extension can contain all of the following files:
/config.xml
/index.html
/background.js
/popup.html
/icons/example.png
/locales/no/index.html
/locales/no/background.js
/locales/no/popup.html
/options.html
Let's go through these parts in turn and explain what they do:
This is the configuration file, which provides valuable meta data for the extension - this is actually one of the two only mandatory part of the extension package (the other is the index.html
), although you won't be able to get it to do very much on its own. Since Opera extensions are based on W3C Widgets, the config.xml
files are the same - see the W3C Widgets spec configuration document section. There are lots of different things you could include in here, and we would recommend that you include at least the following:
Here's an example config.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<widget xmlns="http://www.w3.org/ns/widgets"
id="example.org/example">
<name>My test extension</name>
<description>API experiments and testing.</description>
<author href="http://foo-bar.example.org/"
email="foo-bar@example.org">Foo Bar Corp</author>
<icon src="icons/example.png"/>
</widget>
In widget speak, this is called a "start file". A start file is always needed and serves as the extension's background process (if you want, you can point to a different file using a <content>
element in the config.xml).
This file can also include JavaScript to create UIItems like the UI button and callout. For example:
<!doctype html>
<html>
<head>
<script>
window.addEventListener("load", function(){
var theButton;
var UIItemProperties = {
disabled: false,
title: "101 - createItem w popup big",
icon: "icon.png",
popup: {
href: "popup.html",
width: 250,
height: 500
}
}
theButton = opera.contexts.toolbar.createItem( UIItemProperties );
opera.contexts.toolbar.addItem( theButton );
}, false);
</script>
</head>
<body>
</body>
</html>
Here is where you include your so-called background scripts, which control the extension's background process. Note that you can have as many as you like, and you don't have to call them background.js
.
This file (or files - you can have more than one in a single extension) simply contains the contents of the popup window(s) triggered from the background process. Note that instead of having a popup window specified in one of these files, you can instead specify an external URL as the contents of one of these popups, e.g. http://www.opera.com.
This includes an icon to be used in the extension (see the icons section below for more details).
Any JS files in this folder will be injected to pages you visit while browsing. These files can be made to target certain sites, like youtube.com. To learn more about how injected scripts work, visit our UserJS documentation pages.
The contents of the locales
directory are optional translations for when you have an extension you want to provide translated versions for. Inside the locales
directory you provide a separate directory for each translation, named the same as the language code for that language and dialect. For example, in our above directory tree we have provided a no
folder for a Norwegian translation, but you could provide en-gb
, pt-br
, ru
, cz
, jp
, and as many others as you like (note that they should always be kept lower case, to avoid compatibility problems - this is required by the W3C Widgets Packaging specification). Inside each folder you provide translated alternatives to your default index.html
, injectables, etc.
An Opera Extension's UI is constructed from the UI elements added to a webpage via injected scripts, the buttons in Opera's toolbar, which are created using the UIItems API in the background process, and Popups, which, as explained below, are populated from supplied HTML documents or an external website via its URL.
When the browser finds an options.html file in the extension, it makes a "Preferences" option available for that extension in the extension manager (Tools > Extensions > Manage Extensions). That way, when a user clicks on the extension's "Preferences" dropdown menu item in the extensions manager, the browser will fire up its options page.
Generally, the options page is used for setting preferences or notes such as acknowledgements. Take a look at the Opera extensions options page article for a more detailed explanation.
The architecture of an Opera Extension involves the interaction of the following four basic parts:
Injected script <-> Background Process <-> Button/badge <-> PopupThese parts communicate through cross-document messaging, and do the following:
This part provides the scripting that gets injected to targeted sites.
The background process is what weaves the architecture together - this is the central place where all messaging is routed through, and where most of the interaction with the Extension APIs occurs. The background process triggers the creation of UI items further down the chain via the following methods:
opera.contexts.toolbar.createItem( { ...properties... } )
opera.contexts.toolbar.addItem( uiItem )
opera.contexts.toolbar.removeItem( uiItem )
Note: toolbar
is currently the only context available, but more of these will be available later.
This is where the UI elements of the extension are put together and displayed, for example buttons that you press, badges that display information, and in future releases, menu items.
You can create a button for the browser toolbar by using the opera.contexts.toolbar.createItem()
function, and then add it using opera.contexts.toolbar.addItem()
. Here's an example index.html
file that simply adds a button, and fires a callout when the button is clicked:
<!doctype html>
<html>
<head>
<script>
window.addEventListener("load", function(){
var theButton; // the button variable
var toolbar = opera.contexts.toolbar; //the Opera toolbar
var props = { // options for the button
disabled: false,
title: "My first extension!",
icon: "opera.ico",
popup: {
href: "http://www.google.com",
width: 300,
height: 200
}
}
theButton = toolbar.createItem( props ); // create the button
toolbar.addItem( theButton ); // add button to UI
}, false);
</script>
</head>
<body>
</body>
</html>
You can remove the button using the function opera.contexts.toolbar.removeItem(theButton);
. You can also handle onclick
events on the button by adding onclick: function(){ /* do something */ }
to the button.
If you include a button in the UI, you should include an icon for it. The button icon is rendered 18 x 18 pixels in size, and anything above or below this will be scaled. For best results we recommend that you supply an icon that is exactly 18 x 18 pixels.
Additionally, you will be asked to supply an icon that is 64 x 64 pixels or larger when uploading your extension to our Opera extensions website. This icon will be used in our online catalog next to a title and description of the extension, as well as in the Opera Extension manager in the browser. The title, description, and path to the icon are stored in and retrieved from the config.xml
.
Popups can be defined by simply adding the popup properties when creating a button:
var props = { // options for the button
disabled: false,
title: "My first extension!",
icon: "opera.ico",
popup: {
href: "http://www.google.com",
width: 300,
height: 200
}
}
The above example will create a popup containing the google.com home page. You can also define a local HTML page as the contents of the popup, for example:
popup: {
href: "popup.html",
width: 300,
height: 300
}
A badge is a notification that appears as an overlay on top of an extension button. You add badges by making a dictionary of properties for your button:
var props = { // options for the button
disabled: false,
title: "My first extension!",
icon: "opera.ico",
popup: {
href: "popup.html",
width: 300,
height: 200
},
badge: {
textContent: '123'
}
}
You can also customize the background and foreground color of the badge using the simple style properties, like so:
backgroundColor: '#ffeedd',
color: '#404040',
Finally, the content of the badge can be updated by changing textContent
property:
theButton.badge.textContent = "45"
There are many different types of extension you could build, although the functionality required for all of these is not yet available. We will update this and provide more tutorials in later release phases. Essentially, the different parts of the extensions architecture approximate to this:
Injected script <-> Background <-> Button/badge <-> Popup
The different types of extension you can build are different permutations of these components:
index.html
: This is just an injected script, and an empty index.html
+ config.xml
, packaged up as an extension. It will work fine, but it won't take advantage of any of the extension-specific APIs and other features. javascript:
URL in the address bar.Note: The background has cross-domain XHR abilities, just like widgets. To find more out about using cross-domain XHR, read our article Opera Widgets and Ajax connecting to multiple servers.