UNPKG

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