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
.
let querying = browser.tabs.query(queryObj)
queryObj
对象
。
query()
function will only get tabs whose properties match the properties included here.
见
tabs.Tab
documentation to learn more about these properties.
active
可选
boolean
. Whether the tabs are active in their windows.
audible
可选
boolean
. Whether the tabs are audible.
autoDiscardable
可选
boolean
. Whether the tabs can be discarded automatically by the browser when resources are low.
cookieStoreId
可选
string
or
array
of
string
. Use this to return tabs whose
tab.cookieStoreId
matches any of the
cookieStoreId
strings. This option is only available if the add-on has the
"cookies"
permission
.
currentWindow
可选
boolean
. Whether the tabs are in the current window.
discarded
可选
boolean
. Whether the tabs are discarded. A discarded tab is one whose content has been unloaded from memory, but is still visible in the tab strip. Its content gets reloaded the next time it's activated.
hidden
可选
boolean
. Whether the tabs are hidden.
highlighted
可选
boolean
. Whether the tabs are highlighted.
index
可选
integer
. The position of the tabs within their windows.
muted
可选
boolean
. Whether the tabs are muted.
lastFocusedWindow
可选
boolean
. Whether the tabs are in the last focused window.
pinned
可选
boolean
. Whether the tabs are pinned.
status
可选
tabs.TabStatus
. Whether the tabs have completed loading.
title
可选
string
. Match page titles against a pattern. Requires the "tabs" permission or
host permissions
for the tab to match.
url
可选
string
or
array
of
string
. Match tabs against one or more
match patterns
. Note that fragment identifiers are not matched. Requires the "tabs" permission or
host permissions
for the tab to match.
windowId
可选
integer
。
id
of the parent window, or
windows.WINDOW_ID_CURRENT
for the current window.
windowType
可选
tabs.WindowType
. The type of window the tabs are in.
A
Promise
that will be fulfilled with an
array
of
objects, containing information about each matching tab.
tabs.Tab
If any error occurs, the promise will be rejected with an error message.
Get all tabs:
function logTabs(tabs) {
for (let tab of tabs) {
// tab.url requires the `tabs` permission or a matching host 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 or a matching host 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 or a matching host 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
"mozilla.org"
or any of its subdomains:
function logTabs(tabs) {
for (let tab of tabs) {
// tab.url requires the `tabs` permission or a matching host permission.
console.log(tab.url);
}
}
function onError(error) {
console.log(`Error: ${error}`);
}
let querying = browser.tabs.query({url: "*://*.mozilla.org/*"});
querying.then(logTabs, onError);
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.
最后修改: , 由 MDN 贡献者