UNPKG

@rakcode/shimon

Version:

A lightweight browser fingerprinting library for JavaScript and TypeScript, supporting major frontend frameworks like React, Angular, Vue, and Svelte.

73 lines (72 loc) 2.9 kB
// src/index.ts var takeImpression = async () => { var _a, _b, _c, _d; if (typeof window === "undefined") return { error: "Server-side rendering" }; const fingerprintData = { userAgent: navigator.userAgent, platform: ((_a = navigator.userAgentData) == null ? void 0 : _a.platform) || navigator.platform || "unknown", cpuCores: navigator.hardwareConcurrency.toString(), touchSupport: navigator.maxTouchPoints.toString(), language: navigator.language, preferredLanguages: navigator.languages.join("+"), cookiesEnabled: navigator.cookieEnabled.toString(), doNotTrack: navigator.doNotTrack || "unknown", networkType: ((_b = navigator.connection) == null ? void 0 : _b.effectiveType) || "unknown", networkSpeed: ((_d = (_c = navigator.connection) == null ? void 0 : _c.downlink) == null ? void 0 : _d.toString()) || "unknown", deviceMemory: navigator.deviceMemory ? String(navigator.deviceMemory) : "unknown", colorDepth: screen.colorDepth.toString(), pixelDepth: screen.pixelDepth.toString(), screenResolution: `${screen.width}x${screen.height}`, screenOrientation: screen.orientation.type, indexedDB: checkIndexedDBAvailability(), timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, webGL: getWebGLInfo(), canvas: getCanvasFingerprint(), audio: getAudioFingerprint() }; fingerprintData.fingerprint = await sha256(Object.values(fingerprintData).join("||")); return fingerprintData; }; var sha256 = async (data) => { const encoder = new TextEncoder(); const dataBuffer = encoder.encode(data); const hashBuffer = await crypto.subtle.digest("SHA-256", dataBuffer); return Array.from(new Uint8Array(hashBuffer)).map((b) => b.toString(16).padStart(2, "0")).join(""); }; var checkIndexedDBAvailability = () => !!window.indexedDB ? "Supported" : "Not Supported"; var getWebGLInfo = () => { try { const canvas = document.createElement("canvas"); const gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl"); if (!gl) return "Not supported"; const debugInfo = gl.getExtension("WEBGL_debug_renderer_info"); return debugInfo ? gl.getParameter(debugInfo.UNMASKED_RENDERER_WEBGL) : "Supported"; } catch { return "Error"; } }; var getCanvasFingerprint = () => { try { const canvas = document.createElement("canvas"); const ctx = canvas.getContext("2d"); if (!ctx) return "Not supported"; ctx.textBaseline = "top"; ctx.font = "14px Arial"; ctx.fillText("Fingerprint", 2, 2); return canvas.toDataURL(); } catch { return "Error"; } }; var getAudioFingerprint = () => { try { const audioCtx = new (window.AudioContext || window.webkitAudioContext)(); const analyser = audioCtx.createAnalyser(); return analyser.frequencyBinCount.toString(); } catch { return "Error"; } }; export { takeImpression };