Form Block Page URL

<!-- @tuanphan - Form Lightbox Page URL -->
<script>
window.addEventListener("load", () => {
  const triggerInputChange = (inputElement, value) => {
    const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
      window.HTMLInputElement.prototype,
      "value"
    ).set;
    nativeInputValueSetter.call(inputElement, value);
    inputElement.dispatchEvent(new Event("input", { bubbles: true }));
  };

  const fillPageURL = (root) => {
    const subjectInput = root.querySelector('[placeholder="Page URL"]');
    if (subjectInput) {
      const subjectField = subjectInput.closest('.form-item');
      if (subjectField) subjectField.hidden = true;
      triggerInputChange(subjectInput, location.pathname);
    }
  };

  document.querySelectorAll('.sqs-block-form').forEach(fillPageURL);

  const observer = new MutationObserver((mutations) => {
    for (const mutation of mutations) {
      for (const node of mutation.addedNodes) {
        if (node.nodeType !== 1) continue;
        if (node.matches?.('.sqs-block-form') || node.querySelector?.('.sqs-block-form')) {
          const blocks = node.matches('.sqs-block-form')
            ? [node]
            : node.querySelectorAll('.sqs-block-form');
          blocks.forEach(fillPageURL);
        }
        if (node.querySelector?.('[placeholder="Page URL"]')) {
          fillPageURL(node);
        }
      }
    }
  });

  observer.observe(document.body, { childList: true, subtree: true });
});
</script>

Demo: https://overlander-ireland.squarespace.com/current-stock-portfolio/2025-2-stall-horsebox-used?noredirect

Buy me a coffee