UNPKG

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