---
title: "嵌入评论"
description: "为普通 HTML、静态站点或 CMS 选择接入方式，同一站点和页面标识共享一个讨论串。"
---

> Documentation Index
> Fetch the complete documentation index at: https://blua.pwp.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# 嵌入评论

## 准备三个值

- `endpoint` 是浏览器可访问的 Blua 网关。例如 `https://blua.example`。把下面代码中的示例地址换成自己的地址。
- `site-key` 在 **设置 → 站点** 中创建。把承载文章的网站加入这个站点的允许来源，每行一个完整来源。
- `page-key` 标识一篇文章。用不会随标题、查询参数或显示语言改变的 ID。翻译版需要独立讨论时，可以为每个版本分配不同的键。

## HTML

```html
<script type="module" src="https://blua.example/embed.js"></script>
<blua-comments
  endpoint="https://blua.example"
  site-key="garden"
  page-key="spring-swallows"
  locale="zh"
></blua-comments>
```

每页加载一次模块，将元素放在要显示评论的位置。客户端路由切换文章时，同步更新 `page-key`。[框架接入](/docs/embed/frameworks)包含组件和 SSR 示例。

## iframe

在 CMS 的 HTML 或 iframe 区块中使用网关提供的 `/embed` 页面。`referrerpolicy="origin"` 允许网关验证嵌入来源。

```html
<iframe
  id="discussion"
  title="文章评论"
  src="https://blua.example/embed?siteKey=garden&amp;pageKey=spring-swallows&amp;locale=zh"
  referrerpolicy="origin"
  loading="lazy"
  height="640"
  style="width: 100%; border: 0"
></iframe>
```

固定高度的 iframe 可以滚动。宿主允许模块脚本时，可用网关附带的辅助模块自动调高。单页应用卸载 iframe 时调用返回的清理函数。

```html
<script type="module">
  import { connectBluaFrame } from 'https://blua.example/embed/host.js';
  const disconnect = connectBluaFrame(document.querySelector('#discussion'));
</script>
```

如果宿主统一清除来源信息，可以在 iframe URL 中加入已登记且经过 URL 编码的 `parentOrigin`。在宿主的 CSP 中允许网关作为 `frame-src`。

Source: https://blua.pwp.sh/docs/embed/
