@frillco/script

中文文档
0.1.3发布于 2026/07/12MITTypeScriptES Module零依赖近 30 天下载 5900

Type-safe SDK for embedding Frill widgets, surveys and announcements.

原始使用文档
文档目录10 节
  1. Install
  2. Usage
  3. Loading eagerly
  4. Identifying users
  5. Controlling a widget
  6. Surveys
  7. React / Next.js
  8. Content-Security-Policy (nonce)
  9. Hooks
  10. Embedding a widget

@frillco/script

Type-safe SDK for embedding Frill widgets, surveys and boosted announcements.

Full documentation: developers.frill.co

Install

npm install @frillco/script

Usage

Start by loading your container — it creates the widgets, surveys and boosted announcements you've configured in your Frill dashboard, and is the recommended entry point for most apps.

frill is the typed callable (a drop-in for window.Frill). Calling it loads the Frill script on first use, so calling frill('container', …) up front also loads Frill eagerly.

import { frill } from '@frillco/script';

// Load your container (optionally identifying the current user)
await frill('container', {
  key: 'YOUR_CONTAINER_KEY',
  user: { name: 'Mikey Hill', email: '[email protected]' },
});

// Open a specific widget on demand
const widget = await frill('widget', { key: 'YOUR_WIDGET_KEY' });
widget.open();

Loading eagerly

To start loading the Frill script before you make your first call, use loadFrill (similar to loadStripe). Pass domain: 'frill.eu' for EU data residency.

import { loadFrill } from '@frillco/script';

loadFrill(); // begins loading immediately
// loadFrill({ domain: 'frill.eu' });

Identifying users

// Guest user
await frill('identify', {
  name: 'Mikey Hill',
  email: '[email protected]',
  attributes: { mrr: 100 },
});

// SSO (JWT from your server)
await frill('identify', { ssoToken: 'SSO_JWT' });

// Log out
await frill('unidentify');

Controlling a widget

const widget = await frill('widget', { key: 'YOUR_WIDGET_KEY' });

widget.open();
widget.viewSection('roadmap');
widget.events.on('badgeCount', ({ count }) => console.log(`${count} unread`));

Surveys

const survey = await frill('survey', { key: 'YOUR_SURVEY_KEY', force: true });
survey.open();

React / Next.js

Render <FrillScript /> once (e.g. in your root layout) to load the container before hydration.

import Script from 'next/script';
import { FrillScript } from '@frillco/script/react';

// app/layout.tsx
<FrillScript
  as={Script}
  strategy="beforeInteractive"
  container={{
    key: 'YOUR_CONTAINER_KEY',
    user: { name: 'Mikey Hill', email: '[email protected]' },
  }}
/>;

Content-Security-Policy (nonce)

Running a strict CSP? Pass a nonce. It's applied to the <FrillScript> tag, the container script it injects, and every script/style tag the SDK injects thereafter — so Frill loads without 'unsafe-inline'.

<FrillScript nonce={nonce} container={{ key: 'YOUR_CONTAINER_KEY' }} />

loadFrill({ nonce }) does the same for the imperative API.

Hooks

useWidget returns the widget instance (or null until it's ready), re-rendering when it loads — so effects can reliably subscribe via widget.events.

import { useWidget } from '@frillco/script/react';

function FeedbackButton() {
  const widget = useWidget({ key: 'YOUR_WIDGET_KEY' });

  React.useEffect(() => {
    if (!widget) return;

    const unsubscribe = widget.events.on('badgeCount', ({ count }) => {
      console.log(`${count} unread`);
    });

    return () => {
      unsubscribe();
    };
  }, [widget]);

  return <button onClick={() => widget?.open()}>Feedback</button>;
}

useSurvey and useContainer mirror useWidget (for containers, prefer <FrillScript container={…} /> when you can). useFrill() returns the frill callable for imperative use in event handlers.

Embedding a widget

Render a widget inline (into a <div>, not a modal launcher):

import { FrillWidgetEmbed } from '@frillco/script/react';

<FrillWidgetEmbed widgetKey="YOUR_WIDGET_KEY" style={{ width: 340, height: 460 }} />;

版本与发布记录

@frillco/script 共发布 4 个版本,首次发布于 2026/07/10;过去 12 个月发布了 4 个版本。

版本发布日期距今
0.1.3latest2026/07/124 个月前
0.1.22026/07/114 个月前
0.1.12026/07/104 个月前
0.1.02026/07/104 个月前

关于 @frillco/script 的常见问题

@frillco/script 是什么?

Type-safe SDK for embedding Frill widgets, surveys and announcements.

如何安装 @frillco/script?

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

@frillco/script 支持 TypeScript 吗?

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

@frillco/script 支持 ESM 还是 CommonJS?

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

@frillco/script 需要什么版本的 Node.js?

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

@frillco/script 可以商用吗?

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

@frillco/script 的下载量有多少?

根据 npm 官方下载统计,@frillco/script 在 2026/10/16 – 2026/11/14 这 30 天内被下载 5,900 次,日均约 197 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@frillco/script 还在维护吗?

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