> For the complete documentation index, see [llms.txt](https://docs.convai.com/api-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/vanilla-javascript-es-modules.md).

# 使用 JavaScript 模块嵌入体验

无需 React，使用该包提供的无框架客户端，通过 JavaScript 模块脚本渲染已发布的 Pixel Streaming 体验。

从一个 `<script type="module">` 中渲染已发布的 Convai Pixel Streaming 体验，无需 React，使用不依赖框架的 `PixelStreamClient` 类。完成后，页面会显示该体验的启动画面。

### 前提条件

* 已发布体验的 ID，以及一种授权方式：返回 Convai 身份验证令牌的服务器路由，或已加入 Convai 允许列表的域名。请参阅 [发布体验并进行授权](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/whitelisting-and-publishing-an-experience.md).
* 用于托管页面的 Web 服务器，因为示例会从同一服务器上的路由获取令牌。

### 客户端的打包方式

`PixelStreamClient` 位于 `@convai/experience-embed/core` 入口点。根入口点 `@convai/experience-embed`则导出 React 组件。core 入口点不包含 React 代码，并可解析为 ES 模块、CommonJS 和 UMD 脚本：

| 页面的加载方式                              | 导入内容                                                                |
| ------------------------------------ | ------------------------------------------------------------------- |
| 打包工具，例如 Vite、webpack 或 esbuild       | `import { PixelStreamClient } from '@convai/experience-embed/core'` |
| 不使用打包工具的 `<script type="module">` 标签 | 通过 URL 加载 ES 构建版本： `dist/core/convai-embed.es.js`                   |
| 普通的 `<script>` 标签                    | UMD 构建版本 `dist/core/convai-embed.umd.js`，它会定义全局变量 `PixelStreamCore` |

浏览器无法自行解析裸模块说明符 `@convai/experience-embed/core` ，因此不使用打包工具的模块脚本会通过 URL 导入文件，如下所示。

{% hint style="info" %}
0.7.1 之前的版本发布 `PixelStreamClient` 时仅提供 UMD 构建版本，因此 `import { PixelStreamClient } from '@convai/experience-embed/core'` 在打包工具和 Node 中都会失败。请升级到 0.7.1 或更高版本，或者导入 UMD 文件以触发其副作用，再读取 `globalThis.PixelStreamCore`.
{% endhint %}

### 创建客户端

{% code title="index.html" %}

```html
<div id="experience" style="width: 100%; height: 600px"></div>

<script type="module">
  import { PixelStreamClient } from 'https://unpkg.com/@convai/experience-embed/dist/core/convai-embed.es.js';

  const { token } = await fetch('/api/convai-token').then((response) => response.json());

  const experience = new PixelStreamClient({
    container: document.getElementById('experience'),
    expId: 'YOUR_EXPERIENCE_ID',
    authToken: token,
    onIframeLoaded: () => console.log('Experience loaded'),
  });
</script>
```

{% endcode %}

客户端会填满其容器，因此请为容器设置尺寸。如果 Convai 已将你的域名加入允许列表，而不是使用令牌，请省略 `authToken` 以及 fetch。

若要自行提供该文件，而不是从 unpkg 加载，请使用以下命令安装软件包： `npm install @convai/experience-embed` 然后从服务器公开该文件的位置导入同一个文件： `node_modules/@convai/experience-embed/dist/core/convai-embed.es.js`。使用打包工具时，导入 `@convai/experience-embed/core` 按名称引入，并让打包器解析它。

### 控制体验

客户端会显示一个 **点击以开始体验** 屏幕。如果要改为通过自己的控件启动体验，请传入一个 `InitialScreen` 元素，并从中调用 `experience.initializeExperience()` 一旦 `onIframeLoaded` 触发后，可以调用诸如 `experience.enableCamera()` 或 `experience.sendMessageToCharacter('Hello')`等方法。当你从页面中移除体验时，请调用 `experience.destroy()` 并清空容器。

此 [Pixel Streaming Embed API 参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/api-reference.md#pixelstreamclient-options) 列出每个选项、方法和回调。

### 验证嵌入内容

通过 Web 服务器打开页面，然后点击开始屏幕。

{% hint style="success" %}
体验内容会替换开始屏幕，浏览器控制台会记录 `Experience loaded`.
{% endhint %}

如果容器显示错误消息，请参阅 [故障排除](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/react-typescript.md#troubleshooting)；原因相同。

### 后续步骤

{% content-ref url="/pages/87b57936662c69d8802f20f500191b181163cf02" %}
[Pixel Streaming Embed API 参考](https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/api-reference.md)
{% endcontent-ref %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.convai.com/api-docs/zh/cha-jian-yu-ji-cheng/convai-pixel-streaming-embed/vanilla-javascript-es-modules.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
