📜 ⬆️ ⬇️

Extensions for Opera: Windows

Introduction

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

If you need to familiarize yourself with the basics of Opera extensions, then the article “ Your first Opera extension ” is a good starting point.

Creating windows
')
As usual, we use event interceptor to trigger a function at the time the event fires, in this case, when the window finishes loading. Then we 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 { //  opera.extension.windows.create   } }, false); 

Events in Windows

Events are fired at the moment of interaction with the Window object. For example, if the window is in focus, the onfocus event is raised. In extensions for Opera there are four such events:

When events are raised, we use the opera.postError method to notify about it. You can see 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); 

Window Event Interceptors

Now we turn our attention to event interceptors. Like 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 a boolean value. You can intercept the following types of events:

In the following code example, we register the focus event using the addEventListener function. The last argument specifies the phase in which the event handler should be launched. This 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 performs both functions, since they both are 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 Opera's desktop browser using the windows object. For more information, you can refer to the Opera Extensions API .

API reference

object opera.extension.windows

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

Source: https://habr.com/ru/post/106913/


All Articles