Skip to content

Commit

Permalink
zh-CN: create notifications.onShown (#25070)
Browse files Browse the repository at this point in the history
Co-authored-by: A1lo <[email protected]>
  • Loading branch information
PassionPenguin and yin1999 authored Dec 23, 2024
1 parent 821fc43 commit 272b254
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
title: notifications.onShown
slug: Mozilla/Add-ons/WebExtensions/API/notifications/onShown
l10n:
sourceCommit: b8a0743ca8b1e1b1b1a95cc93a4413c020f11262
---

{{AddonSidebar}}

当通知显示后立即触发。

## 语法

```js-nolint
browser.notifications.onShown.addListener(listener)
browser.notifications.onShown.removeListener(listener)
browser.notifications.onShown.hasListener(listener)
```

事件有三个函数:

- `addListener(listener)`
- : 为此事件添加一个监听器。
- `removeListener(listener)`
- : 停止监听此事件。`listener` 参数是要移除的监听器。
- `hasListener(listener)`
- : 检查是否已为此事件注册了 `listener`。若正在监听,返回 `true`,否则返回 `false`

## addListener 语法

### 参数

- `listener`

- : 当此事件发生时调用的函数。该函数传入此参数:

- `notificationId`
- : `string`。已显示的通知的 ID。

## 浏览器兼容性

{{Compat}}

## 示例

`notifications.onShown` 事件添加一个监听器并记录其详细信息:

```js
function logShown(itemId) {
console.log(`消息已显示:${itemId}`);
browser.notifications.getAll().then((all) => {
console.log(all[itemId]);
});
}

browser.notifications.onShown.addListener(logShown);
```

{{WebExtExamples}}

> [!NOTE]
> 该 API 基于 Chromium 的 [`chrome.notifications`](https://developer.chrome.google.cn/docs/extensions/reference/api/notifications) API。

0 comments on commit 272b254

Please sign in to comment.