tabs.query()

[阿登侧边栏()]

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

这是返回 的异步函数。Promise

语法

let querying = browser.tabs.query(queryObj)

参数

queryObj

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

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

active 可选

boolean.选项卡是否在窗口中处于活动状态。

audible 可选

boolean.标签是否可听见。

autoDiscardable 可选

boolean.当资源不足时,浏览器是否可以自动丢弃选项卡。

cookieStoreId 可选

string.使用此仅返回其 Cookie 存储 ID 为 的选项卡。此选项仅在加载项具有权限时才可用cookieStoreId cookies

currentWindow 可选

boolean.选项卡是否在当前窗口中。

discarded 可选

boolean.是否丢弃选项卡。丢弃的选项卡是其内容已从内存中卸载,但仍在选项卡条中可见的选项卡。下次激活时,其内容将重新加载。

hidden 可选

boolean.选项卡是否隐藏。

highlighted 可选

boolean.选项卡是否突出显示。

index 可选

integer.选项卡在其窗口中的位置。

muted 可选

boolean.选项卡是否为静音。

lastFocusedWindow 可选

boolean.选项卡是否在上一个焦点窗口中。

pinned 可选

boolean.选项卡是否固定。

status 可选

{WebExtAPIRef('选项卡。TabStatus ')=。选项卡是否已完成加载。

title 可选

string.将页面标题与图案匹配。

url 可选

string或。将选项卡与一个或多个匹配模式匹配。请注意,片段标识符不匹配。array of string

windowId可选

integer. The of the parent window, or windows.WINDOW_ID_CURRENT (en-US) for the current window.id

windowType可选

tabs.WindowType (en-US). The type of window the tabs are in.

Return value

A Promise that will be fulfilled with an array of 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 (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

备注: 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.