Extensions for Opera: Tabs

Original author: Zi Bin Cheah
  • Transfer
Introduction

Extensions for Opera are functional: you can control browser buttons, default CSS and many other features using such web standards as HTML, JavaScript and CSS. In this article, we will go over how to manage tabs.

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 Tabs

Let's start by creating a tab. First, use the addEventListener method to intercept the document load event. After the document is loaded, we call the function.

We also check for the existence of the opera.extension.tabs object before we use its functions.

window.addEventListener( "load", function(){
  if( opera.extension.tabs.create ) //проверяем существование метода
  {
    opera.extension.tabs.create();  // создаём вкладку
  } 
  else {
  	//ничего не делаем
    }
}, false);

Creating Tabs with an Address

The opera.extension.tabs.create method takes as an optional argument a TabProperties object that contains the Boolean value of the tab activity and / or URL. By specifying the URL, we can create a tab that will load the specified address after opening.

window.addEventListener( "load", function(){
  if( opera.extension.tabs.create )
  {
   opera.extension.tabs.create({url:"http://www.opera.com/"});
  } else {
    //ничего не делаем
  }
}, false);

Tabs Focus

Using the same idea of ​​an event hook and checking the opera.extension.tabs object, we can manipulate tabs in various ways. First of all, let's see how to create an active tab:

opera.extension.tabs.create({focused:true})

Note translator: Apparently, this is the default behavior. If you want to create a tab in the background, set focused to false.

Now, create an active tab with the specified URL:

opera.extension.tabs.create({url:"http://www.opera.com/",focused:true})

Closing tabs Closing tabs is

just as easy. Let's try this: create a tab and close it in a second.

window.addEventListener( "load", function(){
  if( opera.extension.tabs )
  {
    var tab = opera.extension.tabs.create({url:"http://www.opera.com/",focused:true});
    window.setTimeout( function(){
      opera.extension.tabs.close( tab );
    }, 1000);
  } else {
  // Не найден объект opera.extension.tabs
  }
}, false);

What's next?

That's all. Now you know how to create, modify and close tabs. You can refer to the extension APIs for Opera for complete information about the tabs object and its methods. You can also read an article on managing browser windows from extensions.

API reference opera.extension.tabs
object Examples CreateTab.oex - creates a button that, when clicked, opens a new tab. CreateTabInBG.oex - creates a button that, when clicked, opens a new tab in the background. CreateTabWithUrl.oex - creates a button that, when clicked, opens a new tab with the given address.





Also popular now: