Simple Tab Closing Addon using WebExtension API

May 5, 2017

Before reading this post setup your Firefox so you can run WebExtensions in your browser.

In this blog we will be exploring more about how to close a tab quickly when we click on browserAction button (a small set of icons which is kept aside of address bars).

Step 1 : Defined Title and Icon

First we need to define the browserAction icon and browserAction title in the manifest.json I have defined as below

	
	
	"browser_action": {
	    "default_icon": "icons/page-32.png",
   	    "default_title": "Close New Tab"
  	}
	

Step 2: Listen to browserAction

The second step we have to do is to listen to the browserAction click. To listen to this click by default we have a listener

browser.browserAction.onClicked.addListener(listener)

Here the lisetener is a function where we will be passing the tab (tabs.Tab) object. As said we will pass the tab Object, when the user clicks on browserAction icon we will be sending the current active tab object to the callback function as argument.

Step 3: Defining the callback

So we have got the tab Object, so we know we can get its id by use tab.id.

Then after getting the tab id we can simply close it by using remove API browser.tabs.remove

browser.tabs.remove(tab.id);

My whole call back function is below


    function mylove(tab) {
   	console.log(tab.url+"---------"+tab.id);

	var removing = browser.tabs.remove(tab.id);

	console.log((")created sucessfully("));
	}

Checkout the code in github and share your thoughts.

Read Other Related Posts about : Mozilla

  • Creating ShortenURL Maker Addon
  • Creating Simple Easy Searcher Using ContextMenu
  • Simple Alarm with Notification
  • Comments

    comments powered by Disqus