OA0 = Omni AI 0
OA0 是一个探索 AI 的论坛
现在注册
已注册用户请  登录
OA0  ›  技能包  ›  webchat-audio-notifications:为网页版聊天添加浏览器音频通知功能

webchat-audio-notifications:为网页版聊天添加浏览器音频通知功能

 
  chrome ·  2026-02-06 03:17:59 · 3 次点击  · 0 条评论  

名称: webchat-audio-notifications
描述: 为 Moltbot/Clawdbot 网页聊天添加浏览器音频通知,提供 5 种强度级别——从轻声细语到不容错过(仅在标签页处于后台时播放)。
版本: 1.1.0
作者: brokemac79
仓库: https://github.com/brokemac79/webchat-audio-notifications
主页: https://github.com/brokemac79/webchat-audio-notifications
标签:
- webchat
- notifications
- audio
- ux
- browser
- howler
元数据:
clawdbot:
emoji: 🔔
compatibility:
minVersion: "2026.1.0"
browsers:
- Chrome 92+
- Firefox 90+
- Safari 15+
- Edge 92+
dependencies:
- howler.js (已包含)
files:
- client/howler.min.js
- client/notification.js
- client/sounds/notification.mp3
- client/sounds/alert.mp3
install:
- kind: manual
label: 安装网页聊天音频通知
instructions: |
1. 将文件复制到您的网页聊天目录:
- client/howler.min.js → /webchat/js/
- client/notification.js → /webchat/js/
- client/sounds/ → /webchat/sounds/

      2. 在网页聊天 HTML 的 `</body>` 标签前添加:

      ```html
      <script src="/js/howler.min.js"></script>
      <script src="/js/notification.js"></script>
      <script>
        const notifier = new WebchatNotifications({
          soundPath: '/sounds/notification'
        });
        notifier.init();
      </script>
      ```

      3. 在消息事件中调用通知:

      ```javascript
      socket.on('message', () => {
        if (notifier) notifier.notify();
      });
      ```

      4. 通过切换标签页并触发消息进行测试

      完整指南请参阅 `docs/integration.md`。

🔔 网页聊天音频通知

为 Moltbot/Clawdbot 网页聊天提供浏览器音频通知。当新消息到达时播放通知音效——但仅在标签页处于后台时触发。

功能特性

  • 🔔 智能通知 - 仅在标签页隐藏时播放
  • 🎚️ 音量控制 - 可调节 0-100%
  • 🎵 5 种强度级别 - 从轻声细语 (1) 到不容错过 (5)
  • 📁 自定义音效 - 支持上传自定义音效 (MP3, WAV, OGG, WebM)
  • 🔕 一键开关 - 轻松启用/禁用
  • 💾 设置持久化 - 偏好设置保存在 localStorage 中
  • 📱 移动端友好 - 在移动设备上优雅降级
  • 🚫 自动播放处理 - 遵循浏览器策略
  • ⏱️ 冷却时间 - 防止消息轰炸(警报间隔 3 秒)
  • 🐞 调试模式 - 可选日志记录

快速开始

测试概念验证 (POC)

cd examples
python3 -m http.server 8080
# 打开 http://localhost:8080/test.html

测试步骤:
1. 切换到另一个标签页
2. 点击“触发通知”
3. 听到声音! 🔊

基础集成

// 初始化
const notifier = new WebchatNotifications({
  soundPath: './sounds',
  soundName: 'level3',  // 中等强度(默认)
  defaultVolume: 0.7
});

await notifier.init();

// 在新消息时触发
socket.on('message', () => notifier.notify());

// 为不同事件使用不同级别
socket.on('mention', () => {
  notifier.setSound('level5');  // 提及消息使用最响亮的音效
  notifier.notify();
});

API

构造函数选项

new WebchatNotifications({
  soundPath: './sounds',               // 音效目录路径
  soundName: 'level3',                 // level1 (轻声) 到 level5 (非常响亮)
  defaultVolume: 0.7,                  // 0.0 到 1.0
  cooldownMs: 3000,                    // 警报之间的最短时间
  enableButton: true,                  // 显示启用提示
  debug: false                         // 控制台日志记录
});

强度级别说明:
- level1 - 轻声 (9.5KB) - 最轻微
- level2 - 柔和 (12KB) - 温和
- level3 - 中等 (13KB) - 默认
- level4 - 响亮 (43KB) - 引人注意
- level5 - 非常响亮 (63KB) - 不容错过

方法

  • init() - 初始化(在 Howler 加载后调用)
  • notify(eventType?) - 触发通知(仅在标签页隐藏时)
  • test() - 立即播放音效(忽略标签页状态)
  • setEnabled(bool) - 启用/禁用通知
  • setVolume(0-1) - 设置音量
  • setSound(level) - 更改强度('level1' 到 'level5')
  • getSettings() - 获取当前设置

浏览器兼容性

浏览器 版本 支持情况
Chrome 92+ ✅ 完全支持
Firefox 90+ ✅ 完全支持
Safari 15+ ✅ 完全支持
移动端 最新版 ⚠️ 有限支持

总体情况: 92% 的用户(支持 Web Audio API)

文件结构

webchat-audio-notifications/
├── client/
│   ├── notification.js       # 主类 (10KB)
│   ├── howler.min.js         # 音频库 (36KB)
│   └── sounds/
│       ├── level1.mp3        # 轻声 (9.5KB)
│       ├── level2.mp3        # 柔和 (12KB)
│       ├── level3.mp3        # 中等 (13KB, 默认)
│       ├── level4.mp3        # 响亮 (43KB)
│       └── level5.mp3        # 非常响亮 (63KB)
├── examples/
│   └── test.html            # 包含所有级别的独立测试页面
├── docs/
│   └── integration.md       # 集成指南
└── README.md                # 完整文档

集成指南

完整指南请参阅 docs/integration.md,内容包括:
- 分步设置说明
- Moltbot 特定钩子
- React/Vue 示例
- 常见模式(@提及、请勿打扰、徽章)
- 测试清单

配置示例

简单配置

const notifier = new WebchatNotifications();
await notifier.init();
notifier.notify();

高级配置

const notifier = new WebchatNotifications({
  soundPath: '/assets/sounds',
  soundName: 'level2',  // 从柔和音效开始
  defaultVolume: 0.8,
  cooldownMs: 5000,
  debug: true
});

await notifier.init();

// 普通消息 = 柔和
socket.on('message', () => {
  notifier.setSound('level2');
  notifier.notify();
});

// 提及消息 = 非常响亮
socket.on('mention', () => {
  notifier.setSound('level5');
  notifier.notify();
});

// 私信 = 响亮
socket.on('dm', () => {
  notifier.setSound('level4');
  notifier.notify();
});

配合 UI 控件

<input type="range" min="0" max="100" 
       onchange="notifier.setVolume(this.value / 100)">
<button onclick="notifier.test()">测试 🔊</button>

故障排除

没有声音?
- 先点击页面(自动播放限制)
- 检查标签页是否确实隐藏
- 确认音量 > 0
- 检查控制台是否有错误

标签页激活时也播放声音?
- 启用调试模式
- 检查是否有“标签页可见,跳过”消息
- 如果缺失,请报告为 bug

移动端不工作?
- iOS 要求每次播放都需要用户手势
- 考虑视觉回退方案(闪烁网站图标)

性能

  • 包大小: 总计约 122KB(已压缩)
  • 内存: 播放期间约 2MB
  • CPU: 可忽略不计(浏览器原生支持)
  • 网络: 一次性下载,已缓存

安全性

  • ✅ 无外部请求
  • ✅ 仅使用 localStorage
  • ✅ 无跟踪
  • ✅ 无需特殊权限

许可证

MIT 许可证

致谢

贡献指南

  1. 使用 examples/test.html 进行测试
  2. 启用调试模式
  3. 报告问题时请附带浏览器和控制台输出信息

路线图

  • [ ] WebM 格式(更小的文件)
  • [ ] 按事件定制音效(提及、私信等)
  • [ ] 视觉回退(网站图标闪烁)
  • [ ] 系统通知 API
  • [ ] 设置 UI 组件
  • [ ] 请勿打扰模式

状态: ✅ v1.1.0 完成 - 5 种强度级别
测试环境: Chrome, Firefox, Safari
适用于: 生产环境 & ClawdHub 发布

链接

3 次点击  ∙  0 人收藏  
登录后收藏  
目前尚无回复
0 条回复
About   ·   Help   ·    
OA0 - Omni AI 0 一个探索 AI 的社区
沪ICP备2024103595号-2
Developed with Cursor