Add-ons

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)
browser.tabs.onUpdated.removeListener(listener)
browser.tabs.onUpdated.hasListener(listener)

Events have three functions:

addListener(callback)
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 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:

  • "audible"
  • "discarded"
  • "favIconUrl"
  • "hidden"
  • "isarticle"
  • "mutedInfo"
  • "pinned"
  • "sharingState"
  • "status"
  • "title"
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.

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.

Browser compatibility

ChromeEdgeFirefoxFirefox for AndroidOpera
Basic support Yes144554 Yes
changeInfo.audible Yes144554 Yes
changeInfo.discarded Yes145757 Yes
changeInfo.favIconUrl Yes144554 Yes
changeInfo.mutedInfo Yes144554 Yes
changeInfo.pinned Yes144554 Yes
changeInfo.status Yes144554 Yes
changeInfo.title Yes145354 Yes
changeInfo.url Yes144554 Yes
extraParameters No No6161 No

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

Acknowledgements

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

Document Tags and Contributors

Contributors to this page: GuyXY, wbamberg, Makyen
Last updated by: GuyXY,