browserAction.setBadgeText()

设置浏览器操作的徽章文本。徽章显示在图标顶部。

没有特定徽章文本的标签页将继承全局徽章文本,默认情况下为 ""

语法

js
browser.browserAction.setBadgeText(
  details // 对象
)

此 API 也可通过 chrome.browserAction.setBadgeText() 来使用。

参数

details

一个包含以下属性的对象:

text

stringnull。可以传递任意数量的字符,但空间只能容纳大约四个字符。

如果不需要徽章,请使用空字符串 ""

如果指定了 tabIdnull 将移除特定标签页的徽章文本,使该标签页继承全局徽章文本。否则将全局徽章文本恢复为 ""

如果指定了 windowIdnull 将移除特定窗口的徽章文本,使标签页继承全局徽章文本。否则将全局徽章文本恢复为 ""

tabId 可选

integer。仅为指定的标签页设置徽章文本。当用户将该标签页导航到新页面时,文本会重置。

windowId 可选

integer。为指定的窗口设置徽章文本。

  • 如果同时提供 windowIdtabId,函数将失败。
  • 如果 windowIdtabId 都省略,则会设置全局徽章文本。

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Firefox for Android
Safari on iOS
setBadgeText
details.windowId parameter
The text property of the details parameter can be set to null.

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
Has more compatibility info.

示例

添加一个徽章,显示用户点击按钮的次数:

js
let clicks = 0;

function increment() {
  browser.browserAction.setBadgeText({ text: (++clicks).toString() });
}

browser.browserAction.onClicked.addListener(increment);

示例扩展

备注: 此 API 基于 Chromium 的 chrome.browserAction API。该文档衍生自 Chromium 代码中的 browser_action.json