browserAction.setTitle()

设置浏览器操作的标题。标题会显示在浏览器操作图标的工具提示中。你可以传入一个 tabIdwindowId 作为可选参数——如果这样做,则标题仅为指定的标签页或窗口更改。没有特定标题的标签页或窗口将继承全局标题文本,该文本默认为清单(manifest)中指定的 default_titlename

语法

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

参数

details

object。新标题和可选的目标标签页或窗口 ID。

title

stringnull。鼠标悬停时浏览器操作应显示的字符串。

如果 title 是空字符串,则使用扩展名作为标题,但 browserAction.getTitle 仍会提供空字符串。

如果 titlenull

  • 如果指定了 tabId,并且该标签页设置了特定的标签页标题,则该标签页将继承其所属窗口的标题。
  • 如果指定了 windowId,并且该窗口设置了特定的窗口标题,则该窗口将继承全局标题。
  • 否则,全局标题将重置为清单中的标题。
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
setTitle
details.windowId parameter
The title 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
Partial support
Partial support
No support
No support
Has more compatibility info.

示例

此代码在每次用户点击浏览器操作时,在“this”和“that”之间切换标题:

js
function toggleTitle(title) {
  if (title === "this") {
    browser.browserAction.setTitle({ title: "that" });
  } else {
    browser.browserAction.setTitle({ title: "this" });
  }
}

browser.browserAction.onClicked.addListener(() => {
  let gettingTitle = browser.browserAction.getTitle({});
  gettingTitle.then(toggleTitle);
});

示例扩展

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