UNPKG

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