function createFingerprint() {
// 选择哪些信息作为浏览器指纹生成的依据
const options = {
fonts: {
extendedJsFonts: true,
},
excludes: {
language: true,
colorDepth: true,
deviceMemory: true,
pixelRatio: true,
availableScreenResolution: true,
timezoneOffset: true,
timezone: true,
sessionStorage: true,
localStorage: true,
indexedDb: true,
addBehavior: true,
openDatabase: true,
cpuClass: true,
doNotTrack: true,
plugins: true,
canvas: true,
webglVendorAndRenderer: true,
adBlock: true,
hasLiedLanguages: true,
hasLiedResolution: true,
hasLiedOs: true,
hasLiedBrowser: true,
touchSupport: true,
audio: false,
enumerateDevices: true,
hardwareConcurrency: true
},
};
// 浏览器指纹
const fingerprint = Fingerprint2.get(options, (components) => { // 参数只有回调函数或者options为{}时,默认浏览器指纹依据所有配置信息进行生成
const values = components.map(component => component.value); // 配置的值的数组
const murmur = Fingerprint2.x64hash128(values.join(''), 31); // 生成浏览器指纹
console.log(murmur);
localStorage.setItem('browserId', murmur); // 存储浏览器指纹,在项目中用于校验用户身份和埋点
});
}
// 您不应在页面加载时或加载后直接运行指纹。 而是使用setTimeout或requestIdleCallback将其延迟几毫秒,以确保指纹一致。
if (window.requestIdleCallback) {
requestIdleCallback(() => {
this.createFingerprint();
});
} else {
setTimeout(() => {
this.createFingerprint();
}, 500);
}
console