shell
npm i fingerprintjs2
npm i fingerprintjs2
1
2
2
layout
vue
<script lang="ts">
import Fingerprint2 from 'fingerprintjs2'; // 引入fingerprintjs2
export default {
name: 'App',
components: {
},
data() {
return {
};
},
async created() {
// 您不应在页面加载时或加载后直接运行指纹。 而是使用setTimeout或requestIdleCallback将其延迟几毫秒,以确保指纹一致。
if (window.requestIdleCallback) {
requestIdleCallback(() => {
this.createFingerprint();
});
} else {
setTimeout(() => {
this.createFingerprint();
}, 500);
}
},
methods: {
setCookie (
name: string,
value: number | string,
duration: number
) {
const d = new Date();
d.setTime(d.getTime() + duration);
const expires = `expires=${d.toUTCString()}`;
document.cookie = `${name}=${value};${expires};path=/`;
},
// 创建浏览器指纹
createFingerprint() {
// 选择哪些信息作为浏览器指纹生成的依据
const options = {
fonts: {
extendedJsFonts: true,
},
excludes: {
audio: true,
userAgent: true,
enumerateDevices: true,
touchSupport: true,
},
};
// 浏览器指纹
// 参数只有回调函数或者options为{}时,默认浏览器指纹依据所有配置信息进行生成
const fingerprint = Fingerprint2.get(options, (components) => {
// 配置的值的数组
const values = components.map(component => component.value);
// 生成浏览器指纹
const murmur = Fingerprint2.x64hash128(values.join(''), 31);
console.log('components: ' + components);
console.log('values: ' + 'values');
console.log('BROWSER_ID: ' + murmur);
localStorage.setItem('BROWSER_ID', murmur); // 存储浏览器指纹,在项目中用于校验用户身份和埋点
// 一个月
this.setCookie('BROWSER_ID', murmur, 30 * 24 * 60 * 60 * 1000);
});
},
},
};
</script>
<script lang="ts">
import Fingerprint2 from 'fingerprintjs2'; // 引入fingerprintjs2
export default {
name: 'App',
components: {
},
data() {
return {
};
},
async created() {
// 您不应在页面加载时或加载后直接运行指纹。 而是使用setTimeout或requestIdleCallback将其延迟几毫秒,以确保指纹一致。
if (window.requestIdleCallback) {
requestIdleCallback(() => {
this.createFingerprint();
});
} else {
setTimeout(() => {
this.createFingerprint();
}, 500);
}
},
methods: {
setCookie (
name: string,
value: number | string,
duration: number
) {
const d = new Date();
d.setTime(d.getTime() + duration);
const expires = `expires=${d.toUTCString()}`;
document.cookie = `${name}=${value};${expires};path=/`;
},
// 创建浏览器指纹
createFingerprint() {
// 选择哪些信息作为浏览器指纹生成的依据
const options = {
fonts: {
extendedJsFonts: true,
},
excludes: {
audio: true,
userAgent: true,
enumerateDevices: true,
touchSupport: true,
},
};
// 浏览器指纹
// 参数只有回调函数或者options为{}时,默认浏览器指纹依据所有配置信息进行生成
const fingerprint = Fingerprint2.get(options, (components) => {
// 配置的值的数组
const values = components.map(component => component.value);
// 生成浏览器指纹
const murmur = Fingerprint2.x64hash128(values.join(''), 31);
console.log('components: ' + components);
console.log('values: ' + 'values');
console.log('BROWSER_ID: ' + murmur);
localStorage.setItem('BROWSER_ID', murmur); // 存储浏览器指纹,在项目中用于校验用户身份和埋点
// 一个月
this.setCookie('BROWSER_ID', murmur, 30 * 24 * 60 * 60 * 1000);
});
},
},
};
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70