@chatcops/widget

中文文档
1.1.0发布于 2026/10/31MITTypeScriptES Module零依赖构建来源已验证

Embeddable AI chatbot widget - zero dependencies, Shadow DOM isolated

原始使用文档
文档目录11 节
  1. Install
  2. Script Tag (easiest)
  3. npm
  4. Modes
  5. Popup (default)
  6. Inline
  7. Configuration
  8. API
  9. Script Tag Attributes
  10. Features
  11. License

@chatcops/widget

Embeddable AI chatbot widget. Zero dependencies, Shadow DOM isolated, ~13KB gzipped.

Install

Script Tag (easiest)

<script
  src="https://cdn.jsdelivr.net/npm/@chatcops/widget@1/dist/chatcops.min.js"
  data-api-url="https://your-api.com/chat"
  data-accent="#6366f1">
</script>

The widget auto-initializes from data-* attributes on DOMContentLoaded.

npm

npm install @chatcops/widget
import ChatCops from '@chatcops/widget';

ChatCops.init({
  apiUrl: 'https://your-api.com/chat',
  theme: { accent: '#6366f1' },
});

Modes

Popup (default)

Floating action button (FAB) in the corner. Clicking it opens a chat panel.

ChatCops.init({
  apiUrl: '/chat',
  mode: 'popup',                    // default
  theme: { position: 'bottom-right' }, // or 'bottom-left'
});

Inline

Renders directly inside a container element. No FAB, no welcome bubble.

ChatCops.init({
  apiUrl: '/chat',
  mode: 'inline',
  container: '#chat-container',  // CSS selector or HTMLElement
});

Configuration

ChatCops.init({
  // Required
  apiUrl: 'https://your-api.com/chat',

  // Display mode
  mode: 'popup',                        // 'popup' | 'inline'
  container: '#chat',                   // required for inline mode

  // Theme
  theme: {
    accent: '#6366f1',                  // primary color
    textColor: '#ffffff',
    bgColor: '#0a0a0a',
    fontFamily: 'system-ui, sans-serif',
    borderRadius: '12px',
    tokens: {
      border: '#d0d4da',
      'panel-width': '440px',
    },
    position: 'bottom-right',           // 'bottom-right' | 'bottom-left'
  },

  // Branding
  branding: {
    name: 'Support Bot',
    avatar: 'https://example.com/avatar.png',
    subtitle: 'Online',
  },

  // Messages
  welcomeMessage: 'Hi! How can I help?',
  welcomeBubble: {
    text: 'Need help? Chat with us!',
    delay: 3000,                        // ms before showing
    showOnce: true,                     // once per browser ('local'), per tab ('session'), every load ('none')
  },
  placeholder: 'Type a message...',

  // Behavior
  persistence: 'local',                 // 'local' | 'session' | 'none', default: 'local'
  historyTtlDays: 30,                   // drop saved chats older than this
  maxMessages: 50,                      // max persisted messages
  pageContext: true,                     // send page URL/title to API
  autoOpen: 5000,                       // auto-open after ms (popup only)
  locale: 'en',
  privacyUrl: 'https://example.com/privacy', // link in header menu (https: or http:)

  // Callbacks
  onOpen: () => {},
  onClose: () => {},
  onMessage: (msg) => {},
  onError: (err) => {},
});

API

ChatCops.init(config)         // Initialize the widget
ChatCops.open()               // Open the chat panel (popup mode)
ChatCops.close()              // Close the chat panel
ChatCops.destroy()            // Remove widget and clean up
ChatCops.on(event, handler)   // Subscribe to events (returns unsubscribe function)
ChatCops.off(event, handler)  // Unsubscribe

Events: open, close, message, error, leadCaptured, preChatSubmit.

Script Tag Attributes

All configuration can be set via data-* attributes:

<script
  src="https://cdn.jsdelivr.net/npm/@chatcops/widget@1/dist/chatcops.min.js"
  data-api-url="/chat"
  data-mode="popup"
  data-accent="#6366f1"
  data-text-color="#ffffff"
  data-bg-color="#0a0a0a"
  data-font-family="system-ui"
  data-border-radius="12px"
  data-position="bottom-right"
  data-brand-name="Support Bot"
  data-brand-avatar="https://example.com/avatar.png"
  data-brand-subtitle="Online"
  data-welcome-message="Hi! How can I help?"
  data-welcome-bubble="Need help?"
  data-welcome-bubble-delay="3000"
  data-placeholder="Type here..."
  data-locale="en"
  data-auto-open="true"
  data-privacy-url="https://example.com/privacy">
</script>

Features

  • Shadow DOM - Fully isolated CSS/DOM, no conflicts with host site
  • SSE Streaming - Real-time streaming responses via Server-Sent Events
  • Markdown - Renders bold, italic, code blocks, links, and lists
  • Conversation Persistence - localStorage for history, sessionStorage for session ID
  • Theming - CSS custom properties, auto-derived surface colors from bgColor
  • Mobile Responsive - Full-screen panel on screens below 768px
  • i18n - Ships in English with custom override support for any language
  • Accessible - ARIA labels, semantic HTML, keyboard navigation

License

MIT

版本与发布记录

@chatcops/widget 共发布 15 个版本,首次发布于 2026/03/08;过去 12 个月发布了 15 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.1.0latest2026/10/312 天前
1.0.02026/10/313 天前
1.0.0-rc.0rc2026/10/304 天前
0.7.02026/10/304 天前
0.6.02026/10/2410 天前
0.5.02026/10/2014 天前

关于 @chatcops/widget 的常见问题

@chatcops/widget 是什么?

可嵌入的AI聊天机器人小部件-零依赖,影子DOM隔离

如何安装 @chatcops/widget?

在项目目录运行 npm install @chatcops/widget,或使用 pnpm add / yarn add / bun add。

@chatcops/widget 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@chatcops/widget 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@chatcops/widget 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=22。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@chatcops/widget 可以商用吗?

@chatcops/widget 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@chatcops/widget 还在维护吗?

最新版本 1.1.0 发布于 2026/10/31(2 天前);至今共发布 15 个版本,其中过去 12 个月发布了 15 个;首次发布于 2026/03/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。