How to Create an Opera Extension from Scratch

Opera 11, the next version of the critically acclaimed web browser, packs an interesting, much wanted feature: extensions. Extensions allow users to make the web browser their own by adding features and functionality directly into the browser itself, and not as a standalone Opera Widget or Opera Unite application.

In this tutorial, you will learn the basics of developing Opera components by creating a simple extension with the same web standards we already use to build websites.


Introduction

Opera extensions are based on the W3C widget specification.

Developers can build Opera extensions with the same web stack skills they already possess (HTML, JavaScript, and CSS) to build websites and web applications. With only a few tweaks to their code, developers who have already authored a similar extension for other browsers will be able to create an Opera extension easily.

Opera has also released a supporting API to help developers build Opera extensions much more easily.

Here is a quick example of the mini Wikipedia extension:

Nettuts+ extension

You can create numerous different types of Opera extensions:

  • Opera toolbar button: A basic extension that adds a button to Opera's toolbar.
  • Button + Popup: A button in Opera's toolbar, which, when clicked, displays a popup window with a third party URL. We will create this form of extension today.
  • Button + Badge: It is often used to display a count of items, such as unread mails or messages.
  • Manipulate Tabs and Windows: Opera provides developers with the ability to manipulate tabs and windows.
  • A bookmarklet extension: When clicked, executes a bookmarklet function from the background process and performs it on the current tab.

Step 0: Getting Started

For today's tutorial, we'll create a simple extension that adds a Nettuts+ button to Opera's toolbar. A popup window will display when the user clicks on this button. The user can then search for articles from the Nettuts+ website directly from this popup window. In addition, there are a list of Nettuts+ tutorials.

To get started, all we require is a copy of Opera 11, and a text editor or IDE of your choice. You can download Opera 11 here.


Step 1: Creating The Extension Configuration File

First, we will create the extension configuration file, which holds the meta data describing the extension. We can specify information such as the extension's name, author and icon here.

Create an XML file named config.xml, and then add the following code to it:

The <widget> element is the root element of the config.xml file. It contains four child elements (name, description, author, and icon).

  • The <name> element contains the name of the extension (Nettuts+)
  • The <description> element contains the description of the extension
  • In the <author> element, we enter the name of the extension's author (your name)
  • The <author> element may contain two attributes: href and email

In the <icon> element, we specify the location of the extension icon. The extensions manager and Opera extensions site will use this icon. Opera recommends using a 64×64 pixels icon.


Step 2: Creating the index.html File

Opera extensions require a start file, commonly called index.html. The index.html file is one of the only two mandatory parts of the extension package (the other is config.xml). This file is a bare bones HTML template with a script that creates the UI elements. The body of this document is not used.

Let's create the index.html file and then add the following script within the head of the document:

The script will create a toolbar button with a number of properties. A tooltip is created along with an 18×18 pixels icon. A popup belonging to the button is also created with a specified size, along with a reference to where the popup UI is defined. The script above will add a button to Opera''s toolbar when the extension loads.

In the ToolbarUIItemProperties object, we define the properties of the button. There are five properties that we can pass to the ToolbarUIItemProperties. The five properties are as follows:

  • disabled. This specifies whether the button is enabled or not. It is true (disabled) by default and accepts a boolean value.
  • title. This property definess the tooltip that is shown when the user hovers over the button.
  • icon. This property defines the icon used on the button. If you supply a size other than 18×18 pixels, it will scale the image to fit that size.
  • onclick. The function to execute when a user clicks on the button. We do not use this property on this extension.
  • onremove. The function to execute when the button is removed from the ToolbarContext. We also do not use this property.

After we've defined the properties of the button in the ToolbarUIItemProperties object, we create the actual button and apply the properties by using the createItem method:

Finally, we add the button to Opera's toolbar by using the addItem method:

Here is the complete code of the index.html file:


Step 3: Creating the Popup Window

In step two, above, when we created the index.html file, we defined the properties (the location and window's size) of the popup object within the ToolbarUIItemProperties object. This time, we only need to create the content of the popup window. This is just a HTML document, with its viewport set to the specified size. We can use HTML, CSS, JavaScript or any other web technology that we normally use on a web page.

Create a file called popup.html, and then add the following elements into the body:

Please note that we're using the onclick attribute here only for convenience's sake.

To control the style of the popup window, we add the following stylesheet into the head of the document:

Next, we add a JavaScript function into the head of the document to handle the Button1 click event:

Here is the completed code for the popup.html document:


Step 4: Packaging and Installing the Extension

Our extension is now finished! All that's left to do is to select all the files and zip them up. Once that is done, we can rename the zip file to Nettutsplus.oex (remember to replace the .zip extension with .oex) and we are done.

At this point, we can upload our extension to http://addons.labs.opera.com/.

To install it as a regular user, simply drag the extension (Nettutsplus.oex) into Opera, and it will ask you if you want to install it. After installing it, you will find the Nettuts+ button in Opera's toolbar.

Here is the Nettuts+ extension running on Opera 11:

Nettutsplus Extension

Conclusion

Opera extensions can vary from simple to extremely complex. After working along with this tutorial, I hope that you now possess the basic knowledge behind the process of creating Opera extensions. For more information about developing Opera extensions, please refer to the guide and documentation that is provided by Opera.

Happy Coding!

Tags:

Comments

Related Articles