-
-
Notifications
You must be signed in to change notification settings - Fork 57
/
cookies-eu-banner.js
183 lines (155 loc) · 5.53 KB
/
cookies-eu-banner.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
; // jshint ignore:line
(function (root, factory, undefined) {
'use strict';
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
module.exports = factory();
} else {
// root is window
root.CookiesEuBanner = factory();
}
}(window, function () {
'use strict';
var CookiesEuBanner,
document = window.document;
CookiesEuBanner = function (launchFunction, waitAccept, useLocalStorage, undefined) {
if (!(this instanceof CookiesEuBanner)) {
return new CookiesEuBanner(launchFunction);
}
this.cookieTimeout = 31104000000; // 12 months in milliseconds
this.bots = /bot|crawler|spider|crawling/i;
this.cookieName = 'hasConsent';
this.trackingCookiesNames = ['__utma', '__utmb', '__utmc', '__utmt', '__utmv', '__utmz', '_ga', '_gat', '_gid'];
this.launchFunction = launchFunction;
this.waitAccept = waitAccept || false;
this.useLocalStorage = useLocalStorage || false;
this.init();
};
CookiesEuBanner.prototype = {
init: function () {
// Detect if the visitor is a bot or not
// Prevent for search engine take the cookie alert message as main content of the page
var isBot = this.bots.test(navigator.userAgent);
// Check if DoNotTrack is activated
var dnt = navigator.doNotTrack || navigator.msDoNotTrack || window.doNotTrack;
var isToTrack = (dnt !== null && dnt !== undefined) ? (dnt && dnt !== 'yes' && dnt !== 1 && dnt !== '1') : true;
// Do nothing if it is a bot
// If DoNotTrack is activated, do nothing too
if (isBot || !isToTrack || this.hasConsent() === false) {
this.removeBanner(0);
return false;
}
// User has already consent to use cookies to tracking
if (this.hasConsent() === true) {
// Launch user custom function
this.launchFunction();
return true;
}
// If it's not a bot, no DoNotTrack and not already accept, so show banner
this.showBanner();
if (!this.waitAccept) {
// Accept cookies by default for the next page
this.setConsent(true);
}
},
/*
* Show banner at the top of the page
*/
showBanner: function () {
var _this = this,
getElementById = document.getElementById.bind(document),
banner = getElementById('cookies-eu-banner'),
rejectButton = getElementById('cookies-eu-reject'),
acceptButton = getElementById('cookies-eu-accept'),
moreLink = getElementById('cookies-eu-more'),
waitRemove = (banner.dataset.waitRemove === undefined) ? 0 : parseInt(banner.dataset.waitRemove),
// Variables for minification optimization
addClickListener = this.addClickListener,
removeBanner = _this.removeBanner.bind(_this, waitRemove);
banner.style.display = 'block';
if (moreLink) {
addClickListener(moreLink, function () {
_this.deleteCookie(_this.cookieName);
});
}
if (acceptButton) {
addClickListener(acceptButton, function () {
removeBanner();
_this.setConsent(true);
_this.launchFunction();
});
}
if (rejectButton) {
addClickListener(rejectButton, function () {
removeBanner();
_this.setConsent(false);
// Delete existing tracking cookies
_this.trackingCookiesNames.map(_this.deleteCookie);
});
}
},
/*
* Set consent cookie or localStorage
*/
setConsent: function (consent) {
if (this.useLocalStorage) {
return localStorage.setItem(this.cookieName, consent);
}
this.setCookie(this.cookieName, consent);
},
/*
* Check if user already consent
*/
hasConsent: function () {
var cookieName = this.cookieName;
var isCookieSetTo = function (value) {
return document.cookie.indexOf(cookieName + '=' + value) > -1 || localStorage.getItem(cookieName) === value;
};
if (isCookieSetTo('true')) {
return true;
} else if (isCookieSetTo('false')) {
return false;
}
return null;
},
/*
* Create/update cookie
*/
setCookie: function (name, value) {
var date = new Date();
date.setTime(date.getTime() + this.cookieTimeout);
document.cookie = name + '=' + value + ';expires=' + date.toGMTString() + ';path=/' + ';secure;SameSite=Lax';
},
/*
* Delete cookie by changing expire
*/
deleteCookie: function (name) {
var hostname = document.location.hostname.replace(/^www\./, ''),
commonSuffix = '; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';
document.cookie = name + '=; domain=.' + hostname + commonSuffix;
document.cookie = name + '=' + commonSuffix;
},
addClickListener: function (DOMElement, callback) {
if (DOMElement.attachEvent) { // For IE 8 and earlier versions
return DOMElement.attachEvent('onclick', callback);
}
// For all major browsers, except IE 8 and earlier
DOMElement.addEventListener('click', callback);
},
/*
* Delays removal of banner allowing developers
* to specify their own transition effects
*/
removeBanner: function (wait) {
var banner = document.getElementById('cookies-eu-banner');
banner.classList.add('cookies-eu-banner--before-remove');
setTimeout (function() {
if (banner && banner.parentNode) {
banner.parentNode.removeChild(banner);
}
}, wait);
}
};
return CookiesEuBanner;
}));