-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
cookie.js
299 lines (293 loc) · 12.1 KB
/
cookie.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*!
* Cookie Consent Adapter
* Copyright 2018 Dirk Persky (https://github.com/DirkPersky/typo3-dp_cookieconsent)
* Licensed under GPL v3+ (https://github.com/DirkPersky/typo3-dp_cookieconsent/blob/master/LICENSE)
*/
window.addEventListener("load", function () {
function CookieConsent() {
this.cookie_name = 'dp_cookieconsent_status';
this.cookie = {
// This is the url path that the cookie 'name' belongs to. The cookie can only be read at this location
path: '/',
// This is the domain that the cookie 'name' belongs to. The cookie can only be read on this domain.
// - Guide to cookie domains - https://www.mxsasha.eu/blog/2014/03/04/definitive-guide-to-cookie-domains/
domain: '',
// The cookies expire date, specified in days (specify -1 for no expiry)
expiryDays: 365,
// If true the cookie will be created with the secure flag. Secure cookies will only be transmitted via HTTPS.
secure: false
};
}
/** Async Load Ressources **/
CookieConsent.prototype.asyncLoad = function (u, t, c) {
var d = document,
o = d.createElement(t),
s = d.getElementsByTagName(t)[0];
switch (t) {
case 'script':
o.src = u;
o.setAttribute('defer', '');
break;
case 'link':
o.rel = 'stylesheet';
o.type = 'text/css';
o.setAttribute('defer', '');
o.href = u;
break;
}
if (c) {
o.addEventListener('load', function (e) {
c(null, e);
}, false);
}
s.parentNode.insertBefore(o, s);
};
/** Async Load Helper for JS **/
CookieConsent.prototype.asyncJS = function (u, c) {
this.asyncLoad(u, 'script', c);
};
/** Async Load Helper for CSS **/
CookieConsent.prototype.asyncCSS = function (u) {
this.asyncLoad(u, 'link');
};
/** fallback: getElementsByTagName **/
CookieConsent.prototype.getCookieElementsByTag = function(tag){
// element holder
var elements = [];
// check browser function
if (typeof document.querySelectorAll == 'undefined') {
elements = document.querySelectorAll(tag+'[data-cookieconsent]');
} else {
// fallback
var temp = document.getElementsByTagName(tag);
for (var key in temp) {
var element = temp[key];
if (typeof element.getAttribute != 'undefined' && element.getAttribute('data-cookieconsent')) {
elements.push(element);
}
}
}
// return elements
return elements;
};
/**
* Load Iframes
* @param element
*/
CookieConsent.prototype.callIframeHandler = function(element) {
/**
* Create Element Copy
* @type {ActiveX.IXMLDOMNode | Node}
*/
var iframe = element.cloneNode(true);
// replace src with data-src
if (iframe.getAttribute('data-src')) {
iframe.src = iframe.getAttribute('data-src');
}
// add Element to DOM
element.parentNode.replaceChild(iframe, element);
// add Loaded class
iframe.classList.add("dp--loaded");
};
/**
* Load Script codes
* @param element
*/
CookieConsent.prototype.callScriptHandler = function(element) {
/** get HTML of Elements **/
var code = element.innerHTML;
/** trim Elements **/
if (code && code.length) code = code.trim();
/** run Code it something in in it **/
if (code && code.length) {
/** if Is Code Eval Code **/
eval.call(this, code);
} else {
/**
* If is SRC load that
* Dont use this src="", becouse some Browser will ignore the type=text/plain
* prefer use data-src=""
*/
if (element.getAttribute('data-src')) {
this.asyncJS(element.getAttribute('data-src'));
} else if (element.src) {
this.asyncJS(element.src);
}
}
};
/** Callback after cookies are allowed **/
CookieConsent.prototype.loadCookies = function () {
/** Get all Scripts to load **/
var elements = this.getCookieElementsByTag('script');
// load Iframes
elements = elements.concat(this.getCookieElementsByTag('iframe'));
// elements exist?
if (elements.length > 0) {
var key;
/** Loop through elements and run Code **/
for(key = 0; key < elements.length; key++){
/** Chekbox Access check **/
if (window.cookieconsent_options.layout === 'dpextend') {
var group = elements[key].dataset.cookieconsent;
if(group != 'rquired') {
// load cookies
this.loadCookiesPreset();
// check if value exist
if( !this.dpCookies.hasOwnProperty('dp--cookie-'+group) || this.dpCookies['dp--cookie-'+group] !== true) {
// abort script
continue;
}
}
}
/**
* check tag name
*/
if(typeof elements[key].tagName != 'undefined') {
/**
* Call Handler based on type
*/
switch (elements[key].tagName.toUpperCase()) {
case 'IFRAME':
this.callIframeHandler(elements[key]);
break;
default:
this.callScriptHandler(elements[key]);
}
}
}
}
};
/** load Scripts **/
CookieConsent.prototype.load = function () {
/** Start Loading Scripts & CSS **/
this.asyncCSS(window.cookieconsent_options.css);
/** Lood own CSS extends **/
if (window.cookieconsent_options.layout == 'dpextend') this.asyncCSS(window.cookieconsent_options.dpCSS);
/** Load Javascript **/
this.asyncJS(window.cookieconsent_options.js, this.init);
};
/** Toogle Body Class **/
CookieConsent.prototype.setClass = function (remove) {
if (remove === true) {
document.querySelector('body').classList.remove('dp--cookie-consent');
} else {
document.querySelector('body').classList.add('dp--cookie-consent');
}
};
/** Checkbox Handling **/
CookieConsent.prototype.setCheckboxes = function () {
if (window.cookieconsent_options.layout != 'dpextend') return;
// load checkboxes
var statistik = this.loadCheckbox('dp--cookie-statistics'),
marketing = this.loadCheckbox('dp--cookie-marketing');
// save Cookie Values
this.saveCookie([statistik, marketing]);
};
CookieConsent.prototype.loadCheckboxes = function () {
if (window.cookieconsent_options.layout != 'dpextend') return;
// load cookies
this.loadCookiesPreset();
// load Checkboxes and set default values
var statistik = this.loadCheckbox('dp--cookie-statistics', true),
marketing = this.loadCheckbox('dp--cookie-marketing', true);
};
/** Save checkbox values to Cookie**/
CookieConsent.prototype.saveCookie = function (values) {
var object = {};
// build Store object
values.map(function (e) {
object[e.id] = e.checked;
});
// save script selection
window.cookieconsent.utils.setCookie(
this.cookie_name,
JSON.stringify(object),
this.cookie.expiryDays,
this.cookie.domain,
this.cookie.path,
this.cookie.secure
);
};
/** Load Cookies **/
CookieConsent.prototype.loadCookiesPreset = function(){
if( this.dpCookies != false) this.dpCookies = window.cookieconsent.utils.getCookie(this.cookie_name);
if (typeof this.dpCookies != 'undefined') {
try {
this.dpCookies = JSON.parse(this.dpCookies);
} catch(error) {
this.dpCookies = false;
}
} else {
this.dpCookies = false;
}
};
/** Load Checkboxes by name and fill Cookie value**/
CookieConsent.prototype.loadCheckbox = function (id, cookieLoad) {
var checkbox = document.getElementById(id);
// load Cookie Value
if (cookieLoad === true) {
// get checkbox Value
if(this.dpCookies && this.dpCookies.hasOwnProperty(id)) {
checkbox.checked = this.dpCookies[id];
}
}
// return element
return checkbox;
};
/** Init Cookie Plugin **/
CookieConsent.prototype.init = function () {
/** Bind Self to Handler Class Funktions **/
window.cookieconsent.initialise({
content: window.cookieconsent_options.content,
theme: window.cookieconsent_options.theme,
position: window.cookieconsent_options.position,
palette: window.cookieconsent_options.palette,
dismissOnScroll: window.cookieconsent_options.dismissOnScroll,
type: window.cookieconsent_options.type,
layout: window.cookieconsent_options.layout,
revokable: window.cookieconsent_options.revokable,
cookie: (new CookieConsent()).cookie,
layouts: {
dpextend: "{{dpmessagelink}}{{compliance}}",
},
elements: {
dpmessagelink: '<span id="cookieconsent:desc" class="cc-message">' +
'{{message}} ' +
'<a aria-label="learn more about cookies" role=button tabindex="0" class="cc-link" href="{{href}}" rel="noopener noreferrer nofollow" target="{{target}}">{{link}}</a>' +
'<div class="dp--cookie-check">' +
'<label for="dp--cookie-require"><input type="checkbox" id="dp--cookie-require" class="dp--check-box" disabled="disabled" checked="checked"> {{dpRequire}}</label>' +
'<label for="dp--cookie-statistics"><input type="checkbox" id="dp--cookie-statistics" class="dp--check-box" '+(window.cookieconsent_options.checkboxes.statistics?'checked="checked"':'')+'> {{dpStatistik}}</label>' +
'<label for="dp--cookie-marketing"><input type="checkbox" id="dp--cookie-marketing" class="dp--check-box" '+(window.cookieconsent_options.checkboxes.marketing?'checked="checked"':'')+'> {{dpMarketing}}</label>' +
'</div>' +
'</span>',
},
onPopupOpen: function () {
// set Body Class
(new CookieConsent()).setClass();
// load Checkboxes
(new CookieConsent()).loadCheckboxes();
},
onPopupClose: function () {
// remove Body Class
(new CookieConsent()).setClass(true);
},
onInitialise: function (status) {
if (this.hasConsented() && (status == 'dismiss' || status == 'allow')) (new CookieConsent()).loadCookies();
},
onStatusChange: function (status) {
// save checkboxes?
(new CookieConsent()).setCheckboxes();
// load cookies
if (this.hasConsented() && (status == 'dismiss' || status == 'allow')) (new CookieConsent()).loadCookies();
// Remove the Node from HTML
//if (window.cookieconsent_options.type == 'info' && !window.cookieconsent_options.revokable) this.element.parentNode.removeChild(this.element);
},
onRevokeChoice: function () {
}
});
};
/** Init Handler **/
var cookieHandler = new CookieConsent();
/** Start Script Handling **/
cookieHandler.load();
});