W3cubDocs

/Web Extensions

tabs.onUpdated

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 properties. The 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.

Syntax

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)
Check whether listener is registered for this event. Returns true if it is listening, false otherwise.

addListener syntax

Parameters

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
object. Contains properties for the tab properties that have changed. See changeInfo below.
tab
tabs.Tab. The new state of the tab.
extraParametersOptional

object. 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
Array. An array of match patterns. Fire the event only for tabs whose current url property matches any one of the patterns.
properties

Array. 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:

  • "attention"
  • "audible"
  • "discarded"
  • "favIconUrl"
  • "hidden"
  • "isArticle"
  • "mutedInfo"
  • "pinned"
  • "sharingState"
  • "status"
  • "title"
  • "url"

Note: The "url" value is supported since Firefox 88. In Firefox 87 and earlier, "url" changes can be observed by filtering by "status".

tabId
Integer. Fire this event only for the tab identified by this ID.
windowId
Integer. Fire this event only for tabs which are currently in the window identified by this ID.

Additional objects

changeInfo

Lists the changes to the state of the tab that was updated. To learn more about these properties, see the tabs.Tab documentation.

attention Optional
boolean. Indicates whether the tab is drawing attention. For example, when the tab displays a modal dialog, attention will be true.
audibleOptional
boolean. The tab's new audible state.
discarded Optional
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.
favIconUrlOptional
string. The tab's new favicon URL.
hiddenOptional
boolean. True if the tab is hidden.
isArticleOptional
boolean. True if the tab is an article and is therefore eligible for display in Reader Mode.
mutedInfoOptional
tabs.MutedInfo. The tab's new muted state and the reason for the change.
pinnedOptional
boolean. The tab's new pinned state.
statusOptional
string. The status of the tab. Can be either loading or complete.
titleOptional
string. The tab's new title.
urlOptional
string. The tab's URL if it has changed.

Examples

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);

Filtering examples

Log changes only to tabs whose url property is 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:

  • to the pinned property of tabs
  • whose url property is 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:

  • to the pinned property of tabs
  • whose url property is matched by "https://developer.mozilla.org/*" or "https://twitter.com/mozdevnet"
  • and which are part of the current browser window at the time the update event is fired:
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);

Example extensions

Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
onUpdated
Yes
14
45
?
Yes
14
?
?
54
?
?
?
extraParameters
No
No
61
?
No
No
?
?
No
?
?
?

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

© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/tabs/onUpdated