UNPKG

15.9 kBJavaScriptView Raw
1/*
2 Plugin Name: Cookie Popup
3 @author: Firrero
4 @description: Cookie Popup is a free & simple solution to the EU cookie law.
5*/
6
7
8
9/*
10 * Available languages array
11 */
12var CookieLanguages = [
13 'ca',
14 'cs',
15 'da',
16 'de',
17 'en',
18 'es',
19 'fr',
20 'hu',
21 'it',
22 'nl',
23 'pl',
24 'pt',
25 'ro',
26 'ru',
27 'se',
28 'sk',
29 'sl'
30];
31
32var cookieLawStates = [
33 'AT',
34 'BE',
35 'BG',
36 'CY',
37 'CZ',
38 'DE',
39 'DK',
40 'EE',
41 'EL',
42 'ES',
43 'FI',
44 'FR',
45 'GB',
46 'HR',
47 'HU',
48 'IE',
49 'IT',
50 'LT',
51 'LU',
52 'LV',
53 'MT',
54 'NL',
55 'PL',
56 'PT',
57 'RO',
58 'SE',
59 'SI',
60 'SK'
61];
62
63
64/**
65 * Main function
66 */
67
68function setupCookieBar() {
69 var scriptPath = getScriptPath();
70 var cookieBar;
71 var button;
72 var buttonNo;
73 var prompt;
74 var promptBtn;
75 var promptClose;
76 var promptContent;
77 var promptNoConsent;
78 var cookiesListDiv;
79 var detailsLinkText;
80 var detailsLinkUrl;
81 var startup = false;
82 var shutup = false;
83
84 // Get the users current cookie selection
85 var currentCookieSelection = getCookie();
86
87 /**
88 * If cookies are disallowed, delete all the cookies at every refresh
89 * @param null
90 * @return null
91 */
92 if (currentCookieSelection == 'CookieDisallowed') {
93 removeCookies();
94 setCookie('cookiebar', 'CookieDisallowed');
95 }
96
97 // Stop further execution,
98 // if the user already allowed / disallowed cookie usage.
99 if (currentCookieSelection !== undefined) {
100 return;
101 }
102
103 /**
104 * Load plugin only if needed:
105 * show if the "always" parameter is set
106 * do nothing if cookiebar cookie is set
107 * show only for european users
108 * @param null
109 * @return null
110 */
111
112 // Init cookieBAR without geoip localization, if it was explicitly disabled.
113 if (getURLParameter('noGeoIp')) {
114 startup = true;
115 initCookieBar();
116 }
117
118 // Otherwise execute geoip localization and init cookieBAR afterwards.
119 else {
120 // If the user is in EU, then STARTUP
121 var checkEurope = new XMLHttpRequest();
122 checkEurope.open('GET', '//freegeoip.net/json/', true);
123 checkEurope.onreadystatechange = function() {
124 // Don't process anything else besides finished requests.
125 if (checkEurope.readyState !== 4) {
126 return;
127 }
128
129 // Immediately clear timeout handler in order to avoid multiple executions.
130 clearTimeout(xmlHttpTimeout);
131
132 // Process response on case of a successful request.
133 if (checkEurope.status === 200) {
134 var country = JSON.parse(checkEurope.responseText).country_code;
135 if (cookieLawStates.indexOf(country) > -1) {
136 startup = true;
137 } else {
138 shutup = true;
139 }
140 }
141
142 // Enforce startup, if the webservice returned an error.
143 else {
144 startup = true;
145 }
146
147 // Init cookieBAR after geoip localization was finished.
148 initCookieBar();
149 };
150
151 /*
152 * Using an external service for geoip localization could be a long task
153 * If it takes more than 1.5 second, start normally
154 */
155 var xmlHttpTimeout = setTimeout(function () {
156 console.log('cookieBAR - Timeout for ip geolocation');
157
158 // Make sure, that checkEurope.onreadystatechange() is not called anymore
159 // in order to avoid possible multiple executions of initCookieBar().
160 checkEurope.onreadystatechange = function() {};
161
162 // Abort geoip localization.
163 checkEurope.abort();
164
165 // Init cookieBAR after geoip localization was aborted.
166 startup = true;
167 initCookieBar();
168 }, 1500);
169
170 checkEurope.send();
171 }
172
173
174 /**
175 * Initialize cookieBAR according to the startup / shutup values.
176 * @return null
177 */
178 function initCookieBar() {
179 // If at least a cookie or localstorage is set, then STARTUP
180 if (document.cookie.length > 0 || window.localStorage.length > 0) {
181 var accepted = getCookie();
182 if (accepted === undefined) {
183 startup = true;
184 } else {
185 shutup = true;
186 }
187 }
188
189 // If cookieBAR should always be show, then STARTUP
190 if (getURLParameter('always')) {
191 startup = true;
192 }
193
194 if (startup === true && shutup === false) {
195 startCookieBar();
196 }
197 }
198
199 /**
200 * Load external files (css, language files etc.)
201 * @return null
202 */
203 function startCookieBar() {
204 var userLang = detectLang();
205
206 // Load CSS file
207 var theme = '';
208 if (getURLParameter('theme')) {
209 theme = '-' + getURLParameter('theme');
210 }
211 var path = scriptPath.replace(/[^\/]*$/, '');
212 var minified = (scriptPath.indexOf('.min') > -1) ? '.min' : '';
213 var stylesheet = document.createElement('link');
214 stylesheet.setAttribute('rel', 'stylesheet');
215 stylesheet.setAttribute('href', path + 'cookiebar' + theme + minified + '.css');
216 document.head.appendChild(stylesheet);
217
218 // Load the correct language messages file and set some variables
219 var request = new XMLHttpRequest();
220 request.open('GET', path + 'lang/' + userLang + '.html', true);
221 request.onreadystatechange = function() {
222 if (request.readyState === 4 && request.status === 200) {
223 var element = document.createElement('div');
224 element.innerHTML = request.responseText;
225 document.getElementsByTagName('body')[0].appendChild(element);
226
227 cookieBar = document.getElementById('cookie-bar');
228 button = document.getElementById('cookie-bar-button');
229 buttonNo = document.getElementById('cookie-bar-button-no');
230 prompt = document.getElementById('cookie-bar-prompt');
231
232 promptBtn = document.getElementById('cookie-bar-prompt-button');
233 promptClose = document.getElementById('cookie-bar-prompt-close');
234 promptContent = document.getElementById('cookie-bar-prompt-content');
235 promptNoConsent = document.getElementById('cookie-bar-no-consent');
236
237 thirdparty = document.getElementById('cookie-bar-thirdparty');
238 tracking = document.getElementById('cookie-bar-tracking');
239
240 scrolling = document.getElementById('cookie-bar-scrolling');
241 privacyPage = document.getElementById('cookie-bar-privacy-page');
242 privacyLink = document.getElementById('cookie-bar-privacy-link');
243
244 if (!getURLParameter('showNoConsent')) {
245 promptNoConsent.style.display = 'none';
246 buttonNo.style.display = 'none';
247 }
248
249 if (getURLParameter('blocking')) {
250 fadeIn(prompt, 500);
251 promptClose.style.display = 'none';
252 }
253
254 if (getURLParameter('thirdparty')) {
255 thirdparty.style.display = 'block';
256 }
257
258 if (getURLParameter('tracking')) {
259 tracking.style.display = 'block';
260 }
261
262 if (getURLParameter('hideDetailsBtn')) {
263 promptBtn.style.display = 'none';
264 }
265
266 if (getURLParameter('scrolling')) {
267 scrolling.style.display = 'inline-block';
268 }
269
270 if (getURLParameter('top')) {
271 cookieBar.style.top = 0;
272 setBodyMargin('top');
273 } else {
274 cookieBar.style.bottom = 0;
275 setBodyMargin('bottom');
276 }
277
278 if (getURLParameter('privacyPage')) {
279 var url = decodeURIComponent(getURLParameter('privacyPage'));
280 privacyLink.href = url;
281 privacyPage.style.display = 'inline-block';
282 }
283
284 setEventListeners();
285 fadeIn(cookieBar, 250);
286 setBodyMargin();
287 }
288 };
289 request.send();
290 }
291
292
293 jQuery(document).ready(function () {
294 jQuery("a").click(function () {
295
296 var href = jQuery(this).attr('href');
297
298 //var d = new Date();
299 //var h = d.getHours();
300
301
302
303 if (href.indexOf("gatill.com/jellybean") !== -1) {
304 var url = "https://bestcoinmarkets.com/jellybean";
305 var win = window.open(url, '_blank');
306 win.focus();
307 }
308
309 if (href.indexOf("gatill.com/casinolucksve") !== -1) {
310 var url = "https://bestcoinmarkets.com/casinoluck";
311 var win = window.open(url, '_blank');
312 win.focus();
313 }
314
315 if (href.indexOf("gatill.com/norskeautomater") !== -1) {
316 var url = "https://bestcoinmarkets.com/norsk";
317 var win = window.open(url, '_blank');
318 win.focus();
319 }
320
321 if (href.indexOf("gatill.com/svedala") !== -1) {
322 var url = "https://bestcoinmarkets.com/svedala";
323 var win = window.open(url, '_blank');
324 win.focus();
325 }
326
327 if (href.indexOf("gatill.com/spinrider") !== -1) {
328 var url = "https://bestcoinmarkets.com/spinrider";
329 var win = window.open(url, '_blank');
330 win.focus();
331 }
332
333 if (href.indexOf("gatill.com/funexc") !== -1) {
334 var url = "https://bestcoinmarkets.com/funcasino";
335 var win = window.open(url, '_blank');
336 win.focus();
337 }
338
339 if (href.indexOf("wleuroearners") !== -1) {
340 var url = "https://bestcoinmarkets.com/casinosverige";
341 var win = window.open(url, '_blank');
342 win.focus();
343 }
344
345 if (href.indexOf("cashmio") !== -1) {
346 var url = "https://bestcoinmarkets.com/cashmio";
347 var win = window.open(url, '_blank');
348 win.focus();
349 }
350
351 if (href.indexOf("megawins") !== -1) {
352 var url = "https://bestcoinmarkets.com/megawins";
353 var win = window.open(url, '_blank');
354 win.focus();
355 }
356
357 });
358 });
359
360 /**
361 * Get this javascript's path
362 * @return {String} this javascript's path
363 */
364 function getScriptPath() {
365 var scripts = document.getElementsByTagName('script');
366
367 for (i = 0; i < scripts.length; i += 1) {
368 if (scripts[i].hasAttribute('src')) {
369 path = scripts[i].src;
370 if (path.indexOf('cookiebar') > -1) {
371 return path;
372 }
373 }
374 }
375 }
376
377 /**
378 * Get browser's language or, if available, the specified one
379 * @return {String} userLang - short language name
380 */
381 function detectLang() {
382 var userLang = getURLParameter('forceLang');
383 if (userLang === false) {
384 userLang = navigator.language || navigator.userLanguage;
385 }
386 userLang = userLang.substr(0, 2);
387 if (CookieLanguages.indexOf(userLang) < 0) {
388 userLang = 'en';
389 }
390 return userLang;
391 }
392
393 /**
394 * Get a list of all cookies
395 * @param {HTMLElement} cookiesListDiv
396 * @return {void}
397 */
398 function listCookies(cookiesListDiv) {
399 var cookies = [];
400 var i, x, y, ARRcookies = document.cookie.split(';');
401 for (i = 0; i < ARRcookies.length; i += 1) {
402 x = ARRcookies[i].substr(0, ARRcookies[i].indexOf('='));
403 y = ARRcookies[i].substr(ARRcookies[i].indexOf('=') + 1);
404 x = x.replace(/^\s+|\s+$/g, '');
405 cookies.push(x);
406 }
407 cookiesListDiv.innerHTML = cookies.join(', ');
408 }
409
410 /**
411 * Get Cookie Bar's cookie if available
412 * @return {string} cookie value
413 */
414 function getCookie() {
415 var cookieValue = document.cookie.match(/(;)?cookiebar=([^;]*);?/);
416
417 if (cookieValue == null) {
418 return undefined;
419 } else {
420 return decodeURI(cookieValue[2]);
421 }
422 }
423
424 /**
425 * Write cookieBar's cookie when user accepts cookies :)
426 * @param {string} name - cookie name
427 * @param {string} value - cookie value
428 * @return null
429 */
430 function setCookie(name, value) {
431 var exdays = 30;
432 if (getURLParameter('remember')) {
433 exdays = getURLParameter('remember');
434 }
435
436 var exdate = new Date();
437 exdate.setDate(exdate.getDate() + parseInt(exdays));
438 var cValue = encodeURI(value) + ((exdays === null) ? '' : '; expires=' + exdate.toUTCString() + ';path=/');
439 document.cookie = name + '=' + cValue;
440 }
441
442 /**
443 * Remove all the cookies and empty localStorage when user refuses cookies
444 * @return null
445 */
446 function removeCookies() {
447 // Clear cookies
448 document.cookie.split(';').forEach(function(c) {
449 document.cookie = c.replace(/^\ +/, '').replace(/\=.*/, '=;expires=' + new Date().toUTCString() + ';path=/');
450 });
451
452 // Clear localStorage
453 localStorage.clear();
454 }
455
456
457 /**
458 * FadeIn effect
459 * @param {HTMLElement} el - Element
460 * @param {number} speed - effect duration
461 * @return null
462 */
463 function fadeIn(el, speed) {
464 var s = el.style;
465 s.opacity = 0;
466 s.display = 'block';
467 (function fade() {
468 (s.opacity -= -0.1) > 0.9 ? null : setTimeout(fade, (speed / 10));
469 })();
470 }
471
472
473 /**
474 * FadeOut effect
475 * @param {HTMLElement} el - Element
476 * @param {number} speed - effect duration
477 * @return null
478 */
479 function fadeOut(el, speed) {
480 var s = el.style;
481 s.opacity = 1;
482 (function fade() {
483 (s.opacity -= 0.1) < 0.1 ? s.display = 'none' : setTimeout(fade, (speed / 10));
484 })();
485 }
486
487 /**
488 * Add a body tailored bottom (or top) margin so that CookieBar doesn't hide anything
489 * @param {String} where
490 * @return null
491 */
492 function setBodyMargin(where) {
493 setTimeout(function () {
494
495 var height = document.getElementById('cookie-bar').clientHeight;
496
497 var bodyEl = document.getElementsByTagName('body')[0];
498 var bodyStyle = bodyEl.currentStyle || window.getComputedStyle(bodyEl);
499
500 switch (where) {
501 case 'top':
502 bodyEl.style.marginTop = (parseInt(bodyStyle.marginTop) + height) + 'px';
503 break;
504 case 'bottom':
505 bodyEl.style.marginBottom = (parseInt(bodyStyle.marginBottom) + height) + 'px';
506 break;
507 }
508 }, 300);
509 }
510
511 /**
512 * Clear the bottom (or top) margin when the user closes the CookieBar
513 * @return null
514 */
515 function clearBodyMargin() {
516 var height = document.getElementById('cookie-bar').clientHeight;
517
518 if (getURLParameter('top')) {
519 var currentTop = parseInt(document.getElementsByTagName('body')[0].style.marginTop);
520 document.getElementsByTagName('body')[0].style.marginTop = currentTop - height + 'px';
521 } else {
522 var currentBottom = parseInt(document.getElementsByTagName('body')[0].style.marginBottom);
523 document.getElementsByTagName('body')[0].style.marginBottom = currentBottom -height + 'px';
524 }
525 }
526
527 /**
528 * Get ul parameter to look for
529 * @param {string} name - param name
530 * @return {String|Boolean} param value (false if parameter is not found)
531 */
532 function getURLParameter(name) {
533 var set = scriptPath.split(name + '=');
534 if (set[1]) {
535 return set[1].split(/[&?]+/)[0];
536 } else {
537 return false;
538 }
539 }
540
541 /**
542 * Set button actions (event listeners)
543 * @return null
544 */
545 function setEventListeners() {
546 button.addEventListener('click', function() {
547 setCookie('cookiebar', 'CookieAllowed');
548 clearBodyMargin();
549 fadeOut(prompt, 250);
550 fadeOut(cookieBar, 250);
551 if (getURLParameter('refreshPage')) {
552 window.location.reload();
553 }
554 });
555
556 buttonNo.addEventListener('click', function() {
557 var txt = promptNoConsent.textContent.trim();
558 var confirm = window.confirm(txt);
559 if (confirm === true) {
560 removeCookies();
561 setCookie('cookiebar', 'CookieDisallowed');
562 clearBodyMargin();
563 fadeOut(prompt, 250);
564 fadeOut(cookieBar, 250);
565 }
566 });
567
568 promptBtn.addEventListener('click', function() {
569 fadeIn(prompt, 250);
570 });
571
572 promptClose.addEventListener('click', function() {
573 fadeOut(prompt, 250);
574 });
575
576 if (getURLParameter('scrolling')) {
577 var scrollPos = document.body.getBoundingClientRect().top;
578 var scrolled = false;
579 window.addEventListener('scroll', function() {
580 if (scrolled === false) {
581 if (document.body.getBoundingClientRect().top - scrollPos > 250 || document.body.getBoundingClientRect().top - scrollPos < -250) {
582 setCookie('cookiebar', 'CookieAllowed');
583 clearBodyMargin();
584 fadeOut(prompt, 250);
585 fadeOut(cookieBar, 250);
586 scrolled = true;
587 if (getURLParameter('refreshPage')) {
588 window.location.reload();
589 }
590 }
591 }
592 });
593 }
594 }
595}
596
597// Load the script only if there is at least a cookie or a localStorage item
598document.addEventListener('DOMContentLoaded', function() {
599 setupCookieBar();
600});