/
cookietool.js
370 lines (338 loc) · 11.2 KB
/
cookietool.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/** Main class */
window.CookieTool = function() {};
/** Utility functions */
CookieTool.Utils = {
/**
* Extend an object
* @param {Object} obj1
* @param {Object} obj2
* @return {Object} the merged object
*/
extend: function(obj1, obj2) {
var ret = obj1,
prop;
for (prop in obj2) {
if(obj2.hasOwnProperty(prop)) {
ret[prop] = obj2[prop];
}
}
return ret;
},
/**
* Load a script asynchronously
* @param {String} src the script src
* @param {Function} callback an optionall callback
*/
loadScript: (function() {
var firstjs = document.getElementsByTagName('script')[0];
return function(src, callback) {
var s = document.createElement('script'),
loaded;
s.async = true;
s.onload = s.onreadystatechange = function() {
if( ! loaded && (! s.readyState || s.readyState === 'complete' || s.readyState === 'loaded') ) {
loaded = true;
if( callback && typeof callback === 'function' ) {
callback.call(null, s);
}
}
};
s.src = src;
firstjs.parentNode.insertBefore(s, firstjs);
};
}())
};
CookieTool.Config = (function() {
var config = {
'position': 'top',
'message': 'En este sitio se usan cookies para ofrecer una experiencia más personalizada. <a href="{{link}}" target="_blank">Más información</a>.<br>¿Nos consiente usar cookies?',
'link': '/cookies',
'agreetext': 'Sí',
'declinetext': 'No'
};
return {
get: function(key) {
return config[key];
},
set: function(key, val) {
if( typeof key === 'string' ) {
config[key] = val;
} else {
config = CookieTool.Utils.extend(config, key);
}
}
};
}());
/**
* Event API for customisation
*/
CookieTool.Event = (function() {
/** Where the callbacks are stored */
var events = {};
return {
/**
* Add an event listener
* @param {String} name the identifier
* @param {Function} callback
* @return undefined
*/
on: function(name, callback) {
if( events[name] === undefined ) {
events[name] = [];
}
if( typeof callback === 'function' ) {
events[name].push(callback);
}
},
/**
* Trigger all event listeners for an identifier
* @param {String} name the identifier
* @return undefined
*/
trigger: function(name) {
var cbs = events[name],
i = 0,
len;
if( ! cbs ) {
return;
}
len = cbs.length;
for (; i < len; i++) {
cbs[i]();
}
}
};
}());
/**
* Cookie functions
*/
CookieTool.Cookie = {
/**
* Get a cookie value
* @param {String} key
* @return {String} the value
*/
get: function(key) {
return decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*" + encodeURIComponent(key).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) || null;
},
/**
* Set a cookie value
* @param {String} key
* @param {String} value
* @param {Number} days
* @param {String} domain
* @param {String} path
*/
set: function(key, value, days, domain, path) {
var cookie = key + "=" + encodeURIComponent(value),
date;
if (days) {
date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
cookie += "; expires="+date.toGMTString();
}
if ( domain ) {
cookie += "; domain=" + domain;
}
if( ! path ) {
path = "/";
}
cookie += "; path=" + path;
document.cookie = cookie;
},
/**
* Delete a cookie
* @param {String} key
* @param {String} domain necessary for GA cookies
*/
remove: function(key, domain) {
CookieTool.Cookie.set(key,"",-1, domain);
}
};
/**
* Storage functions (HTML5 localStorage based) (permanent storage)
*/
if( window.localStorage ) {
CookieTool.Storage = {
/**
* Get a stored value
* @param {String} key
* @return {String} the value
*/
get: function(key) {
return window.localStorage.getItem(key);
},
/**
* Set a value
* @param {String} key
* @param {String} value
*/
set: function(key, value) {
return window.localStorage.setItem(key, value);
},
/**
* Delete a stored value
* @param {String} key
*/
remove: function(key) {
return window.localStorage.removeItem(key);
}
};
} else {
// Cookie based storage
CookieTool.Storage = CookieTool.Cookie;
}
/**
* Main API
*/
CookieTool.API = {
/**
* Some status codes
*/
statuses: {
'AGREE': '1',
'DECLINE': '0',
'UNDETERMINED': null
},
/**
* Set/get the current status of tracking
*/
status: function(value) {
if( value === undefined ) {
return CookieTool.Storage.get('ctstatus');
}
if( CookieTool.API.statuses[value] ) {
return CookieTool.Storage.set('ctstatus', CookieTool.API.statuses[value], 365);
}
return CookieTool.Storage.set('ctstatus', value, 365);
},
/**
* Ask for cookie consenting
*/
ask: function() {
var message,
status,
body = document.body;
// Already agreed
if( CookieTool.API.status() === CookieTool.API.statuses.AGREE ) {
return CookieTool.API.agree();
}
if( CookieTool.API.status() === CookieTool.API.statuses.DECLINE ) {
return CookieTool.API.decline();
}
message = document.createElement('div');
message.className = 'cookietool-message cookietool-message-' + CookieTool.Config.get('position');
body.className += ' cookietool';
// No overcomplications with event listeners
message.onclick = function(e) {
var target, action;
e = e || window.event;
target = e.target || e.srcElement;
action = target.getAttribute('data-action');
if( action && CookieTool.API[action] ) {
CookieTool.API[action]();
if( e.preventDefault ) {
e.preventDefault();
} else {
e.returnValue = false;
}
message.parentNode.removeChild(message);
body.className = body.className.replace(/\bcookietool\b/, '');
return false;
}
};
// onclick="" in <button>'s to fix event delegation in Safari for iPhone: http://www.quirksmode.org/blog/archives/2010/09/click_event_del.html
message.innerHTML = '<p>' + CookieTool.Config.get('message').replace(/\{\{link\}\}/g, CookieTool.Config.get('link')) + '</p><button data-action="agree" onclick="">' + CookieTool.Config.get('agreetext') + '</button> <button data-action="decline" onclick="">' + CookieTool.Config.get('declinetext') + '</button>';
body.appendChild(message);
},
/**
* Assume implied agreement
* Note that you'll need at least somewhere in your page a link with a decline option (eg: <button onclick="CookieTool.API.decline()">I don't want cookies</button>)
*/
impliedAgreement: function() {
var status = CookieTool.API.status();
switch(status) {
case CookieTool.API.statuses.DECLINE:
CookieTool.API.decline();
break;
// case CookieTool.API.statuses.UNDETERMINED:
// case CookieTool.API.statuses.AGREE:
default:
CookieTool.API.agree();
}
},
displaySettings: function(container) {
var status = CookieTool.API.status();
if( ! container ) {
return;
}
container.className += ' cookietool-settings';
container.onclick = function(e) {
var target, action;
e = e || window.event;
target = e.target || e.srcElement;
action = target.getAttribute('data-action');
if( action && CookieTool.API[action] ) {
CookieTool.API[action]();
CookieTool.API.displaySettings(container);
if( e.preventDefault ) {
e.preventDefault();
} else {
e.returnValue = false;
}
return false;
}
};
if( status === CookieTool.API.statuses.AGREE ) {
container.innerHTML = 'Actualmente <strong>aceptas</strong> el uso de cookies en el sitio. <a role="button" data-action="decline" href="#">Pulsa aquí para no permitir cookies</a>';
} else if ( status === CookieTool.API.statuses.DECLINE ) {
container.innerHTML = 'Actualmente <strong>no aceptas</strong> el uso de cookies en el sitio. <a role="button" data-action="agree" href="#">Pulsa aquí para permitir cookies</a>';
} else {
container.innerHTML = 'Aún no has establecido tu configuración. Haz click <a role="button" data-action="agree" href="#">aquí</a> si quieres aceptar el uso de cookies, o <a role="button" data-action="decline" href="#">aquí</a> si no.';
}
},
/** Agree */
agree: function() {
CookieTool.API.status('AGREE');
CookieTool.Event.trigger('agree');
},
/** Decline */
decline: function() {
CookieTool.API.status('DECLINE');
CookieTool.Event.trigger('decline');
}
};
/**
* Default id for settings, allows to put the script at the footer with no worries
*/
if( document.getElementById('cookietool-settings') ) {
CookieTool.API.displaySettings(document.getElementById('cookietool-settings'));
}
/**
* Default behaviour on agree: Load google analytics and adsense if present
*/
CookieTool.Event.on('agree', function() {
if( window.adsbygoogle ) {
CookieTool.Utils.loadScript('http://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js');
}
if( window._gaq ) {
CookieTool.Utils.loadScript(('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js');
} else if ( window.ga ) {
CookieTool.Utils.loadScript('//wwww.google-analytics.com/analytics.js');
}
});
/**
* Default behaviour on decline: Delete GA cookies
*/
CookieTool.Event.on('decline', function() {
var cookiestodelete = ['__utma', '__utmb', '__utmc', '__utmz', '__utmv'],
i = 0,
len = cookiestodelete.length,
domain = window.location.hostname;
if( (/^www\./).test(domain) ) {
domain = domain.substring(4);
}
for( ; i < len; i++) {
CookieTool.Cookie.remove(cookiestodelete[i], domain);
}
});