|
| 1 | +/** |
| 2 | + * http://schalkneethling.github.io/blog/2015/11/06/respect-user-choice-do-not-track/ |
| 3 | + * https://github.com/schalkneethling/dnt-helper/blob/master/js/dnt-helper.js |
| 4 | + * |
| 5 | + * Returns true or false based on whether doNotTack is enabled. It also takes into account the |
| 6 | + * anomalies, such as !bugzilla 887703, which effect versions of Fx 31 and lower. It also handles |
| 7 | + * IE versions on Windows 7, 8 and 8.1, where the DNT implementation does not honor the spec. |
| 8 | + * @see https://bugzilla.mozilla.org/show_bug.cgi?id=1217896 for more details |
| 9 | + * @params {string} [dnt] - An optional mock doNotTrack string to ease unit testing. |
| 10 | + * @params {string} [userAgent] - An optional mock userAgent string to ease unit testing. |
| 11 | + * @returns {boolean} true if enabled else false |
| 12 | + */ |
| 13 | +function _dntEnabled(dnt, userAgent) { |
| 14 | + |
| 15 | + 'use strict'; |
| 16 | + |
| 17 | + // for old version of IE we need to use the msDoNotTrack property of navigator |
| 18 | + // on newer versions, and newer platforms, this is doNotTrack but, on the window object |
| 19 | + // Safari also exposes the property on the window object. |
| 20 | + var dntStatus = dnt || navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack; |
| 21 | + var ua = userAgent || navigator.userAgent; |
| 22 | + |
| 23 | + // List of Windows versions known to not implement DNT according to the standard. |
| 24 | + var anomalousWinVersions = ['Windows NT 6.1', 'Windows NT 6.2', 'Windows NT 6.3']; |
| 25 | + |
| 26 | + var fxMatch = ua.match(/Firefox\/(\d+)/); |
| 27 | + var ieRegEx = /MSIE|Trident/i; |
| 28 | + var isIE = ieRegEx.test(ua); |
| 29 | + // Matches from Windows up to the first occurance of ; un-greedily |
| 30 | + // http://www.regexr.com/3c2el |
| 31 | + var platform = ua.match(/Windows.+?(?=;)/g); |
| 32 | + |
| 33 | + // With old versions of IE, DNT did not exist so we simply return false; |
| 34 | + if (isIE && typeof Array.prototype.indexOf !== 'function') { |
| 35 | + return false; |
| 36 | + } else if (fxMatch && parseInt(fxMatch[1], 10) < 32) { |
| 37 | + // Can't say for sure if it is 1 or 0, due to Fx bug 887703 |
| 38 | + dntStatus = 'Unspecified'; |
| 39 | + } else if (isIE && platform && anomalousWinVersions.indexOf(platform.toString()) !== -1) { |
| 40 | + // default is on, which does not honor the specification |
| 41 | + dntStatus = 'Unspecified'; |
| 42 | + } else { |
| 43 | + // sets dntStatus to Disabled or Enabled based on the value returned by the browser. |
| 44 | + // If dntStatus is undefined, it will be set to Unspecified |
| 45 | + dntStatus = { '0': 'Disabled', '1': 'Enabled' }[dntStatus] || 'Unspecified'; |
| 46 | + } |
| 47 | + |
| 48 | + return dntStatus === 'Enabled' ? true : false; |
| 49 | +} |
0 commit comments