@castleio/castle-js

中文文档
2.8.5发布于 2026/06/26MITTypeScriptCommonJS零依赖近 30 天下载 136.4万

城堡指纹脚本

此软件包已被标记为弃用

Castle.js v2 is deprecated. Please upgrade to v3 — upgrade notes are available in the Castle dashboard.

中文使用文档
文档目录21 节
  1. 导言
  2. 安装
  3. 配置
  4. 应用程序的基本配置
  5. 浏览器版本( 遗留设置)
  6. 使用量
  7. 获取城堡请求令牌
  8. 窗体提交帮助示例( 当使用 cdn 版本时)
  9. 发送页面事件
  10. 发送窗体事件
  11. 窗体为窗体事件提交帮助
  12. 发送自定义事件
  13. 从1.x升级到2.0.x
  14. 1.x 运算
  15. 2.0.x (中文(简体) )
  16. 从2.0.x升级到2.1.x或稍后
  17. 2.0.x (中文(简体) )
  18. 2.1.x 或晚点
  19. 更改日志
  20. 最低要求:
  21. 许可证

城堡指纹脚本

包装版本 城堡 指纹脚本.

导言

Castle JavaScript 自动抓取您网页应用程序中的每个用户动作,包括点击,自带,刷新,格式提交,以及页面浏览等. 我们利用这些数据来构建良好用户行为的剖面图,以便检测坏处.

安装

npm install --save @castleio/castle-js

yarn add @castleio/castle-js

配置

应用程序的基本配置

// @option options [string] :pk castle publishable key.
// @option options [object] :window (default `window`) eg JSDOM.window
// @option options [string] :apiUrl (default `https://m.castle.io/v1/monitor`) castle api url
// @option options [number] :timeout (default 1000) castle api response timeout
// @option options [boolean] :verbose (default true) verbose mode (console warnings)
// @option options [object] :storage (default name `__cuid`, default expireIn 400 days in seconds: `34560000`) used for storing uuid in the localStorage and the cookies, and for setting cookie expiration time.

import * as Castle from '@castleio/castle-js'

Castle.configure(options);

浏览器版本( 遗留设置)

如果您的环境不支持模块, 您可以使用浏览器版本

import '@castleio/castle-js/dist/castle.browser.js'

Castle.configure(options);

使用量

获取城堡请求令牌

import * as Castle from '@castleio/castle-js'

Castle.createRequestToken().then( (requestToken) => {
  ....
});

// or
const requestToken = await Castle.createRequestToken();

见 城堡医生 需要更多信息和如何进一步传递信号.

窗体提交帮助示例( 当使用 cdn 版本时)

// @param event [Event] submit event
// @param onDone [Function] optional form submit callback replacement
<form action="/" onsubmit="Castle.injectTokenOnSubmit(event)">
  <button type="submit">Submit</button>
</form>

也可以直接获得帮助(在使用castle.umd.js时)

import * as Castle from '@castleio/castle-js'

const submitHandler = (evt) => Castle.injectTokenOnSubmit(evt);
<form action="/" onsubmit="submitHandler(event)">
  <button type="submit">Submit</button>
</form>

发送页面事件

// @param options [PageParams] castle page command params.
// @option user [UserParams] user object with the required `id`, optional `email`, `phone`, `registered_at`, `name`, `traits`
// @option userJwt [string] optiona jwt encoded UserParams
// @option name [String]
// @option referrer [String]
// @option url [String]

import * as Castle from '@castleio/castle-js'

const page = Castle.page(options);

事件响应可以以呼叫等承诺来验证.

import * as Castle from '@castleio/castle-js'

// result - true - success response from the server
// result - false - error response from the server, missing configuration or data
// result - null - timeout or debounced event (300ms)

Castle.page(options).then( (result) => { } )

发送窗体事件

// @param options [FormParams] castle form command params.
// @option user [UserParams] user object with the required `id`, optional `email`, `phone`, `registered_at`, `name`, `traits`
// @option userJwt [string] JWT encoded UserParams (can be passed instead of the user)
// @option name [String]
// @option values [Record<string, string>]

import * as Castle from '@castleio/castle-js'

Castle.form(options);

事件响应可以以呼叫等承诺来验证.

import * as Castle from '@castleio/castle-js'

// result - true - success response from the server
// result - false - error response from the server, missing configuration or data
// result - null - timeout or throttled event (300ms)

Castle.form(options).then( (result) => { } )

窗体为窗体事件提交帮助

import * as Castle from '@castleio/castle-js'

const user = YOUR_USER_DATA;
const submitHandler = (evt) => Castle.formEventOnSubmit(evt, user);
<form action="/" data-castle-name="Change Profile" onsubmit="submitHandler(event)">
  <input type="text" name="user_mail" data-castle-value="email">
  <button type="submit">Submit</button>
</form>

发送自定义事件

// @param options [CustomParams] castle custom command params.
// @option user [UserParams] user object with the required `id`, optional `email`, `phone`, `registered_at`, `name`, `traits`
// @option userJwt [string] JWT encoded UserParams (can be passed instead of the user)
// @option name [String]
// @option properties [Record<string,string>]

import * as Castle from '@castleio/castle-js'

const custom = Castle.custom(options);

事件响应可以以呼叫等承诺来验证.

import * as Castle from '@castleio/castle-js'

// result - true - success response from the server
// result - false - error response from the server, missing configuration or data
// result - null - timeout or debounced event (default 1000ms)

Castle.custom(options).then( (result) => { } )

中断更改

从1.x升级到2.0.x

1.x 运算

遗产包 : https://www.npmjs.com/package/castle.js (中文(简体) )

require "castle.js"
_castle('setAppId', "YOUR_APP_ID")
_castle('getClientId')

2.0.x (中文(简体) )

新建软件包 :

import * as Castle from "@castleio/castle-js"
  1. 重命名对象从 _castle 改为 Castle 在新模块中,但保留 _castle 在CDN版本以及浏览器专用模块中 @castleio/castle-js/dist/castle.browser.js
  2. 引入了请求符的概念,每个呼叫需要生成到卡塞尔的服务器侧API.
Castle.createRequestToken().then( (requestToken) => {
});

// or

const token = await Castle.createRequestToken();
  1. 请求符已经无法从 cookie 字符串中取出 .
  2. 取消了以下所有方法:

autoForwardClientId, (中文(简体) ), autoTrack, (中文(简体) ), catchHistoryErrors, (中文(简体) ), identify, (中文(简体) ), setUserId, (中文(简体) ), setAccount, (中文(简体) ), setKey, (中文(简体) ), setAccount, (中文(简体) ), sessionId, (中文(简体) ), reset, (中文(简体) ), page, (中文(简体) ), trackPageView, (中文(简体) ), setTrackerUrl

请求 Token现在需要在请求中被通过,它已经无法在饼干中使用。 检查 文档

从2.0.x升级到2.1.x或稍后

2.0.x (中文(简体) )

Castle.configure(YOUR_CASTLE_APP_ID);

2.1.x 或晚点

  1. 已删除 _castle 所有版本的对象, 现在只依赖 Castle。 。 。 .

  2. 切换到使用 可出版密钥 与现在已贬值的 App ID 相同.

Castle.configure({pk: YOUR_PUBLISHABLE_KEY});
  1. onFormSubmit 已更名为 injectTokenOnSubmit

  2. _castle 全球方法不再支持,已被替换为 Castle (用于CDN和castle.browser.js版本)检查 文档

  3. CDN版本不再需要 appID 在url和需要时

<script>Castle.configure({pk: YOUR_CASTLE_PUBLISHABLE_KEY});</script>

待补。 CDN版本不能用于生成请求符,而只能用于跟踪客户端-侧事件.

  1. 介绍 page, (中文(简体) ), form,以及 custom 客户端事件跟踪方法

更多信息可在 文档

更改日志

  • 2.8.5 - 固定状态 types 选项
  • 2.8.4 - 最新业绩
  • 2.8.3 - 最新业绩
  • 2.8.2 - 加强安全保障
  • 2.8.1 - 错误修正
  • 2.8.0 - 安全更新,取消对旧浏览器的官方支持(目标从ES3更新为ES5);更新最低要求
  • 2.7.1 - 无变化
  • 2.7.0 - 国家 createRequestToken, (中文(简体) ), page, (中文(简体) ), form,以及 custom 返回承诺(如果有的话),内部增强
  • 2.6.2 - 缩小捆绑尺寸、增加内部特性、增加链条支持 .then,以及使用 .catch 财务报告和审定财务报表 .finally 用于创建 RequestToken、页面、窗体和自定义
  • 2.6.1 - 无证件 getVersion 函数和临时饼干的减少使用,内部增强
  • 2.6.0 - 错误修正和内部特性
  • 2.5.3 - 安全最新情况
  • 2.5.2 - 内部释放
  • 2.5.1 - 扩展浏览器支持范围和错误修正
  • 2.5.0 - 提高机器人检测能力,改进性能,修复错误
  • 2.4.3 - 内部增强
  • 2.4.2 - 强制 https 显示器请求、错误修正、 utf 增强
  • 2.4.1 内部加强
  • 2.4.0 - 更大的一组增强和内部特性
  • 2.3.6 - 内部增强
  • 2.3.5 — d.ts 文件中含有导出类型的固定问题
  • 2.3.4 - 内部增强
  • 2.3.3-内部增强
  • 2.3.2 - 错误修正
  • 2.3.1 - 内部稳定
  • 2.3.0 - 新的配置选项 throttling,默认启用,并设置为500ms,用于在短时间内发送多个自定义事件.
  • 2.2.0 - 新的配置选项 storage,已贬值 storageNamespace和稳定改善.
  • 2.1.15 - 新的配置选项 storageNamespace,用于指定本地Storage和cookie存储命名空间,稳定性改进
  • 2.1.14. 改善稳定性
  • 2.1.13 - 未配置 pk 时抛出错误
  • 2.1.12 – 使 Castle.confile 返回 Castle 界面对象
  • 2.1.11 - 为用户添加页面、格式、自定义事件的地址支持
  • 2.1.10 - 错误修正和稳定性改进
  • 2.1.9 - 错误修正和稳定性改进
  • 2.1.8 - pk验证和内部增强
  • 2.1.7 - 页面命令错误修正和改进
  • 2.1.6 - 固定的JWT验证问题
  • 2.1.5 - 读取更新
  • 2.1.4 - 改善稳定性
  • 2.1.3 - 视图事件错误修正和稳定性改进
  • 2.1.2 - 视图事件错误修正
  • 2.1.1 - 内部增强和稳定性改进
  • 2.1.0 — 添加页面、 窗体、 自定义事件支持、 更改配置 DSL、 添加窗体EventOnSubmit 帮助器、 在FormSubmit上更名以注入TokenOnSubmit( 突破更改 - 检查) 文档 (中文(简体) )
  • 2.0.4 - 错误修正
  • 2.0.3 - 错误修正和稳定性改进
  • 2.0.2 - 错误修正
  • 2.0.1 - 内部增强
  • 2.0.0 - 从 1.x 运算,模块化,更新DSL,(突破更改 - 检查) 文档 (中文(简体) )

最低要求:

  • 铬 38+
  • Firefox 44+ (英语)
  • 歌剧院 32+
  • 边缘12+
  • 萨法里8+
  • 所有现代浏览器(桌面和移动)

许可证

麻省理工学院

版本与发布记录

@castleio/castle-js 共发布 53 个版本,首次发布于 2021/06/15;过去 12 个月发布了 4 个版本。

版本发布日期距今
2.8.5latest2026/06/264 个月前
2.8.42026/01/209 个月前
2.8.32026/01/0710 个月前
2.8.22025/11/1711 个月前
2.8.12025/10/071.1 年前
2.8.02025/09/181.1 年前

其他发布通道:rc → 2.1.0-rc.1。

关于 @castleio/castle-js 的常见问题

@castleio/castle-js 是什么?

城堡指纹脚本注意:该包已被作者标记为弃用——Castle.js v2 is deprecated. Please upgrade to v3 — upgrade notes are available in the Castle dashboard.

如何安装 @castleio/castle-js?

在项目目录运行 npm install @castleio/castle-js,或使用 pnpm add / yarn add / bun add。

@castleio/castle-js 支持 TypeScript 吗?

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

@castleio/castle-js 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@castleio/castle-js 可以商用吗?

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

@castleio/castle-js 的下载量有多少?

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

@castleio/castle-js 还在维护吗?

最新版本 2.8.5 发布于 2026/06/26(4 个月前);至今共发布 53 个版本,其中过去 12 个月发布了 4 个;首次发布于 2021/06/15。作者已将其标记为弃用,新项目建议寻找替代方案。