kollapsminoriteten/wp-content/plugins/jetpack/modules/widgets/eu-cookie-law/eu-cookie-law.js

97 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-09-15 14:30:05 +02:00
( function () {
2019-11-15 23:26:29 +01:00
var cookieValue = document.cookie.replace(
/(?:(?:^|.*;\s*)eucookielaw\s*\=\s*([^;]*).*$)|^.*$/,
'$1'
),
2020-03-03 18:49:45 +01:00
overlay = document.getElementById( 'eu-cookie-law' ),
widget = document.querySelector( '.widget_eu_cookie_law_widget' ),
2021-04-27 08:32:47 +02:00
inCustomizer = widget && widget.hasAttribute( 'data-customize-widget-id' ),
2020-03-03 18:49:45 +01:00
getScrollTop,
2019-11-15 23:26:29 +01:00
initialScrollPosition,
scrollFunction;
2021-04-27 08:32:47 +02:00
if ( null === widget || null === overlay ) {
return;
}
2020-03-03 18:49:45 +01:00
/**
* Gets the amount that the window is scrolled.
*
* @return int The distance from the top of the document.
*/
2020-09-15 14:30:05 +02:00
getScrollTop = function () {
2020-03-03 18:49:45 +01:00
return Math.abs( document.body.getBoundingClientRect().y );
};
if ( overlay.classList.contains( 'top' ) ) {
widget.classList.add( 'top' );
2019-11-15 23:26:29 +01:00
}
2020-03-03 18:49:45 +01:00
if ( overlay.classList.contains( 'ads-active' ) ) {
2019-11-15 23:26:29 +01:00
var adsCookieValue = document.cookie.replace(
/(?:(?:^|.*;\s*)personalized-ads-consent\s*\=\s*([^;]*).*$)|^.*$/,
'$1'
);
2020-09-15 14:30:05 +02:00
if ( '' !== cookieValue && '' !== adsCookieValue && ! inCustomizer ) {
2020-03-03 18:49:45 +01:00
overlay.parentNode.removeChild( overlay );
2019-11-15 23:26:29 +01:00
}
2020-09-15 14:30:05 +02:00
} else if ( '' !== cookieValue && ! inCustomizer ) {
2020-03-03 18:49:45 +01:00
overlay.parentNode.removeChild( overlay );
2019-11-15 23:26:29 +01:00
}
2020-03-03 18:49:45 +01:00
document.body.appendChild( widget );
overlay.querySelector( 'form' ).addEventListener( 'submit', accept );
2019-11-15 23:26:29 +01:00
2020-03-03 18:49:45 +01:00
if ( overlay.classList.contains( 'hide-on-scroll' ) ) {
initialScrollPosition = getScrollTop();
2020-09-15 14:30:05 +02:00
scrollFunction = function () {
2020-03-03 18:49:45 +01:00
if ( Math.abs( getScrollTop() - initialScrollPosition ) > 50 ) {
2019-11-15 23:26:29 +01:00
accept();
}
};
2020-03-03 18:49:45 +01:00
window.addEventListener( 'scroll', scrollFunction );
} else if ( overlay.classList.contains( 'hide-on-time' ) ) {
setTimeout( accept, overlay.getAttribute( 'data-hide-timeout' ) * 1000 );
2019-11-15 23:26:29 +01:00
}
var accepted = false;
function accept( event ) {
if ( accepted ) {
return;
}
accepted = true;
if ( event && event.preventDefault ) {
event.preventDefault();
}
2020-03-03 18:49:45 +01:00
if ( overlay.classList.contains( 'hide-on-scroll' ) ) {
window.removeEventListener( 'scroll', scrollFunction );
2019-11-15 23:26:29 +01:00
}
var expireTime = new Date();
expireTime.setTime(
2020-03-03 18:49:45 +01:00
expireTime.getTime() + overlay.getAttribute( 'data-consent-expiration' ) * 24 * 60 * 60 * 1000
2019-11-15 23:26:29 +01:00
);
document.cookie =
'eucookielaw=' + expireTime.getTime() + ';path=/;expires=' + expireTime.toGMTString();
2020-03-03 18:49:45 +01:00
if (
overlay.classList.contains( 'ads-active' ) &&
overlay.classList.contains( 'hide-on-button' )
) {
2019-11-15 23:26:29 +01:00
document.cookie =
'personalized-ads-consent=' +
expireTime.getTime() +
';path=/;expires=' +
expireTime.toGMTString();
}
2020-03-03 18:49:45 +01:00
overlay.classList.add( 'hide' );
2020-09-15 14:30:05 +02:00
setTimeout( function () {
2020-03-03 18:49:45 +01:00
overlay.parentNode.removeChild( overlay );
2024-02-16 11:03:01 +01:00
widget.parentNode.removeChild( widget );
2020-03-03 18:49:45 +01:00
}, 400 );
2019-11-15 23:26:29 +01:00
}
2020-03-03 18:49:45 +01:00
} )();