1
0
mirror of https://github.com/ipdb/website.git synced 2024-11-12 00:04:48 +01:00
website/_src/_assets/js/_dnt.js

44 lines
2.2 KiB
JavaScript
Raw Normal View History

2017-08-09 17:12:21 +02:00
/**
* Returns true or false based on whether doNotTack is enabled. It also takes into account the
* anomalies, such as !bugzilla 887703, which effect versions of Fx 31 and lower. It also handles
* IE versions on Windows 7, 8 and 8.1, where the DNT implementation does not honor the spec.
* @see https://bugzilla.mozilla.org/show_bug.cgi?id=1217896 for more details
* @params {string} [dnt] - An optional mock doNotTrack string to ease unit testing.
* @params {string} [userAgent] - An optional mock userAgent string to ease unit testing.
* @returns {boolean} true if enabled else false
*/
function _dntEnabled(dnt, userAgent) {
// for old version of IE we need to use the msDoNotTrack property of navigator
// on newer versions, and newer platforms, this is doNotTrack but, on the window object
// Safari also exposes the property on the window object.
2017-08-29 15:09:36 +02:00
let dntStatus = dnt || navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack
const ua = userAgent || navigator.userAgent
2017-08-09 17:12:21 +02:00
// List of Windows versions known to not implement DNT according to the standard.
2017-08-29 15:09:36 +02:00
const anomalousWinVersions = ['Windows NT 6.1', 'Windows NT 6.2', 'Windows NT 6.3']
2017-08-09 17:12:21 +02:00
2017-08-29 15:09:36 +02:00
const fxMatch = ua.match(/Firefox\/(\d+)/)
const ieRegEx = /MSIE|Trident/i
const isIE = ieRegEx.test(ua)
2017-08-09 17:12:21 +02:00
// Matches from Windows up to the first occurance of ; un-greedily
// http://www.regexr.com/3c2el
2017-08-29 15:09:36 +02:00
const platform = ua.match(/Windows.+?(?=;)/g)
2017-08-09 17:12:21 +02:00
// With old versions of IE, DNT did not exist so we simply return false;
if (isIE && typeof Array.prototype.indexOf !== 'function') {
2017-08-29 15:09:36 +02:00
return false
2017-08-09 17:12:21 +02:00
} else if (fxMatch && parseInt(fxMatch[1], 10) < 32) {
// Can't say for sure if it is 1 or 0, due to Fx bug 887703
2017-08-29 15:09:36 +02:00
dntStatus = 'Unspecified'
2017-08-09 17:12:21 +02:00
} else if (isIE && platform && anomalousWinVersions.indexOf(platform.toString()) !== -1) {
// default is on, which does not honor the specification
2017-08-29 15:09:36 +02:00
dntStatus = 'Unspecified'
2017-08-09 17:12:21 +02:00
} else {
// sets dntStatus to Disabled or Enabled based on the value returned by the browser.
// If dntStatus is undefined, it will be set to Unspecified
2017-08-29 15:09:36 +02:00
dntStatus = { '0': 'Disabled', '1': 'Enabled' }[dntStatus] || 'Unspecified'
2017-08-09 17:12:21 +02:00
}
2017-08-29 15:09:36 +02:00
return dntStatus === 'Enabled'
2017-08-09 17:12:21 +02:00
}