docs.rodeo

MDN Web Docs mirror

tabs.onUpdated

Fired when a tab is updated.

When the user navigates to a new URL in a tab, this typically generates several onUpdated events as various properties of the {{WebExtAPIRef("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 also fires for changes to a tab’s properties that don’t involve navigation, such as 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, changes to particular properties, changes to a tab or window, or any combinations of these restrictions.

Syntax

browser.tabs.onUpdated.addListener(
  listener, // function
  filter     // optional object
)
browser.tabs.onUpdated.removeListener(listener)
browser.tabs.onUpdated.hasListener(listener)

Events have three functions:

addListener syntax

Parameters

Additional objects

changeInfo

Lists the changes to the state of the tab that is updated. To learn more about these properties, see the {{WebExtAPIRef("tabs.Tab")}}  documentation. Note that not all {{WebExtAPIRef("tabs.Tab")}}  properties are supported.

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: ", changeInfo);
  console.log("New tab Info: ", 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://mastodon.social/@mdn:

const pattern1 = "https://developer.mozilla.org/*";
const pattern2 = "https://mastodon.social/@mdn";

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 (that is, 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 only when the pinned property of tabs changes for tabs whose url property is matched by https://developer.mozilla.org/* or https://mastodon.social/@mdn:

const pattern1 = "https://developer.mozilla.org/*";
const pattern2 = "https://mastodon.social/@mdn";

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 when the pinned property of tabs changes for tabs whose url property is matched by https://developer.mozilla.org/* or https://mastodon.social/@mdn where the tab was part of the current browser window when the update event fired:

const pattern1 = "https://developer.mozilla.org/*";
const pattern2 = "https://mastodon.social/@mdn";

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

{{WebExtExamples}} 

Browser compatibility

{{Compat}} 

[!NOTE] This API is based on Chromium’s chrome.tabs API. This documentation is derived from tabs.json in the Chromium code.

{{AddonSidebar}} 

In this article

View on MDN