From 3c609793fdfb71790bbd91ead54f5a34db608bd2 Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 9 Aug 2023 09:28:05 -0400 Subject: [PATCH] Improve `fingerprint2.js` scriptlet Related issue: https://github.com/uBlockOrigin/uBlock-issues/issues/2741 --- src/web_accessible_resources/fingerprint2.js | 32 +++++++++++++++----- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/web_accessible_resources/fingerprint2.js b/src/web_accessible_resources/fingerprint2.js index 91c589722..24a39b909 100644 --- a/src/web_accessible_resources/fingerprint2.js +++ b/src/web_accessible_resources/fingerprint2.js @@ -19,19 +19,37 @@ Home: https://github.com/gorhill/uBlock */ +// Reference: +// https://github.com/fingerprintjs/fingerprintjs/tree/v2 + (function() { 'use strict'; - let browserId = ''; - for ( let i = 0; i < 8; i++ ) { - browserId += (Math.random() * 0x10000 + 0x1000 | 0).toString(16).slice(-4); - } + const hex32 = len => { + return Math.floor(Math.random() * Number.MAX_SAFE_INTEGER) + .toString(16) + .slice(-len) + .padStart(len, '0'); + }; + const browserId = `${hex32(8)}${hex32(8)}${hex32(8)}${hex32(8)}`; const fp2 = function(){}; fp2.get = function(opts, cb) { if ( !cb ) { cb = opts; } - setTimeout(( ) => { cb(browserId, []); }, 1); + setTimeout(( ) => { cb([]); }, 1); + }; + fp2.getPromise = function() { + return Promise.resolve([]); + }; + fp2.getV18 = function() { + return browserId; + }; + fp2.x64hash128 = function() { + return browserId; }; fp2.prototype = { - get: fp2.get + get: function(opts, cb) { + if ( !cb ) { cb = opts; } + setTimeout(( ) => { cb(browserId, []); }, 1); + }, }; - window.Fingerprint2 = fp2; + self.Fingerprint2 = fp2; })();