Skip to content

Instantly share code, notes, and snippets.

@gabalafou
Last active August 29, 2015 14:23
Show Gist options
  • Save gabalafou/06fbb0d5fc8976555e13 to your computer and use it in GitHub Desktop.
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)
function getNavigationTime() {
var timing = window.performance && window.performance.timing;
// this method is expected to return a big number, so do this in any case
if (!timing)
return 1e5;
var timing = window.performance.timing;
var dns = timing.domainLookupEnd - timing.domainLookupStart;
var connection = timing.connectEnd - timing.connectStart;
var navigation = timing.responseStart - timing.navigationStart;
// multiply values by random prime numbers just to decrease likelihood of
// making the same sum by different summands
return dns * 11 + connection * 13 + navigation * 17;
}
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);
}
}
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;
}
}
@gabalafou
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment