Opera extensions: windows

By Zi Bin Cheah

IMPORTANT: Please note that the the Opera extensions Windows functionality is currently incomplete, and undergoing a serious overhaul. At the moment, we would advise against using it - wait until we have completed the update, or use it at your own risk.

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:

  • onfocus
  • onclose
  • oncreate
  • onblur

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:

  • focus
  • close
  • create
  • update
  • blur

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. For more information, consult the Opera extensions API reference.

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

API reference

object opera.extension.windows

Web Evangelist in the Developer Relations Team at Opera Software ASA.


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

Comments

The forum archive of this article is still available on My Opera.

  • photo

    D1sasterp1ece

    Wednesday, March 7, 2012

    Does JS need to be packed as an extension for that I can access tabs & window manipulation methods, or can the code be just run as a userscript?
No new comments accepted.