PageSourceSearch

https://themeist.com/wp-content/plugins/cookie-notice/js/front.js?ver=3.1.12

js themeist.com collected 2026-10-02 05:13:50 UTC 16,104 bytes, 528 lines download raw bytes

1// CustomEvent polyfil for IE support
2( function() {
3
4	if ( typeof window.CustomEvent === "function" )
5		return false;
6
7	function CustomEvent( event, params ) {
8		params = params || { bubbles: false, cancelable: false, detail: undefined };
9
10		var evt = document.createEvent( 'CustomEvent' );
11
12		evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail );
13
14		return evt;
15	}
16
17	CustomEvent.prototype = window.Event.prototype;
18
19	window.CustomEvent = CustomEvent;
20} )();
21
22// ClassList polyfil for IE/Safari support
23( function() {
24	var regExp = function ( name ) {
25		return new RegExp( '(^| )' + name + '( |$)' );
26	};
27
28	var forEach = function ( list, fn, scope ) {
29		for ( var i = 0; i < list.length; i++ ) {
30			fn.call( scope, list[i] );
31		}
32	};
33
34	function ClassList( element ) {
35		this.element = element;
36	}
37
38	ClassList.prototype = {
39		add: function() {
40			forEach( arguments, function ( name ) {
41				if ( !this.contains( name ) ) {
42					this.element.className += this.element.className.length > 0 ? ' ' + name : name;
43				}
44			}, this );
45		},
46		remove: function() {
47			forEach( arguments, function ( name ) {
48				this.element.className =
49					this.element.className.replace( regExp( name ), '' );
50			}, this );
51		},
52		toggle: function ( name ) {
53			return this.contains( name )
54				? ( this.remove( name ), false ) : ( this.add( name ), true );
55		},
56		contains: function ( name ) {
57			return regExp( name ).test( this.element.className );
58		},
59		// bonus..
60		replace: function ( oldName, newName ) {
61			this.remove( oldName ), this.add( newName );
62		}
63	};
64
65	// IE8/9, Safari
66	if ( !( 'classList' in Element.prototype ) ) {
67		Object.defineProperty( Element.prototype, 'classList', {
68			get: function() {
69				return new ClassList( this );
70			}
71		} );
72	}
73
74	if ( window.DOMTokenList && DOMTokenList.prototype.replace == null )
75		DOMTokenList.prototype.replace = ClassList.prototype.replace;
76} )();
77
78// cookieNotice
79( function ( window, document, undefined ) {
80
81	var cookieNotice = new function() {
82		// cookie status
83		this.cookiesAccepted = null;
84
85		// notice container
86		this.noticeContainer = null;
87
88		// set cookie value
89		this.setStatus = function ( cookieValue ) {
90			var _this = this;
91			var cookieDomain = '';
92			var cookiePath = '';
93			var date = new Date();
94			var laterDate = new Date();
95
96			// remove listening to scroll event
97			if ( cnArgs.onScroll )
98				window.removeEventListener( 'scroll', this.handleScroll );
99
100			// set cookie type and expiry time in seconds
101			if ( cookieValue === 'accept' ) {
102				cookieValue = 'true';
103				laterDate.setTime( parseInt( date.getTime() ) + parseInt( cnArgs.cookieTime ) * 1000 );
104			} else {
105				cookieValue = 'false';
106				laterDate.setTime( parseInt( date.getTime() ) + parseInt( cnArgs.cookieTimeRejected ) * 1000 );
107			}
108
109			if ( cnArgs.globalCookie )
110				cookieDomain = this.getDomain( document.location.hostname );
111
112			// get domain path in localhost
113			if ( document.location.hostname === 'localhost' )
114				cookiePath = document.location.pathname.split( '/' )[1];
115
116			var secureValue = '';
117
118			if ( document.location.protocol === 'https:' )
119				secureValue = ';secure';
120
121			// set cookie
122			document.cookie = cnArgs.cookieName + '=' + cookieValue + ';expires=' + laterDate.toUTCString() + ';path=/' + cookiePath + ';domain=' + cookieDomain + secureValue;
123
124			// update global status
125			this.cookiesAccepted = cookieValue === 'true';
126
127			// trigger custom event
128			var event = new CustomEvent(
129				'setCookieNotice',
130				{
131					detail: {
132						value: cookieValue,
133						time: date,
134						expires: laterDate,
135						data: cnArgs
136					}
137				}
138			);
139
140			document.dispatchEvent( event );
141
142			this.setBodyClass( [ 'cookies-set', cookieValue === 'true' ? 'cookies-accepted' : 'cookies-refused' ] );
143
144			this.hideCookieNotice();
145
146			// show revoke notice if enabled
147			if ( cnArgs.revokeCookiesOpt === 'automatic' ) {
148				// show cookie notice after the revoke is hidden
149				this.noticeContainer.addEventListener( 'animationend', function handler() {
150					_this.noticeContainer.removeEventListener( 'animationend', handler );
151					_this.showRevokeNotice();
152				} );
153				this.noticeContainer.addEventListener( 'webkitAnimationEnd', function handler() {
154					_this.noticeContainer.removeEventListener( 'webkitAnimationEnd', handler );
155					_this.showRevokeNotice();
156				} );
157			}
158
159			// redirect?
160			if ( cnArgs.redirection && ( ( cookieValue === 'true' && this.cookiesAccepted === null ) || ( co
160okieValue !== this.cookiesAccepted && this.cookiesAccepted !== null ) ) ) {
161				var url = window.location.protocol + '//',
162					hostname = window.location.host + '/' + window.location.pathname;
163
164				// is cache enabled?
165				if ( cnArgs.cache ) {
166					url = url + hostname.replace( '//', '/' ) + ( window.location.search === '' ? '?' : window.location.search + '&' ) + 'cn-reloaded=1' + window.location.hash;
167
168					window.location.href = url;
169				} else {
170					url = url + hostname.replace( '//', '/' ) + window.location.search + window.location.hash;
171
172					window.location.reload( true );
173				}
174
175				return;
176			}
177		};
178
179		// get domain
180		this.getDomain = function( url ) {
181			var regex = new RegExp( /https?:\/\// );
182
183			if ( ! regex.test( url ) )
184				url = 'http://' + url;
185
186			var parts = new URL( url ).hostname.split( '.' );
187
188			return parts.slice( 0 ).slice( -( parts.length === 4 ? 3 : 2 ) ).join( '.' );
189		}
190
191		// get cookie value
192		this.getStatus = function ( bool ) {
193			var value = "; " + document.cookie,
194				parts = value.split( '; cookie_notice_accepted=' );
195
196			if ( parts.length === 2 ) {
197				var val = parts.pop().split( ';' ).shift();
198
199				if ( bool )
200					return val === 'true';
201				else
202					return val;
203			} else
204				return null;
205		};
206
207		// display cookie notice
208		this.showCookieNotice = function() {
209			var _this = this;
210
211			// trigger custom event
212			var event = new CustomEvent(
213				'showCookieNotice',
214				{
215					detail: {
216						data: cnArgs
217					}
218				}
219			);
220
221			document.dispatchEvent( event );
222
223			this.noticeContainer.classList.remove( 'cookie-notice-hidden' );
224			this.noticeContainer.classList.add( 'cn-animated' );
225			this.noticeContainer.classList.add( 'cookie-notice-visible' );
226
227			// detect animation
228			this.noticeContainer.addEventListener( 'animationend', function handler() {
229				_this.noticeContainer.removeEventListener( 'animationend', handler );
230				_this.noticeContainer.classList.remove( 'cn-animated' );
231			} );
232			this.noticeContainer.addEventListener( 'webkitAnimationEnd', function handler() {
233				_this.noticeContainer.removeEventListener( 'webkitAnimationEnd', handler );
234				_this.noticeContainer.classList.remove( 'cn-animated' );
235			} );
236		};
237
238		// hide cookie notice
239		this.hideCookieNotice = function() {
240			var _this = this;
241
242			// trigger custom event
243			var event = new CustomEvent(
244				'hideCookieNotice',
245				{
246					detail: {
247						data: cnArgs
248					}
249				}
250			);
251
252			document.dispatchEvent( event );
253
254			this.noticeContainer.classList.add( 'cn-animated' );
255			this.noticeContainer.classList.remove( 'cookie-notice-visible' );
256
257			// detect animation
258			this.noticeContainer.addEventListener( 'animationend', function handler() {
259				_this.noticeContainer.removeEventListener( 'animationend', handler );
260				_this.noticeContainer.classList.remove( 'cn-animated' );
261				_this.noticeContainer.classList.add( 'cookie-notice-hidden' );
262			} );
263			this.noticeContainer.addEventListener( 'webkitAnimationEnd', function handler() {
264				_this.noticeContainer.removeEventListener( 'webkitAnimationEnd', handler );
265				_this.noticeContainer.classList.remove( 'cn-animated' );
266				_this.noticeContainer.classList.add( 'cookie-notice-hidden' );
267			} );
268		};
269
270		// display revoke notice
271		this.showRevokeNotice = function() {
272			var _this = this;
273
274			// trigger custom event
275			var event = new CustomEvent(
276				'showRevokeNotice',
277				{
278					detail: {
279						data: cnArgs
280					}
281				}
282			);
283
284			document.dispatchEvent( event );
285
286			this.noticeContainer.classList.remove( 'cookie-revoke-hidden' );
287			this.noticeContainer.classList.add( 'cn-animated' );
288			this.noticeContainer.classList.add( 'cookie-revoke-visible' );
289
290			// detect animation
291			this.noticeContainer.addEventListener( 'animationend', function handler() {
292				_this.noticeContainer.removeEventListener( 'animationend', handler );
293				_this.noticeContainer.classList.remove( 'cn-animated' );
294			} );
295			this.noticeContainer.addEventListener( 'webkitAnimationEnd', function handler() {
296				_this.noticeContainer.removeEventListener( 'webkitAnimationEnd', handler );
297				_this.noticeContainer.classList.remove( 'cn-animated' );
298			} );
299		};
300
301		// hide revoke notice
302		this.hideRevokeNotice = function() {
303			var _this = this;
304
305			// trigger custom event
306			var event = new CustomEvent(
307				'hideRevokeNotice',
308				{
309					detail: {
310						data: cnArgs
311					}
312				}
313			);
314
315			document.dispatchEvent( event );
316
317			this.noticeContainer.classList.add( 'cn-animated' );
318			this.noticeContainer.classList.remove( 'cookie-revoke-visible' );
319
320			// detect animation
321			this.noticeContainer.addEventListener( 'animationend', function handler() {
322				_this.noticeContainer.removeEventListener( 'animationend', handler );
323				_this.noticeContainer.classList.remove( 'cn-animated' );
324				_this.noticeContainer.classList.add( 'cookie-revoke-hidden' );
325			} );
326			this.noticeContainer.addEventListener( 'webkitAnimationEnd', function handler() {
327				_this.noticeContainer.removeEventListener( 'webkitAnimationEnd', handler );
328				_this.noticeContainer.classList.remove( 'cn-animated' );
329				_this.noticeContainer.classList.add( 'cookie-revoke-hidden' );
330			} );
331		};
332
333		// change body classes
334		this.setBodyClass = function ( classes ) {
335			// remove body classes
336			document.body.classList.remove( 'cookies-revoke' );
337			document.body.classList.remove( 'cookies-accepted' );
338			document.body.classList.remove( 'cookies-refused' );
339			document.body.classList.remove( 'cookies-set' );
340			document.body.classList.remove( 'cookies-not-set' );
341
342			// add body classes
343			for ( var i = 0; i < classes.length; i++ ) {
344				document.body.classList.add( classes[i] );
345			}
346		};
347
348		// handle mouse scrolling
349		this.handleScroll = function() {
350			var scrollTop = window.pageYOffset || ( document.documentElement || document.body.parentNode || document.body ).scrollTop
351
352			// accept cookie
353			if ( scrollTop > parseInt( cnArgs.onScrollOffset ) )
354				this.setStatus( 'accept' );
355		};
356
357		// cross browser compatible closest function
358		this.getClosest = function ( elem, selector ) {
359			// element.matches() polyfill
360			if ( !Element.prototype.matches ) {
361				Element.prototype.matches =
362					Element.prototype.matchesSelector ||
363					Element.prototype.mozMatchesSelector ||
364					Element.prototype.msMatchesSelector ||
365					Element.prototype.oMatchesSelector ||
366					Element.prototype.webkitMatchesSelector ||
367					function ( s ) {
368						var matches = ( this.document || this.ownerDocument ).querySelectorAll( s ),
369							i = matches.length;
370						while ( --i >= 0 && matches.item( i ) !== this ) {
371						}
372						return i > -1;
373					};
374			}
375
376			// get the closest matching element
377			for ( ; elem && elem !== document; elem = elem.parentNode ) {
378				if ( elem.matches( selector ) )
379					return elem;
380			}
381
382			return null;
383		};
384
385		// check if displaye in an iframe
386		this.inIframe = function() {
387			try {
388				return window.self !== window.top;
389			} catch (e) {
390				return true;
391			}
392		}
393
394		// initialize
395		this.init = function() {
396			var _this = this;
397
398			// bail if in iframe
399			if ( this.inIframe() === true )
400				return;
401
402			this.cookiesAccepted = this.getStatus( true );
403			this.noticeContainer = document.getElementById( 'cookie-notice' );
404
405			// no container?
406			if ( ! this.noticeContainer )
407				return;
408
409			var cookieButtons = document.getElementsByClassName( 'cn-set-cookie' ),
410				revokeButtons = document.getElementsByClassName( 'cn-revoke-cookie' ),
411				linkButton = document.getElementById( 'cn-more-info' ),
412				closeButton = document.getElementById( 'cn-close-notice' );
413
414			// add effect class
415			this.noticeContainer.classList.add( 'cn-effect-' + cnArgs.hideEffect );
416
417			// check cookies status
418			if ( this.cookiesAccepted === null ) {
419				// handle on scroll
420				if ( cnArgs.onScroll )
421					window.addEventListener( 'scroll', function ( e ) {
422						_this.handleScroll();
423					} );
424
425				// handle on click
426				if ( cnArgs.onClick )
427					window.addEventListener( 'click', function ( e ) {
428						var outerContainer = _this.getClosest( e.target, '#cookie-notice' );
429
430						// accept notice if clicked element is not inside the container
431						if ( outerContainer === null )
432							_this.setStatus( 'accept' );
433					}, true );
434
435				this.setBodyClass( [ 'cookies-not-set' ] );
436
437				// show cookie notice
438				this.showCookieNotice();
439			} else {
440				this.setBodyClass( [ 'cookies-set', this.cookiesAccepted === true ? 'cookies-accepted' : 'cookies-refused' ] );
441
442				// show revoke notice if enabled
443				if ( cnArgs.revokeCookies && cnArgs.revokeCookiesOpt === 'automatic' )
444					this.showRevokeNotice();
445			}
446
447			// handle cookie buttons click
448			for ( var i = 0; i < cookieButtons.length; i++ ) {
449				cookieButtons[i].addEventListener( 'click', function ( e ) {
450					e.preventDefault();
451					// Chrome double click event fix
452					e.stopPropagation();
453
454					_this.setStatus( this.dataset.cookieSet );
455				} );
456			}
457			
458		// handle link button
459		if ( linkButton !== null ) {
460			linkButton.addEventListener( 'click', function ( e ) {
461				var linkUrl = this.dataset.linkUrl || this.getAttribute( 'href' );
462				var linkTarget = this.dataset.linkTarget || this.getAttribute( 'target' ) || '_self';
463
464				// only intercept when we have a destination
465				if ( ! linkUrl )
466					return;
467
468				e.preventDefault();
469				// Chrome double click event fix
470				e.stopPropagation();
471
472				window.open( linkUrl, linkTarget );
473			} );
474		}
475
476		// handle close button
477		if ( closeButton !== null ) {
478			closeButton.addEventListener( 'keydown', function ( e ) {
479				if ( e.key === 'Enter' || e.key === ' ' || e.key === 'Spacebar' || e.keyCode === 13 || e.keyCode === 32 ) {
480					e.preventDefault();
481					// Chrome double click event fix
482					e.stopPropagation();
483
484					_this.setStatus( 'reject' );
485				}
486			} );
487
488			closeButton.addEventListener( 'click', function ( e ) {
489				e.preventDefault();
490				// Chrome double click event fix
491				e.stopPropagation();
492
493					_this.setStatus( 'reject' );
494				} );
495			}
496
497			// handle revoke buttons click
498			for ( var i = 0; i < revokeButtons.length; i++ ) {
499				revokeButtons[i].addEventListener( 'click', function ( e ) {
500					e.preventDefault();
501
502					// hide revoke notice
503					if ( _this.noticeContainer.classList.contains( 'cookie-revoke-visible' ) ) {
504						_this.hideRevokeNotice();
505
506						// show cookie notice after the revoke is hidden
507						_this.noticeContainer.addEventListener( 'animationend', function handler() {
508							_this.noticeContainer.removeEventListener( 'animationend', handler );
509							_this.showCookieNotice();
510						} );
511						_this.noticeContainer.addEventListener( 'webkitAnimationEnd', function handler() {
512							_this.noticeContainer.removeEventListener( 'webkitAnimationEnd', handler );
513							_this.showCookieNotice();
514						} );
515						// show cookie notice
516					} else if ( _this.noticeContainer.classList.contains( 'cookie-notice-hidden' ) && _this.noticeContainer.classList.contains( 'cookie-revoke-hidden' ) )
517						_this.showCookieNotice();
518				} );
519			}
520		};
521	}
522
523	// initialize plugin
524	window.addEventListener( 'load', function() {
525		cookieNotice.init();
526	}, false );
527
528} )( window, document, undefined );

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.