This repository has been archived by the owner on Dec 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookieBlock.js
350 lines (268 loc) · 9.28 KB
/
CookieBlock.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
'use strict';
require("babel-polyfill");
import GoogleAnalytics from "./modules/analytics/GoogleAnalytics";
import Piwik from "./modules/analytics/Piwik";
import Facebook from "./modules/social/Facebook";
import Twitter from "./modules/social/Twitter";
import Instagram from "./modules/social/Instagram";
import Pinterest from "./modules/social/Pinterest";
/**
* CookieBlock
*
* @author Felix Rupp <[email protected]>
* @copyright Felix Rupp
* @license MIT
*
* @since 1.0.0
*/
export default class CookieBlock {
/**
* Constructor
*/
constructor() {
this.appOptions = new Map([
["respectDoNotTrack", false]
]);
this.pluginOptions = new Map([
["googleAnalytics", {active: false, options: {propertyId: ''}}],
["piwik", {active: false, options: {url: ''}}],
["facebook", {active: false, options: {}}],
["twitter", {active: false, options: {}}],
["instagram", {active: false, options: {}}],
["pinterest", {active: false, options: {}}]
]);
this.pluginObjects = new Map([
["googleAnalytics", {object: new GoogleAnalytics()}],
["piwik", {object: new Piwik()}],
["facebook", {object: new Facebook()}],
["twitter", {object: new Twitter()}],
["instagram", {object: new Instagram()}],
["pinterest", {object: new Pinterest()}]
]);
}
/**
* Init
*/
init() {
console.log("Initial Plugin Options:");
console.log(this.pluginOptions);
let cookieValue = this.getCookieBlockCookie();
console.log("Initial Cookie Values:");
console.log(this.getCookieBlockCookie());
// Init the cookie values with default options
if (cookieValue === null) {
console.log("Manage DNT before Cookie:");
let dntActive = this.manageDoNotTrack();
console.log(dntActive);
console.log("Set Default Plugin Options to Cookie:");
console.log(this.pluginOptions);
this.setCookieBlockCookie(this.pluginOptions);
}
else {
this.pluginOptions = cookieValue;
console.log("Plugin Values before DNT Manage:");
console.log(this.pluginOptions);
console.log("Manage DNT after Cookie:");
/*let dntActive = */this.manageDoNotTrack();
//console.log(dntActive);
console.log("Plugin Values after DNT Manage:");
console.log(this.pluginOptions);
// Overwrite the pluginOptions with disabled values:
/*if (dntActive) {
// Block all active modules initially
this.pluginOptions.forEach((options, moduleId) => {
this.manageCookie(moduleId)
});
this.setCookieBlockCookie(this.pluginOptions);
}
*/
this.setCookieBlockCookie(this.pluginOptions);
}
console.log("Cookie Values:");
console.log(this.getCookieBlockCookie());
}
/**
* Set Cookie
* @param pluginOptions
* @param days
*/
setCookieBlockCookie(pluginOptions, days = 36500) {
let expires;
if (days) {
let date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
}
else {
expires = "";
}
document.cookie = encodeURIComponent('CookieBlock') + "=" + encodeURIComponent(JSON.stringify(pluginOptions)) + expires + "; path=/";
}
/**
* Get Cookie
* @returns {*}
*/
getCookieBlockCookie() {
let nameEQ = encodeURIComponent('CookieBlock') + "=";
let ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) === ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) === 0) {
return new Map(JSON.parse(decodeURIComponent(c.substring(nameEQ.length, c.length))));
}
}
return null;
}
/**
* Erase the Cookie
*/
eraseCookieBlockCookie() {
this.setCookieBlockCookie("", -1);
}
/**
* Disable one module
*
* @param moduleId
*/
disableModule(moduleId) {
// Save options for cookie
let moduleOptions = this.pluginOptions.get(moduleId);
moduleOptions.active = false;
this.pluginOptions.set(moduleId, moduleOptions);
this.setCookieBlockCookie(this.pluginOptions);
// Set the object settings and disable blocking
let moduleObject = this.pluginObjects.get(moduleId);
moduleObject.object.unblock();
}
/**
* Disable one module
*
* @param moduleId
* @param classOptions
*/
enableModule(moduleId, classOptions = {}) {
// Save options for cookie
let moduleOptions = this.pluginOptions.get(moduleId);
moduleOptions.active = true;
moduleOptions.options = classOptions;
this.pluginOptions.set(moduleId, moduleOptions);
this.setCookieBlockCookie(this.pluginOptions);
// Set the object settings and enable blocking
let moduleObject = this.pluginObjects.get(moduleId);
moduleObject.object.setClassOptions(classOptions);
moduleObject.object.block();
this.pluginObjects.set(moduleId, moduleObject);
}
/**
* Set moduleOptions
* @param moduleId
* @param classOptions
*/
setModuleOptions(moduleId, classOptions) {
// Save options for cookie
let moduleOptions = this.pluginOptions.get(moduleId);
moduleOptions.options = classOptions;
this.pluginOptions.set(moduleId, moduleOptions);
this.setCookieBlockCookie(this.pluginOptions);
// Set the object settings
let moduleObject = this.pluginObjects.get(moduleId);
moduleObject.object.setClassOptions(classOptions);
}
/**
* Manage single cookie
* @param moduleId
*/
manageCookie(moduleId) {
let moduleOptions = this.pluginOptions.get(moduleId);
let moduleObject = this.pluginObjects.get(moduleId);
if (moduleOptions.active) {
console.log("Block Module: " + moduleId);
moduleObject.object.block();
}
else {
console.log("Unblock Module: " + moduleId);
moduleObject.object.unblock();
}
}
/**
* Manage the DNT flag in browsers
* @returns {boolean}
*/
manageDoNotTrack() {
if (this.appOptions.get("respectDoNotTrack")) {
let hasDNT = false;
let ie9DNT = false;
let ie10DNT = false;
if (window.navigator.doNotTrack) {
hasDNT = window.navigator.doNotTrack;
console.log(hasDNT);
}
if ('external' in window) {
let ie9DNT = (window.external.InPrivateFilteringEnabled !== undefined) ? window.external.InPrivateFilteringEnabled() : false;
let ie10DNT = (window.external.msTrackingProtectionEnabled !== undefined) ? window.external.msTrackingProtectionEnabled() : false;
console.log(ie9DNT + ie10DNT);
}
if (hasDNT || ie9DNT || ie10DNT) {
let alreadyActive = false;
// Activate all modules initially
this.pluginOptions.forEach((options, moduleId) => {
if (options.active) alreadyActive = true;
});
if (!alreadyActive) {
this.pluginOptions.forEach((options, moduleId) => {
options.active = true;
});
}
// also set a Content Security Policy meta tag to prevent the loading of external js sources
let meta = document.createElement('meta');
meta.httpEquiv = "Content-Security-Policy";
meta.content = "script-src 'self'";
document.getElementsByTagName('head')[0].insertBefore(meta, document.getElementsByTagName('head')[0].firstChild);
console.log("DNT is being respected and active.");
return true;
}
else {
console.log("DNT is being respected, but not active.");
}
}
else {
console.log("DNT is not being respected.");
}
return false;
}
/**
* Set app option
* @param optionKey
* @param optionValue
*/
setAppOption(optionKey, optionValue) {
this.appOptions.set(optionKey, optionValue);
}
/**
* Get app option
* @param optionKey
*/
getAppOption(optionKey) {
return this.appOptions.get(optionKey);
}
/**
* Get all app options
* @returns {Map}
*/
getAppOptions() {
return this.appOptions;
}
/**
* Call a function based on a string literal without use of eval
* @param name
* @param params
* @deprecated
*/
callFunction(name, params = []) {
// find object
let fn = window[name];
// is object a function?
if (typeof fn === "function") fn.apply(null, params);
}
}