remove _dntEnabled from global scope

update dnt-helper.js from source, move into libs folder
This commit is contained in:
Leo McArdle 2021-07-29 15:33:01 +01:00 коммит произвёл Leo McArdle
Родитель 6b7aed4f1d
Коммит 6d953ef8ea
6 изменённых файлов: 77 добавлений и 54 удалений

Просмотреть файл

@ -1,47 +0,0 @@
// Direct copy from https://github.com/mozilla/bedrock/blob/master/media/js/base/dnt-helper.js
// Update from bedrock if needed.
/**
* 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} [ua] - An optional mock userAgent string to ease unit testing.
* @returns {boolean} true if enabled else false
*/
function _dntEnabled(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 ua = _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 = ua.match(/Firefox\/(\d+)/);
var ieRegEx = /MSIE|Trident/i;
var isIE = ieRegEx.test(ua);
// Matches from Windows up to the first occurance of ; un-greedily
// http://www.regexr.com/3c2el
var platform = ua.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;
}

Просмотреть файл

@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import dntEnabled from "./libs/dnt-helper";
(function(w) {
'use strict';
@ -17,7 +19,7 @@
}
// If doNotTrack is not enabled, it is ok to add GTM
// @see https://bugzilla.mozilla.org/show_bug.cgi?id=1217896 for more details
if (typeof _dntEnabled === 'function' && !_dntEnabled() && GTM_CONTAINER_ID) {
if (typeof dntEnabled === 'function' && !dntEnabled() && GTM_CONTAINER_ID) {
(function(w,d,s,l,i,j,f,dl,k,q){
w[l]=w[l]||[];w[l].push({'gtm.start': new Date().getTime(),event:'gtm.js'});f=d.getElementsByTagName(s)[0];
k=i.length;q='//www.googletagmanager.com/gtag/js?id=@&l='+(l||'dataLayer');

Просмотреть файл

@ -0,0 +1,56 @@
// Direct copy from https://github.com/mozilla/bedrock/blob/master/media/js/base/dnt-helper.js
// Update from bedrock if needed.
/* 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 http://mozilla.org/MPL/2.0/. */
// create namespace
if (typeof window.Mozilla === 'undefined') {
window.Mozilla = {};
}
/**
* 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
*/
Mozilla.dntEnabled = function(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;
};

Просмотреть файл

@ -146,7 +146,6 @@ const entrypoints = {
"kpi/js/kpi.browserify.js",
],
"gtm-snippet": [
"sumo/js/dnt-helper.js",
"sumo/js/gtm-snippet.js",
],
}

Просмотреть файл

@ -32,7 +32,6 @@ module.exports = {
"tabsInit": "readonly",
"Mozilla": "readonly",
"trackEvent": "readonly",
"_dntEnabled": "readonly",
"dialogSet": "readonly",
"$": "readonly",
"jQuery": "readonly",

Просмотреть файл

@ -18,11 +18,13 @@ module.exports = [
"Mozilla.UITour"
),
exportAndExpose("../kitsune/sumo/static/sumo/js/analytics.js", "trackEvent"),
exportAndExpose(
"../kitsune/sumo/static/sumo/js/dnt-helper.js",
"_dntEnabled"
),
exportAndExpose("../kitsune/sumo/static/sumo/js/upload.js", "dialogSet"),
// we copy these libraries from external sources, so define their exports here,
// rather than having to modify them, making updating them more difficult:
exports(
"../kitsune/sumo/static/sumo/js/libs/dnt-helper.js",
"default Mozilla.dntEnabled"
),
// this library attempts to expose a bunch of stuff globally by adding them to `this`, imports-loader makes that work:
{
test: require.resolve(
@ -35,6 +37,18 @@ module.exports = [
},
];
function exports(path, exports) {
// export the named variable
return {
test: require.resolve(path),
loader: "exports-loader",
options: {
type: "module",
exports,
},
};
}
function expose(path, exposes) {
// expose a module's export globally
return {