const isValidUrl = (url) => {
  try {
    new URL(url);
    return true;
  } catch {
    return false;
  }
};

const observer = new MutationObserver(() => {
  // make link clickable from batter badge plugin
  document.querySelectorAll('[data-button-link]').forEach((el) => {
    el.addEventListener('click', (e) => {
      e.stopPropagation();
      e.preventDefault();
      const link = el.dataset.buttonLink;
      if (isValidUrl(link)) {
        window.open(link, '_blank');
      }
    });
  });
});

observer.observe(document.body, {
  childList: true,
  subtree: true,
});
