Extensions for Opera: Windows

Original author: Zi Bin Cheah
  • Transfer
Introduction

Extensions for Opera give you the opportunity to add functionality directly to the desktop Opera browser, using such web standards as HTML, JavaScript and CSS. In this article, we will look at how to use the extension APIs to control the browser window system. This is done through the Windows object.

If you need to familiarize yourself with the basics of extensions for Opera, then the article “ Your First Extension for Opera ” is a good place to start.

Creating windows

As usual, we use the event hook to launch the function when the event fires, in this case, when the window finishes loading. Then we check if the opera.extension.windows.create function exists before calling it:

window.addEventListener( "load", function(){
  if( opera.extension.windows.create )
  {
  opera.extension.windows.create();
  } else {
    // Функция opera.extension.windows.create не найдена
  }
}, false);

Events in Windows

Events are raised at the moment of interaction with the Window object. For example, if the window is in focus, the onfocus event is raised. There are four such events in the extensions for Opera:
  • onfocus
  • onclose
  • oncreate
  • onblur

When events are raised, we use the opera.postError method to notify about this. You can observe the output of opera.postError in the 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 {
    // Объект opera.extension.windows не найден
  }
}, false);

Event interceptors in Windows

Now we turn our attention to event interceptors. Like the standard event hooks in JavaScript, you pass three arguments to the event hook in the extension for Opera: the event, the function to execute, and the boolean value. You can intercept the following types of events:
  • focus
  • close
  • create
  • update
  • blur

In the following code example, we register the focus event using the addEventListener function. The last argument determines the phase in which the event handler should be launched. It should be a Boolean value and at the moment we will set its value to false.

opera.extension.windows.addEventListener( "focus", function(){// делаем что-нибудь}, false)

The difference between addEventListener and onevent is how these two event models work. For example, this code will only execute the last line, since it will replace the first.

opera.extension.windows.onfocus = function(){ // делаем что-нибудь };
opera.extension.windows.onfocus = function(){ // делаем что-нибудь };

The following example will perform both functions since they are both registered:

opera.extension.windows.addEventListener( "focus", function(){ // делаем что-нибудь }, false);
opera.extension.windows.addEventListener( "focus", function(){ // делаем что-нибудь }, false);

Conclusion

In this article, we looked at how to manage Windows in the Opera desktop browser using the windows object. For more information, you can refer to the extension API for Opera .

API reference opera.extension.windows Update

object . A couple of examples: CreateWindow.oex - creates a button that, when clicked, opens a new window. Events.oex - posts to the Error Console about events that occur with the browser window.




Also popular now: