docs.rodeo

MDN Web Docs mirror

tabs.query()

{{AddonSidebar}} 

Gets all tabs that have the specified properties, or all tabs if no properties are specified.

This is an asynchronous function that returns a Promise.

Syntax

let querying = browser.tabs.query(queryInfo)

Parameters

Return value

A Promise that will be fulfilled with an array of ``{{WebExtAPIRef(‘tabs.Tab’)}}  objects, containing information about each matching tab.

If any error occurs, the promise will be rejected with an error message.

Examples

Get all tabs:

function logTabs(tabs) {
  for (const tab of tabs) {
    // tab.url requires the `tabs` permission or a matching host permission.
    console.log(tab.url);
  }
}

function onError(error) {
  console.error(`Error: ${error}`);
}

browser.tabs.query({}).then(logTabs, onError);

Get all tabs in the current window:

function logTabs(tabs) {
  for (const tab of tabs) {
    // tab.url requires the `tabs` permission or a matching host permission.
    console.log(tab.url);
  }
}

function onError(error) {
  console.error(`Error: ${error}`);
}

browser.tabs.query({ currentWindow: true }).then(logTabs, onError);

Get the active tab in the current window:

function logTabs(tabs) {
  // tabs[0].url requires the `tabs` permission or a matching host permission.
  console.log(tabs[0].url);
}

function onError(error) {
  console.error(`Error: ${error}`);
}

browser.tabs
  .query({ currentWindow: true, active: true })
  .then(logTabs, onError);

Get tabs for all HTTP and HTTPS URLs under "mozilla.org" or any of its subdomains:

function logTabs(tabs) {
  for (const tab of tabs) {
    // tab.url requires the `tabs` permission or a matching host permission.
    console.log(tab.url);
  }
}

function onError(error) {
  console.error(`Error: ${error}`);
}

browser.tabs.query({ url: "*://*.mozilla.org/*" }).then(logTabs, onError);

{{WebExtExamples}} 

Browser compatibility

{{Compat}} 

[!NOTE] This API is based on Chromium’s chrome.tabs API. This documentation is derived from tabs.json in the Chromium code.

In this article

View on MDN