首页 > 解决方案 > 与服务人员进行指纹识别

问题描述

所以,我目前正在学习服务工作者,我想测试的方面之一是如何使用服务工作者进行指纹识别。

目前,我正在使用https://github.com/LukasDrgon/fingerprintjs2来实现指纹功能并从StackOverflow 上的此链接获取帮助。

但是当我尝试重新加载页面时,它给出了一个错误说

Uncaught (in promise) TypeError: ServiceWorker script at http://localhost:9000/sw.js for scope http://localhost:9000/ threw an exception during script evaluation.

这就是我的代码的样子。

// import fp from "fingerprintjs2";

function receivePushNotification(event) {
  console.log("[Service Worker] Push Received.");

  const { image, tag, url, title, text } = event.data.json();

  const options = {
    data: url,
    body: text,
    icon: image,
    vibrate: [200, 100, 200],
    tag: tag,
    image: image,
    badge: "https://spyna.it/icons/favicon.ico",
    actions: [{ action: "Detail", title: "View", icon: "https://via.placeholder.com/128/ff0000" }]
  };


  // new Fingerprint2().get(function(result, components){
  //   console.log(result); //a hash, representing your device fingerprint
  //   console.log(components); // an array of FP components
  // });


  event.waitUntil(self.registration.showNotification(title, options));
}

function openPushNotification(event) {
  console.log("[Service Worker] Notification click Received.", event.notification.data);
  
  event.notification.close();
  event.waitUntil(clients.openWindow(event.notification.data));
}


self.addEventListener("push", receivePushNotification);
self.addEventListener("notificationclick", openPushNotification);
console.log("here in service Worker")

//fingerprint code below this line

var info = {};

new Fingerprint2().get(function(result, components) {
    info.fingerprint = result;

    afterFingerprintIsCalculated();
});

function afterFingerprintIsCalculated() {
    alert(info.fingerprint);
}



// BETTER (no global state)
new Fingerprint2().get(function(result, components) {
    var info = {
        fingerprint: result
    };

    processFingerprint(info);
});

function processFingerprint(data) {
    alert(data.fingerprint);
}

让我知道如何使它工作并使用服务人员获取浏览器的指纹。我目前对 JS 和这种范式很陌生,如果您有其他有用的(且易于理解的链接),也请分享。谢谢你。

标签: javascriptservice-workerfingerprint

解决方案


推荐阅读