UNPKG

@mozmeao/dnt-helper

Version:

A helper that returns true or false based on whether doNotTrack is enabled on the browser.

108 lines (97 loc) 4.35 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(); else if(typeof define === 'function' && define.amd) define([], factory); else if(typeof exports === 'object') exports["dntHelper"] = factory(); else root["dntHelper"] = factory(); })(self, function() { return /******/ (function() { // webpackBootstrap /******/ var __webpack_modules__ = ({ /***/ 994: /***/ (function(module) { /* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ /** * Returns true or false based on whether doNotTrack 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} [ua] - An optional mock userAgent string to ease unit testing. * @returns {boolean} true if enabled else false */ var dntHelper = function dntHelper(dnt, ua) { 'use strict'; // 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. var dntStatus = dnt || navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack; var userAgent = ua || navigator.userAgent; // List of Windows versions known to not implement DNT according to the standard. var anomalousWinVersions = ['Windows NT 6.1', 'Windows NT 6.2', 'Windows NT 6.3']; var fxMatch = userAgent.match(/Firefox\/(\d+)/); var ieRegEx = /MSIE|Trident/i; var isIE = ieRegEx.test(userAgent); // Matches from Windows up to the first occurance of ; un-greedily // http://www.regexr.com/3c2el var platform = userAgent.match(/Windows.+?(?=;)/g); // With old versions of IE, DNT did not exist so we simply return false; if (isIE && typeof Array.prototype.indexOf !== 'function') { return false; } else if (fxMatch && parseInt(fxMatch[1], 10) < 32) { // Can't say for sure if it is 1 or 0, due to Fx bug 887703 dntStatus = 'Unspecified'; } else if (isIE && platform && anomalousWinVersions.indexOf(platform.toString()) !== -1) { // default is on, which does not honor the specification dntStatus = 'Unspecified'; } 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 dntStatus = { 0: 'Disabled', 1: 'Enabled' }[dntStatus] || 'Unspecified'; } return dntStatus === 'Enabled' ? true : false; }; module.exports = dntHelper; /***/ }) /******/ }); /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ var cachedModule = __webpack_module_cache__[moduleId]; /******/ if (cachedModule !== undefined) { /******/ return cachedModule.exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = __webpack_module_cache__[moduleId] = { /******/ // no module.id needed /******/ // no module.loaded needed /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /************************************************************************/ /******/ /******/ // startup /******/ // Load entry module and return exports /******/ // This entry module is referenced by other modules so it can't be inlined /******/ var __webpack_exports__ = __webpack_require__(994); /******/ /******/ return __webpack_exports__; /******/ })() ; });