Last active
August 29, 2015 14:23
-
-
Save gabalafou/06fbb0d5fc8976555e13 to your computer and use it in GitHub Desktop.
Disqus helper functions for client-side generation of session id (see https://gist.github.com/gabalafou/66f04297c0f03dee7d20)
This file contains 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
function random() { | |
try { | |
// get cryptographically strong entropy when available (CryptoAPI) | |
var ab = new Uint32Array(1); | |
return window.crypto.getRandomValues(ab)[0]; | |
} catch (e) { // could throw QuotaExceededError if too much entropy is drained | |
// fallback to old way | |
return Math.floor(Math.random() * 1e9); | |
} | |
} |
This file contains 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
function reduceMiscSessionMetrics() { | |
// wrap in try/catch just to make sure it won't break anything in some exotic browser | |
try { | |
// The time-zone offset is the difference, in minutes, between UTC and local time | |
var offset = new Date().getTimezoneOffset(); | |
var screenRes = 1, | |
screen = window.screen; | |
if (screen && screen.availWidth) { | |
screenRes = screen.availWidth * screen.availHeight + screen.colorDepth; | |
} else if (screen && screen.width) { | |
// screen.availWidth is not supported on mobile devices | |
screenRes = screen.width * screen.height; | |
} | |
var doc = document.documentElement, | |
browser = doc.clientWidth * doc.clientHeight; | |
// multiply values by random prime numbers just to decrease likelihood of | |
// making the same sum by different summands. Substract `browser` in order | |
// to decrease output value | |
return Math.abs(offset * 17 + screenRes * 25 - browser); | |
} catch (e) { | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Helper functions for https://gist.github.com/gabalafou/66f04297c0f03dee7d20