JavaScript SDK
在网站中集成 OpenBait JavaScript SDK
OpenBait JavaScript SDK 可以帮助您在前端检测用户是否处于钓鱼页面,并采取保护措施。
安装
通过 npm 安装:
npm install @openbait/sdk
或使用 CDN:
<script src="https://cdn.openbait.com/sdk/v1/openbait.min.js"></script>
基础用法
import { OpenBait } from '@openbait/sdk'
const openbait = new OpenBait({
apiKey: 'your_api_key',
domain: 'example.com'
})
// 初始化检测
openbait.init()
// 监听检测结果
openbait.on('phishing_detected', (result) => {
console.log('检测到钓鱼风险:', result)
// 显示警告或阻止表单提交
})
配置选项
| 选项 | 类型 | 默认值 | 描述 |
|---|---|---|---|
apiKey | string | - | 您的 API Key |
domain | string | - | 保护的域名 |
checkInterval | number | 5000 | 检测间隔(毫秒) |
showWarning | boolean | true | 是否显示内置警告 |
事件
| 事件名 | 描述 |
|---|---|
phishing_detected | 检测到钓鱼风险 |
safe | 页面安全 |
error | 检测出错 |
高级用法
自定义警告 UI
openbait.on('phishing_detected', (result) => {
// 禁用内置警告
openbait.config.showWarning = false
// 显示自定义警告
showCustomWarning(result)
})
阻止表单提交
document.querySelector('form').addEventListener('submit', async (e) => {
e.preventDefault()
const isSafe = await openbait.check()
if (isSafe) {
e.target.submit()
} else {
alert('检测到安全风险,请确认您在正确的网站')
}
})