Replace anchor links with JS

<?php
/**
 * captures anchor links and instead scrolls
 * with JavaScript
 * Fixes issues with # and Facet
 * Code credit to https://www.the-art-of-web.com/javascript/remove-anchor-links/
 **/

add_action( 'facetwp_scripts', function() {
    ?>
      <script>

      window.addEventListener("DOMContentLoaded", function(e) {

        // Original JavaScript code by Chirp Internet: www.chirpinternet.eu
        // Please acknowledge use of this code by including this header.

        var links = document.getElementsByTagName("A");

        for(let i=0; i < links.length; i++) {

          if(!links[i].hash) {
            continue;
          }

          if(links[i].origin + links[i].pathname != self.location.href) {
            continue;
          }

          (function(anchorPoint) {
            links[i].addEventListener("click", function(e) {
              anchorPoint.scrollIntoView(true);
              e.preventDefault();
            }, false);
          })(document.getElementById(links[i].hash.replace(/#/, "")));

        }

      }, false);

    </script>
    <?php
  }, 100 );