使用fingerprintjs获取设备指纹

  |   0 评论   |   0 浏览

fingerprintjs是一款开源的获取设备指纹的js库,在github上有接近20K的星星了,还是比较可靠的。下面是其github地址

https://github.com/fingerprintjs/fingerprintjs

API参考

安装

该库支持所有流行的安装方法:

浏览器 ECMAScript 模块

<script>
  // Initialize the agent at application startup.
  // You can also use https://openfpcdn.io/fingerprintjs/v3/esm.min.js
  const fpPromise = import('https://openfpcdn.io/fingerprintjs/v3')
    .then(FingerprintJS => FingerprintJS.load())

  // Get the visitor identifier when you need it.
  fpPromise
    .then(fp => fp.get())
    .then(result => console.log(result.visitorId))
</script>

运行这段代码

浏览器<script>标签

在加载期间暂停其他脚本的同步代码,因此不推荐:

<!-- Note that we use iife.min.js -->
<script src="https://openfpcdn.io/fingerprintjs/v3/iife.min.js"></script>
<script>
  // Initialize the agent at application startup.
  var fpPromise = FingerprintJS.load()

  // Analyze the visitor when necessary.
  fpPromise
    .then(fp => fp.get())
    .then(result => console.log(result.visitorId))
</script>

UMD

require(
  ['https://openfpcdn.io/fingerprintjs/v3/umd.min.js'],
  FingerprintJS => {
    // Initialize the agent at application startup.
    const fpPromise = FingerprintJS.load()

    // Get the visitor identifier when you need it.
    fpPromise
      .then(fp => fp.get())
      .then(result => console.log(result.visitorId))
  }
)

Webpack/Rollup/NPM/Yarn

# Install the package first:
npm i @fingerprintjs/fingerprintjs
# or
yarn add @fingerprintjs/fingerprintjs
import FingerprintJS from '@fingerprintjs/fingerprintjs'

// Initialize an agent at application startup.
const fpPromise = FingerprintJS.load()

;(async () => {
  // Get the visitor identifier when you need it.
  const fp = await fpPromise
  const result = await fp.get()
  console.log(result.visitorId)
})()

运行这段代码

当您运行与 NPM 或 Yarn 一起安装的 FingerprintJS 时,该库将向 FingerprintJS 服务器发送 AJAX 请求以收集使用情况统计信息。load函数运行时,有 0.1% 的机会发送请求。请求每周最多从一个浏览器实例发送一次(除非浏览器缓存已清除)。请求包含以下信息:

  • 库版本
  • 客户端发送的 HTTP 标头,包括库运行的页面的来源和引用者
  • 客户端的IP

您可以使用以下选项关闭这些请求monitoring

const fpPromise = FingerprintJS.load({
+ monitoring: false
})

💡 从我们的 CDN ( https://openfpcdn.io ) 下载的脚本默认禁用监控。

如果 FingerprintJS 文件中出现 TypeScript 错误,请参阅TypeScript 支持指南

CommonJS 语法(已过时):

const FingerprintJS = require('@fingerprintjs/fingerprintjs')

// Initialize the agent at application startup.
const fpPromise = FingerprintJS.load()

// Get the visitor identifier when you need it.
fpPromise
  .then(fp => fp.get())
  .then(result => console.log(result.visitorId))

应用程序编程接口

FingerprintJS.load({ delayFallback?: number, debug?: boolean, monitoring?: boolean }): Promise<Agent>

构建 Agent 实例并等待正确操作所需的延迟。我们建议尽快致电。delayFallback是一个可选参数,为不支持requestIdleCallback的浏览器设置回退的持续时间(毫秒) ;它有一个很好的默认值,我们不建议更改。debug: true将调试消息打印到控制台。monitoring: false禁用库发送到 FingerprintJS 服务器以收集使用统计信息的 AJAX 请求(在 CDN 版本中始终禁用)。

agent.get(): Promise<GetResult>

Agent 实例的一种获取访问者标识符的方法。我们建议您稍后在真正需要标识符时调用它,以增加获得准确标识符的机会。返回的对象格式:

interface GetResult {
  visitorId: string
  confidence: {
    score: number
    comment?: string
  }
  components: {
    [key: string]:
      { value: any, duration: number } |
      { error: object, duration: number }
  }
  version: string
}

返回的对象字段:

  • visitorId访客标识符
  • confidencescore置信度分数。这是一个介于 0 和 1 之间的数字,表示代理对访客标识符的确定程度。数字越大,访客标识符真实的可能性就越高。
  • confidencecomment置信度分数的附加信息。人类可读的文本。
  • components形成标识符的组件的字典。键是组件名称。value是一个组件值(如果成功)。error是一个错误对象(如果在获取组件期间出现意外错误)。
  • version指纹算法版本等于库版本。有关更多详细信息,请参阅版本策略指南。

请参阅扩展指南以了解如何删除和添加熵分量。

FingerprintJS.hashComponents(components: object): string

将组件字典(如上所述)转换为短哈希字符串,也称为访问者标识符。专为使用您自己的组件扩展库而设计。

FingerprintJS.componentsToDebugString(components: object): string

将组件字典(如上所述)转换为人类友好的格式。


标题:使用fingerprintjs获取设备指纹
作者:michael
地址:https://blog.junxworks.cn/articles/2023/07/07/1688697171207.html