Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
See the Notes on Browser Extension Development column for a series of articles
Reference: chrome tabs
Chrome provides apis prefixed with Chrome. tabs, which are page-related methods.
These methods usually return the Tab object, which contains the following common attributes:
active
Boolean value for whether the TAB is the currently active TAB in the window in which it is locatedaudible
Boolean (optional), whether the TAB generated a sound in the last few seconds (if a sound is playing, a speaker icon 🔈 is displayed on the right side of the TAB)mutedInfo
Object (optional), whether the TAB is muted and mutedreasonfavIconUrl
String (optional), favicon icon URL of the URL displayed on the TAB page. To access this property, you need a configuration listing that appears in the extensionmanifest.json
The configuration of the itempermissions
In a statementtabs
Permissions. Value of this property if the TAB page is loading a web pageIt could be an empty string.groupId
Value, the tag group to which the TAB page belongswidth
Value (optional), the width of the TAB page, in pixelspx
height
Value (optional), the height of the TAB page, in pixelspx
id
Value (optional), in the browser window, each TAB ID is unique, but there are some cases, such as usingsessions
APIWhen querying a label page, the label object does notDon’tAn ID value is assignedincognito
Boolean value, whether the TAB is in stealth modeindex
Value, position of TAB page in browser window, index from0
starttitle
String (optional) the title of the page displayed on the TAB page. To access this property, you need a configuration listing that appears in the extensionmanifest.json
The configuration of the itempermissions
In a statementtabs
Permissions.url
String (optional) the URL of the page displayed on the TAB page. List of configurations that need to appear in the extensionmanifest.json
The configuration of the itempermissions
In a statementtabs
Permissions.
💡 also uses bookmarks’ bookmarks-related apis when developing tabs related extensions
Create a new TAB
Use the method chrome.tabs. Create (createProperties: Object) to create a new label. This is an asynchronous function that immediately returns a Promise and eventually returns the created Tab object, Tab.
This method takes as an input a tag-associated configuration object with the following common properties:
active
Boolean (optional) whether the newly created TAB is the current active TABindex
Value (optional), the order of all tabs in the current window, indexed from0
starturl
(Optional) a string that accesses the given URL after creating a new TABwindowId
Value (optional) creates a new TAB in the specified window, which is opened by default in the currently active window
// Encapsulate a method to create a new TAB page using Chrome's API
// It takes an active parameter that controls whether the newly created TAB is the currently active TAB
const createNewTab = async (active = true) = > {const newTab = await chrome.tabs.create({
  active,
});
 return newTab;
};
Copy the code
Gets the currently active TAB page
Query (queryInfo: object) is an asynchronous function that immediately returns a Promise and finally returns an array of Tab objects that meet the criteria.
// Encapsulates a method to get the currently active TAB page using Chrome's API
const getActiveTab = async() = > {CurrentWindow the active TAB of the currentWindow
 // Since this method returns an array with only one active TAB, we can use deconstruction to get a unique element of the array
 const [tab] = await chrome.tabs.query({
  active: true.currentWindow: true});return tab;
};
Copy the code