选项卡. 查询 ()

[阿登侧边栏()]

获取具有指定属性的所有选项卡,如果未指定任何属性,则获取所有选项卡。

这是返回 的异步函数。Promise

语法

let querying = browser.tabs.query(queryObj)

参数

queryObj

object.函数将仅获取其属性与此处包含的属性匹配的选项卡。query()

请参阅 \WebExtAPIRef("选项卡")。Tab")=文档以了解有关这些属性的详细信息。

active[optional_inline]
boolean.选项卡是否在窗口中处于活动状态。
audible[optional_inline]
boolean.标签是否可听见。
autoDiscardable[optional_inline]
boolean.当资源不足时,浏览器是否可以自动丢弃选项卡。
cookieStoreId[optional_inline]
string.使用此仅返回其 Cookie 存储 ID 为 的选项卡。此选项仅在加载项具有权限时才可用cookieStoreId"cookies"
currentWindow[optional_inline]
boolean.选项卡是否在当前窗口中。
discarded[optional_inline]
boolean.是否丢弃选项卡。丢弃的选项卡是其内容已从内存中卸载,但仍在选项卡条中可见的选项卡。下次激活时,其内容将重新加载。
hidden[optional_inline]
boolean.选项卡是否隐藏。
highlighted[optional_inline]
boolean.选项卡是否突出显示。
index[optional_inline]
integer.选项卡在其窗口中的位置。
muted[optional_inline]
boolean.选项卡是否为静音。
lastFocusedWindow[optional_inline]
boolean.选项卡是否在上一个焦点窗口中。
pinned[optional_inline]
boolean.选项卡是否固定。
status[optional_inline]
{WebExtAPIRef('选项卡。TabStatus ')=。选项卡是否已完成加载。
title[optional_inline]
string.将页面标题与图案匹配。
url[optional_inline]
string或。将选项卡与一个或多个匹配模式匹配。请注意,片段标识符不匹配。array of string
windowId可选
integer. The of the parent window, or windows.WINDOW_ID_CURRENT for the current window.id
windowType可选
tabs.WindowType. The type of window the tabs are in.

Return value

A that will be fulfilled with an of objects, containing information about each matching tab.Promisearraytabs.Tab

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

Examples

Get all tabs:

function logTabs(tabs) {
  for (let tab of tabs) {
    // tab.url requires the `tabs` permission
    console.log(tab.url);
  }
}

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

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

Get all tabs in the current window:

function logTabs(tabs) {
  for (let tab of tabs) {
    // tab.url requires the `tabs` permission
    console.log(tab.url);
  }
}

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

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

Get the active tab in the current window:

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

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

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

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

function logTabs(tabs) {
  for (let tab of tabs) {
    // tab.url requires the `tabs` permission
    console.log(tab.url);
  }
}

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

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

Example extensions

Browser compatibility

BCD tables only load in the browser

Acknowledgements

This API is based on Chromium's chrome.tabs API. This documentation is derived from tabs.json in the Chromium code.

Microsoft Edge compatibility data is supplied by Microsoft Corporation and is included here under the Creative Commons Attribution 3.0 United States License.