@ciolabs/framecast

中文文档
0.3.0发布于 2026/08/05Apache-2.0 WITH Commons-ClauseTypeScriptESM + CJS构建来源已验证近 30 天下载 1.9万

TypeScript cross-frame communication library.

原始使用文档
文档目录13 节
  1. Install
  2. Broadcasts
  3. Example
  4. Functions
  5. Creating a function
  6. Calling a function
  7. Handling errors
  8. Shared State
  9. Creating shared state
  10. Example
  11. Evaluating arbitrary code
  12. Passing arguments
  13. API

@ciolabs/framecast

TypeScript cross-frame communication library

Install

npm install @ciolabs/framecast

Broadcasts

Broadcasts allow you to send one-way communications. The broadcast event is emitted when a valid broadcast is received.

You can add many listeners to the broadcast event.

The only argument contains an object with a deserialized message. All data sent via framecast must be valid JSON.

Example

Parent

import { Framecast } from '@ciolabs/framecast';

const target = document.querySelector('iframe').contentWindow;
const framecast = new Framecast(target);

framecast.on('broadcast', (message: any) => {
  console.log(message);
});

Child

import { Framecast } from '@ciolabs/framecast';

const target = window.parent;
const framecast = new Framecast(target);

framecast.broadcast('Hello world');

Functions

Framecast allows you to call functions across frames. The function:* event is emitted when the call is made. The returned value of the listener is passed back to the calling frame.

Note, unlike the broadcast event, function:* events can only have one listener. All data sent via framecast must be valid JSON.

Creating a function

Create a function by adding a listener for the function:* event where * is the name of the function. So in the example below the function name is getElementId so we name the event function:getElementId.

Child

import { Framecast } from '@ciolabs/framecast';

const target = window.parent;
const framecast = new Framecast(target);

framecast.on('function:getElementId', selector => {
  return document.querySelector(selector).getAttribute('id');
});

Calling a function

To call the function from another frame, we use call. Note, that all functions return a promise, even if the handler on the other end is a synchronous function.

Parent

import { Framecast } from '@ciolabs/framecast';

const target = document.querySelector('iframe').contentWindow;
const framecast = new Framecast(target);

const bodyId = await framecast.call('getElementId', 'body');

Handling errors

You handle errors the exact same way as if the function was in the same frame. Wrap the function call in a try/catch.

try {
  const bodyId = await framecast.call('getElementId', 'body');
} catch (error) {
  console.log('Something went wrong', error);
}

By default Framecast will throw an error if the handler take more than 10 seconds to complete. You can customize this with the config.functionTimeoutMs option.

There are times where you don't want a function to timeout for individual calls. You can use waitFor to call a function without a timeout. It will return a result and dispose function. The dispose function will remove the listener.

const { result, dispose } = framecast.waitFor('getElementId', 'body');

console.log(await result);

Shared State

Framecast has support for shared state between the parent and child frames. This is done by using nanostores.

Each state is an atom and can be subscribed to. When the state is updated in one frame it will be updated in the other frame.

Creating shared state

You can create a shared state by calling state on the framecast instance. The first argument is the name of the state and the second is the initial value.

const $counter = framecast.state('counter', 0);

$counter.subscribe(value => {
  console.log('counter', value);
});

On initial creation the state will be set to the initial value. If the state already exists in the other frame will be synced to the value of the other frame.

Note: you must subscribe to the state to mount it.

Example

In the following example when either the parent or child frame updates the $counter the other frame will be updated.

The implmentation is identical in both the parent and child frames.

Child

import { Framecast } from '@ciolabs/framecast';

const framecast = new Framecast(window.parent);

const $counter = framecast.state('counter', 0);

$counter.subscribe(value => {
  console.log('counter', value);
});

document.querySelector('button').addEventListener('click', () => {
  $counter.set($counter.get() + 1);
});

Parent

import { Framecast } from '@ciolabs/framecast';
import { persistentAtom, setPersistentEngine } from '@nanostores/persistent';

const framecast = new Framecast(document.querySelector('iframe').contentWindow);

$counter.subscribe(value => {
  console.log('counter', value);
});

Evaluating arbitrary code

Framecast has a built-in function named evaluate. This evaluates the given function in the context of the target window.

The framecast instance in the child must opt-in to this feature by setting config.supportEvaluate to true. Doing so comes with all of the security risks of eval() so think carefully before enabling this.

This was inspired by playwright's evaluate function.

Child

import { Framecast } from '@ciolabs/framecast';

const target = window.parent;
const framecast = new Framecast(target, { supportEvaluate: true });

Parent

import { Framecast } from '@ciolabs/framecast';

const target = document.querySelector('iframe').contentWindow;
const framecast = new Framecast(target);

const bodyId = await framecast.evaluate(() => document.querySelector('body').getAttribute('id'));

Passing arguments

You can pass arguments to the function by passing them as additional arguments to evaluate. Arguments can be any Serializable values.

import { Framecast } from '@ciolabs/framecast';

const target = document.querySelector('iframe').contentWindow;
const framecast = new Framecast(target);

const bodyId = await framecast.evaluate(selector => document.querySelector(selector).getAttribute('id'), 'body');

API

constructor(target: Window, config: FramecastConfig);


// broadcasts
on(type: 'broadcast', listener: (message: any) => void);
off(type: 'broadcast', listener: (message: any) => void);
broadcast(message: any);

// functions
on(type: `function:${string}`, listener: (...args: any[]) => void);
off(type: `function:${string}`, listener: (...args: any[]) => void);
call(type: `function:${string}`, ...args: any[]) => Promise<any>;
waitFor(type: `function:${string}`, ...args: any[]) => { result: Promise<any>, dispose: () => void };


// evaluate
evaluate<ReturnType = any>(fn: (...args: any[]) => ReturnType, ...args: any[]) => Promise<ReturnType>;


type FramecastConfig = {
  origin: string | null;
  channel: string | null;
  self: Window | null;
  functionTimeoutMs: number;
  supportEvaluate: boolean;
};

Inspired by Tabcast

版本与发布记录

@ciolabs/framecast 共发布 5 个版本,首次发布于 2025/10/01;过去 12 个月发布了 4 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.3.0latest2026/08/053 个月前
0.2.02026/03/307 个月前
0.1.02026/03/307 个月前
0.0.0-oidc-test-2025-11-172025/11/1811 个月前
0.0.12025/10/011.1 年前

关于 @ciolabs/framecast 的常见问题

@ciolabs/framecast 是什么?

TypeScript cross-frame communication library.

如何安装 @ciolabs/framecast?

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

@ciolabs/framecast 支持 TypeScript 吗?

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

@ciolabs/framecast 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@ciolabs/framecast 可以商用吗?

@ciolabs/framecast 使用 Apache-2.0 WITH Commons-Clause 许可。声明的许可为 Apache-2.0 WITH Commons-Clause,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@ciolabs/framecast 的下载量有多少?

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

@ciolabs/framecast 还在维护吗?

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