Fired when a tab is updated.
When the user navigates to a new URL in a tab, this will typically generate several
onUpdated
events as various properties of the
tabs.Tab
object are updated. This includes the
url
, but also potentially the
title
and
favIconUrl
特性。
status
property will cycle through
"loading"
and
"complete"
.
This event will also be fired for changes to a tab's properties that don't involve navigation, like pinning and unpinning (which updates the
pinned
property) and muting or unmuting (which updates the
audible
and
mutedInfo
properties).
You can filter this event, making it only fire for tabs whose urls match specific patterns , or for changes to specific properties, or for changes to a specific tab or window, or any combinations of these restrictions.
browser.tabs.onUpdated.addListener(listener[, extraParameters])
browser.tabs.onUpdated.removeListener(listener)
browser.tabs.onUpdated.hasListener(listener)
Events have three functions:
addListener(callback[, extraParameters])
Adds a listener to this event.
removeListener(listener)
Stop listening to this event. The
listener
argument is the listener to remove.
hasListener(listener)
校验是否
listener
is registered for this event. Returns
true
if it is listening,
false
否则。
callback
Function that will be called when this event occurs. The function will be passed the following arguments:
tabId
integer
. ID of the tab that was updated.
changeInfo
对象
. Contains properties for the tab properties that have changed. See
changeInfo
下文。
tab
tabs.Tab
. The new state of the tab.
extraParameters
可选
对象
. A set of filters that restricts the events that will be sent to this listener. This is an object which may have one or more of the following properties. Events will only be sent if they satisfy all the filters given.
urls
数组
. An array of
match patterns
. Fire the event only for tabs whose current
url
property matches any one of the patterns.
properties
数组
. An array of strings, which are the names of properties of the
tabs.Tab
object. Fire this event only for changes to one of the properties named in this array. The following properties may be listed here:
注意: The "url" value is supported since Firefox 88. In Firefox 87 and earlier, "url" changes can be observed by filtering by "status".
tabId
整数
. Fire this event only for the tab identified by this ID.
windowId
整数
. Fire this event only for tabs which are currently in the window identified by this ID.
Lists the changes to the state of the tab that was updated. To learn more about these properties, see the
tabs.Tab
文档编制。
attention
可选
boolean
. Indicates whether the tab is drawing attention. For example, when the tab displays a modal dialog,
attention
将是
true
.
audible
可选
boolean
. The tab's new audible state.
discarded
可选
boolean
. Whether the tab is 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.
favIconUrl
可选
string
. The tab's new favicon URL.
hidden
可选
boolean
. True if the tab is
hidden
.
isArticle
可选
boolean
. True if the tab is an article and is therefore eligible for display in
Reader Mode
.
mutedInfo
可选
tabs.MutedInfo
. The tab's new muted state and the reason for the change.
pinned
可选
boolean
. The tab's new pinned state.
status
可选
string
. The status of the tab. Can be either
loading
or
complete
.
title
可选
string
. The tab's new title.
url
可选
string
. The tab's URL if it has changed.
Listen for and log all the change info and new state:
function handleUpdated(tabId, changeInfo, tabInfo) {
console.log("Updated tab: " + tabId);
console.log("Changed attributes: ");
console.log(changeInfo);
console.log("New tab Info: ");
console.log(tabInfo);
}
browser.tabs.onUpdated.addListener(handleUpdated);
Log changes to URLs:
function handleUpdated(tabId, changeInfo, tabInfo) {
if (changeInfo.url) {
console.log("Tab: " + tabId +
" URL changed to " + changeInfo.url);
}
}
browser.tabs.onUpdated.addListener(handleUpdated);
Log changes only to tabs whose
url
特性为
matched
by "https://developer.mozilla.org/*" or "https://twitter.com/mozdevnet":
const pattern1 = "https://developer.mozilla.org/*";
const pattern2 = "https://twitter.com/mozdevnet";
const filter = {
urls: [pattern1, pattern2]
}
function handleUpdated(tabId, changeInfo, tabInfo) {
console.log(`Updated tab: ${tabId}`);
console.log("Changed attributes: ", changeInfo);
console.log("New tab Info: ", tabInfo);
}
browser.tabs.onUpdated.addListener(handleUpdated, filter);
Log changes only to the
pinned
property of tabs (i.e. pin and unpin actions):
const filter = {
properties: ["pinned"]
}
function handleUpdated(tabId, changeInfo, tabInfo) {
console.log(`Updated tab: ${tabId}`);
console.log("Changed attributes: ", changeInfo);
console.log("New tab Info: ", tabInfo);
}
browser.tabs.onUpdated.addListener(handleUpdated, filter);
Combine both the previous filters: log changes only:
pinned
property of tabs
url
特性为
matched
by "https://developer.mozilla.org/*" or "https://twitter.com/mozdevnet":
const pattern1 = "https://developer.mozilla.org/*";
const pattern2 = "https://twitter.com/mozdevnet";
const filter = {
urls: [pattern1, pattern2],
properties: ["pinned"]
}
function handleUpdated(tabId, changeInfo, tabInfo) {
console.log(`Updated tab: ${tabId}`);
console.log("Changed attributes: ", changeInfo);
console.log("New tab Info: ", tabInfo);
}
browser.tabs.onUpdated.addListener(
handleUpdated,
filter);
Log changes only:
pinned
property of tabs
url
特性为
matched
by "https://developer.mozilla.org/*" or "https://twitter.com/mozdevnet"
const pattern1 = "https://developer.mozilla.org/*";
const pattern2 = "https://twitter.com/mozdevnet";
const filter = {
urls: [pattern1, pattern2],
properties: ["pinned"],
windowId: browser.windows.WINDOW_ID_CURRENT
}
function handleUpdated(tabId, changeInfo, tabInfo) {
console.log(`Updated tab: ${tabId}`);
console.log("Changed attributes: ", changeInfo);
console.log("New tab Info: ", tabInfo);
}
browser.tabs.onUpdated.addListener(
handleUpdated,
filter);
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.
最后修改: , 由 MDN 贡献者