document.querySelectorAll('.f3-widget-paginator a').forEach(function (el) {
  el.addEventListener('click', newsAjaxHandler);
});

function newsAjaxHandler(e) {
  var ajaxUrl = e.target.getAttribute('href');
  if (ajaxUrl !== undefined && ajaxUrl !== '') {
    e.preventDefault();
    history.pushState(null, '', ajaxUrl.split('?')[0]);
    var container = e.target.closest('.frame').getAttribute('id');
    document.getElementById(container).classList.add('loading');
    var xhr = new XMLHttpRequest();
    xhr.open('GET', ajaxUrl, true);
    xhr.onreadystatechange = function() {
      if (xhr.readyState === 4 && xhr.status === 200) {
        var parser = new DOMParser();
        var result = parser.parseFromString(xhr.responseText, 'text/html');
        var ajaxDom = result.getElementById(container);
        ajaxDom.querySelectorAll('.f3-widget-paginator a').forEach(function (el) {
          el.addEventListener('click', newsAjaxHandler);
        });
        document.getElementById(container).replaceWith(ajaxDom);
        window.scrollTo({
          top: document.getElementById(container).offsetTop - 111,
          behavior: 'smooth'
        });
      }
    };
    xhr.send();
  }
}
