Last active
November 26, 2018 02:46
-
-
Save mrf345/c495680827260d2e4241f108be338732 to your computer and use it in GitHub Desktop.
Creating anonymous UUID in JavaScript using combination of IP address and User-Agent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getUUID = ( | |
service='https://api.ipify.org/?format=json', | |
key='ip' | |
) => { | |
return new Promise( | |
(resolve, reject) => { | |
fetch(service) | |
.then((r) => r.json()) | |
.then((j) => { | |
let r = /\D+/g | |
let h = String( | |
window.navigator.userAgent.replace(r, '' | |
) * j[key].replace(r, '') | |
).replace(r, '').replace('0', '').split('') | |
let g = () => Math.floor(h.shift() * 0x10000) | |
resolve(g() + g() + '-' + g() + '-' + g() + '-' + | |
g() + '-' + g() + g() + g()) | |
}).catch((e) => reject('failed to create UUID')) | |
} | |
) | |
} | |
// Excute it | |
getUUID().then((d) => console.log(d)).catch((e) => console.warn(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment