📜  chrome.tabs.query( - Javascript (1)

📅  最后修改于: 2023-12-03 15:29:59.401000             🧑  作者: Mango

Introduction to chrome.tabs.query()

chrome.tabs.query() is a method in the Chrome extension API that allows developers to retrieve information about open tabs in the browser. This method can be used to get details like the URL, ID, title, and other attributes of the tabs.

The basic syntax of chrome.tabs.query() is as follows:

chrome.tabs.query(queryInfo, callback)

Here, queryInfo is an object that specifies the properties of the tabs to match, and callback is a function that is called with the result of the query.

Query Parameters

The queryInfo object can have the following properties:

  • active: Whether the tab is currently active in the window.
  • audible: Whether the tab is currently playing audio.
  • discarded: Whether the tab is currently discarded from memory.
  • favIconUrl: The URL of the tab's favicon.
  • highlighted: Whether the tab is highlighted in the window.
  • muted: Whether the tab is currently muted.
  • pinned: Whether the tab is currently pinned.
  • status: The status of the tab (loading, complete, etc.).
  • title: The title of the tab.
  • url: The URL of the tab.
  • windowId: The ID of the window containing the tab.

For example, we can retrieve all active tabs with the following query:

chrome.tabs.query({ active: true }, function(tabs) {
  console.log(tabs);
});

This will log an array of tab objects to the console, each containing information about an active tab.

Result Object

The callback function is called with an array of tab objects that match the query parameters. Each tab object has the following properties:

  • active: Whether the tab is currently active in the window.
  • audible: Whether the tab is currently playing audio.
  • discarded: Whether the tab is currently discarded from memory.
  • favIconUrl: The URL of the tab's favicon.
  • height: The height of the tab.
  • highlighted: Whether the tab is highlighted in the window.
  • id: The ID of the tab.
  • incognito: Whether the tab is in an incognito window.
  • index: The index of the tab within its window.
  • mutedInfo: Information about whether the tab is currently muted.
  • pinned: Whether the tab is currently pinned.
  • selected: Whether the tab is currently selected in the window.
  • status: The status of the tab (loading, complete, etc.).
  • title: The title of the tab.
  • url: The URL of the tab.
  • width: The width of the tab.
  • windowId: The ID of the window containing the tab.

For example, we can log the titles of all active tabs with the following code:

chrome.tabs.query({ active: true }, function(tabs) {
  tabs.forEach(function(tab) {
    console.log(tab.title);
  });
});
Conclusion

In conclusion, chrome.tabs.query() is a powerful tool for Chrome extension developers that allows for the retrieval of information about open tabs in the browser. By specifying query parameters, developers can retrieve detailed information about specific tabs and use that information to build more robust extensions.