« Documentation Home

Opera Extension Windows API Guide

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.

Introduction

Opera extensions give you the power to add additional functionality right into the Opera desktop browser using web standards such as HTML, JavaScript and CSS. In this article, we'll look at how to use the extensions APIs to manipulate Opera desktop's windows system. This is done using the Windows object.

If you need to get grounding in the basics of Opera extensions before carrying on, the Saying Hello World article is a good place to start.

Contents

Creating Windows

As always, we use an event listener to listen out, and launch a function when an event is triggered, in this case once the window finishes loading. We then check if the function opera.extension.windows.create exists before calling it:

window.addEventListener( "load", function(){
  if( opera.extension.windows.create )
  {
  opera.extension.windows.create();
  } else {
    // Couldn't find an opera.extension.windows.create object.
  }
}, false);

Events in Windows

Events are fired when an interaction occurs on the Windows object. For example, if the Windows is in focus, an onfocus event is fired. In Opera extensions, there are four to play with:

As the events are fired, we use the opera.postError method to handle creating appropriate messages. You can read the opera.postError dumps in the Error Console (Tools > Advanced > Error Console).


window.addEventListener( "load", function(){
  if( opera.extension.windows )
  {
     opera.extension.windows.onfocus = function( event ){
    opera.postError("windows is focused");
    }
   opera.extension.windows.onclose = function( event ){
    opera.postError("windows is closed");
    }
      opera.extension.windows.oncreate = function( event ){
    opera.postError("windows is create");
    }
     opera.extension.windows.onblur = function( event ){
    opera.postError("windows loses focus");
    }

   } else {
    // couldn't find an opera.extension.windows object.
  }
}, false);

eventListeners in Windows

Next let's bring our attention to event listeners. Similar to a standard JavaScript event listener, you give an Opera extension event listener three arguments: the event handler, the function to fire and a boolean value. The allowed event types for an Opera extension event listener are as follows:

In the code snippet below, we register a focus event in an addEventListener function. The last argument determines the phase in which the event handler should be fired. It should be a boolean value and we will keep it false for now.

opera.extension.windows.addEventListener( "focus", function(){// do something}, false)

The difference between addEventListener and onevent lies in the difference between how the two event models work. For example this snippet will fire only the last line, since it has replaced the first line:

opera.extension.windows.onfocus = function(){ // do something };
opera.extension.windows.onfocus = function(){ // do something };

This second snippet will fire both, since they are both registered.

opera.extension.windows.addEventListener( "focus", function(){ // do something }, false);
opera.extension.windows.addEventListener( "focus", function(){ // do something }, false);

Conclusion

In this article we discussed how to manipulate Windows in the Opera desktop browser using the windows object.

Next, you can consider playing with buttons, badges and popups in Opera Extensions.

API reference

object opera.extension.windows