~/writings/api-exploration-tabs
Sep 21, 2024·6 min read

Chrome API Exploration: Tabs API

Learn more about the Tabs API and how to use it in your Chrome Extensions

RFRyan Fitzgerald·@rfitzio

I orginially published this article on ExtensionKit.

Introduction

The Chrome Tabs API is a powerful tool for developers who want to interact with Chrome's tab system. In this blog post, we'll explore the key features of the Tabs API and provide practical examples to help you master tab management in your Chrome extensions.

What is the Tabs API?

The Tabs API provides a way to interact with the browser's tabs, meaning developers can create, modify, rearrange,and remove tabs programmatically. In addition to managing tabs, this API can also detect the language of the current tab, take screenshots, and more. This API is essential for building powerful and flexible Chrome extensions that can manage multiple tabs and windows.

You can view the full API reference here.

Relevant Permissions

By default, there are no permissions required to use the Tabs API. However, depending on the functionality you want to add to your extension, you may need to request additional permissions for more sensitive data in tabs.

The following permissions can be added to your manifest.json file to gain more access to the Tabs API.

tabs permission

This permission is used to call the tabs.query() function and return more sensitive information on the tabs.Tab object. This includes: url, title, favIconUrl, and pendingUrl.

Host permissions

Adding host permissions to your manifest.json file allows your extension to read and query against a matching tab for the sensitive information listed above in the tabs permission.

activeTab permission

The activeTab permission allows your extension to have temporary host permissions for the currently active tab. The primary benefit of this permission is, unlike the host permissions, this permission does not trigge any warnings, but is limited to the active tab only.

Essential functions

The Tabs API comes with a variety of functions that allow for tab management that can be found in the API reference. Here are some of the most notable and highly used ones:

You can find all the properties on the tabs.Tab object here.

Essential events

The Tabs API also comes with a variety of events that allow for listening to changes in the browser's tabs. Here are some of the most notable and highly used ones:

Examples

Opening a new tab with a specified URL

chrome.tabs.create({
  url: "https://www.google.com",
}, function (newTab) {
  console.log("New tab created with ID: ", newTab.id);
});

Closing the current tab

chrome.tabs.query({ 
  currentWindow: true, active: true 
}, function(tabs) {
  chrome.tabs.remove(tabs[0].id);
});

Opening an extension page after the extension is installed

chrome.runtime.onInstalled.addListener(({ reason }) => {
  if (reason === 'install') {
    chrome.tabs.create({
      url: chrome.runtime.getURL("some-onboarding-page.html")
    });
  }
});

ExtensionKit

If you'd like to see more examples of the Tabs API and many others in action, feel free to checkout my project ExtensionKit. It contains a variety of battle-tested starter templates to help you get started on your next Chrome Extension project.