IntroductionOpera extensions are functional: you can control the browser buttons, default CSS, and many other features using web standards such as HTML, JavaScript, and CSS. In this article we will discuss how to manage tabs.
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 Tabs')
Let's start by creating a tab. At the beginning, we use the addEventListener method to intercept the document load event. After the document loads, we call the function.
Also, we check the presence of the object opera.extension.tabs before we use its functions.
window.addEventListener( "load", function(){ if( opera.extension.tabs.create )
Creating Address TabsThe opera.extension.tabs.create method takes as an optional argument a TabProperties object that contains the boolean activity of the tab and / or URL. 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 {
Focus TabsUsing the same idea of an interceptor of events and checking the existence of the object opera.extension.tabs, 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})
Close tabsClosing 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 {
What's next?That's all. Now you know how to create, edit and close tabs. You can refer to the
Opera Extensions API for complete information about the tabs object and its methods. You can also read an article about
managing browser windows from extensions.
API referenceobject
opera.extension.tabsExamplesCreateTab.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, when clicked, opens a new tab with the specified address.