> ## Documentation Index
> Fetch the complete documentation index at: https://docs.festtide.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 运行时 JS API

> window.Festtide、它的事件和错误码。

Script 标签会安装 `window.Festtide`。可用它响应活动（例如接入您自己的分析工具）、传递同意状态，或移除页面
上的所有内容。

```ts theme={null}
interface FesttideRuntime {
  /** The runtime's version, e.g. "1.0.3". */
  readonly version: string
  /** Resolves after the first render decision, or after the 1.5 s give-up. Never rejects. */
  readonly ready: Promise<void>
  /** Listens to an event; returns a function that stops listening. */
  on(event: RuntimeEventName, handler: (detail: RuntimeEventDetail) => void): () => void
  /** Consent for counting (views, clicks). Rendering never needs consent. */
  consent(granted: boolean): void
  /** Campaign ids live on this page right now. */
  active(): string[]
  /** Removes everything the runtime added to the page, until the next page load. */
  pause(): void
}

type RuntimeEventName =
  | "ready"
  | "campaign:start"
  | "campaign:end"
  | "banner:view"
  | "banner:click"
  | "banner:dismiss"
  | "offer:copy"
  | "error"

interface RuntimeEventDetail {
  campaignId?: string
  /** For "error": a short code. */
  code?: string
}
```

脚本以异步方式加载，因此在您自己的代码运行时，`window.Festtide` 可能还不存在。请先检查它是否存在，如下面的
示例所示。

## 属性和方法

### `version`

运行时的版本字符串。提交错误报告时很有用。

### `ready`

一个 Promise：当运行时决定好在本页显示什么，或在 1.5 秒后放弃时完成。它从不 reject，因此您始终可以
`await` 它。

```js theme={null}
await window.Festtide?.ready
console.log(window.Festtide?.active())
```

### `on(event, handler)`

每当 `event` 发生时，以一个详情对象调用 `handler`。返回一个用于移除该处理函数的函数。

```js theme={null}
const off = window.Festtide?.on("banner:click", ({ campaignId }) => {
  myAnalytics.track("campaign_banner_click", { campaignId })
})

// 之后停止监听：
off?.()
```

### `consent(granted)`

告诉运行时是否可以统计浏览和点击。渲染从不需要同意。参见[同意](https://docs.festtide.com/zh/consent.md)。

### `active()`

当前在本页生效的活动 ID。没有生效的活动、页面被排除，或调用 `pause()` 之后，返回空数组。

### `pause()`

移除运行时添加到页面的所有内容（横幅、特效、主题颜色），直到下一次页面加载。若要对所有访客在所有页面停止
活动，请改用控制台中的**全部暂停**。

## 事件

| 事件 | 触发时机 | 详情 |
| - | - | - |
| `ready` | 做出首次渲染决定，或运行时已放弃。 | – |
| `campaign:start` | 某个活动开始在页面上显示。 | `campaignId` |
| `campaign:end` | 某个活动停止显示（时间段结束，或调用了 `pause()`）。 | `campaignId` |
| `banner:view` | 横幅被显示。 | `campaignId` |
| `banner:click` | 横幅按钮被点击。 | `campaignId` |
| `banner:dismiss` | 访客关闭了横幅。 | `campaignId` |
| `offer:copy` | 访客复制了优惠码。 | `campaignId` |
| `error` | 有问题使运行时停止；页面按原样显示。 | `code` |

## 错误码

所有错误都会保持页面原样：访客看到的是您的正常页面，最后一份有效的时间线会保留到下一次加载。

| 错误码 | 含义 |
| - | - |
| `fetch-timeout` | 时间线未在 1.5 秒内到达。 |
| `bad-signature` | 时间线签名验证失败，已被忽略。 |
| `unknown-key` | 时间线使用了此运行时不认识的密钥签名。请更新插件，或从控制台复制当前的 script 标签。 |
| `expired` | 时间线已过期。系统会自动重新发布；如果问题持续，请联系支持。 |
| `bad-json` | 无法读取响应。 |
| `blocked` | 请求被拦截，例如被内容安全策略或内容拦截器拦截。 |

```js theme={null}
window.Festtide?.on("error", ({ code }) => {
  console.warn("Campaigns not shown:", code)
})
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.