暫無描述

eu-cookie-law.js 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ( function () {
  2. var cookieValue = document.cookie.replace(
  3. /(?:(?:^|.*;\s*)eucookielaw\s*\=\s*([^;]*).*$)|^.*$/,
  4. '$1'
  5. ),
  6. overlay = document.getElementById( 'eu-cookie-law' ),
  7. widget = document.querySelector( '.widget_eu_cookie_law_widget' ),
  8. inCustomizer = widget && widget.hasAttribute( 'data-customize-widget-id' ),
  9. getScrollTop,
  10. initialScrollPosition,
  11. scrollFunction;
  12. if ( null === widget || null === overlay ) {
  13. return;
  14. }
  15. /**
  16. * Gets the amount that the window is scrolled.
  17. *
  18. * @return int The distance from the top of the document.
  19. */
  20. getScrollTop = function () {
  21. return Math.abs( document.body.getBoundingClientRect().y );
  22. };
  23. if ( overlay.classList.contains( 'top' ) ) {
  24. widget.classList.add( 'top' );
  25. }
  26. if ( overlay.classList.contains( 'ads-active' ) ) {
  27. var adsCookieValue = document.cookie.replace(
  28. /(?:(?:^|.*;\s*)personalized-ads-consent\s*\=\s*([^;]*).*$)|^.*$/,
  29. '$1'
  30. );
  31. if ( '' !== cookieValue && '' !== adsCookieValue && ! inCustomizer ) {
  32. overlay.parentNode.removeChild( overlay );
  33. }
  34. } else if ( '' !== cookieValue && ! inCustomizer ) {
  35. overlay.parentNode.removeChild( overlay );
  36. }
  37. document.body.appendChild( widget );
  38. overlay.querySelector( 'form' ).addEventListener( 'submit', accept );
  39. if ( overlay.classList.contains( 'hide-on-scroll' ) ) {
  40. initialScrollPosition = getScrollTop();
  41. scrollFunction = function () {
  42. if ( Math.abs( getScrollTop() - initialScrollPosition ) > 50 ) {
  43. accept();
  44. }
  45. };
  46. window.addEventListener( 'scroll', scrollFunction );
  47. } else if ( overlay.classList.contains( 'hide-on-time' ) ) {
  48. setTimeout( accept, overlay.getAttribute( 'data-hide-timeout' ) * 1000 );
  49. }
  50. var accepted = false;
  51. function accept( event ) {
  52. if ( accepted ) {
  53. return;
  54. }
  55. accepted = true;
  56. if ( event && event.preventDefault ) {
  57. event.preventDefault();
  58. }
  59. if ( overlay.classList.contains( 'hide-on-scroll' ) ) {
  60. window.removeEventListener( 'scroll', scrollFunction );
  61. }
  62. var expireTime = new Date();
  63. expireTime.setTime(
  64. expireTime.getTime() + overlay.getAttribute( 'data-consent-expiration' ) * 24 * 60 * 60 * 1000
  65. );
  66. document.cookie =
  67. 'eucookielaw=' + expireTime.getTime() + ';path=/;expires=' + expireTime.toGMTString();
  68. if (
  69. overlay.classList.contains( 'ads-active' ) &&
  70. overlay.classList.contains( 'hide-on-button' )
  71. ) {
  72. document.cookie =
  73. 'personalized-ads-consent=' +
  74. expireTime.getTime() +
  75. ';path=/;expires=' +
  76. expireTime.toGMTString();
  77. }
  78. overlay.classList.add( 'hide' );
  79. setTimeout( function () {
  80. overlay.parentNode.removeChild( overlay );
  81. var widgetSection = document.querySelector( '.widget.widget_eu_cookie_law_widget' );
  82. widgetSection.parentNode.removeChild( widgetSection );
  83. }, 400 );
  84. }
  85. } )();