Catch current session parameters and pass them dynamically to URL modals

A couple of days ago @acjnas helped me to develop a custom code that I’ve been using a lot and I suspect it might be useful to much more people.

I had a large list of students from different schools and several users (teachers) for each school, then, I had a page in which you could take exams to each student, so I wanted to know which teacher specifically was taking the exam to the student, without asking the teacher to fill that info everytime and avoiding errors from selecting the wrong teacher etc.

The code grabs a parameter from the current session (Record ID in my case) and pass it to a URL that opens thru a modal, I haven’t tried, but I understand you could grab other parameters too.

<script>
  window.addEventListener('load', () => {
    const checkUserInterval = setInterval(() => {
      if (window.logged_in_user && window.logged_in_user['Record ID']) {
        const recordId = window.logged_in_user['Record ID'];
        console.log("[LOG] Record ID found:", recordId);
        clearInterval(checkUserInterval);

        function updateIframeSrc(iframe) {
          try {
            const url = new URL(iframe.src);
            url.searchParams.set("us", recordId);
            const updatedSrc = url.toString();
            iframe.src = updatedSrc;
            console.log("[LOG] Updated iframe src with 'us' param:", updatedSrc);
          } catch (e) {
            console.warn("[WARN] Could not parse iframe src:", iframe.src);
          }
        }

        // Immediate deep check
        const deepScan = () => {
          const iframes = document.querySelectorAll('iframe[src*="forms.fillout.com"]');
          iframes.forEach((iframe) => {
            console.log("[LOG] Found existing iframe:", iframe.src);
            updateIframeSrc(iframe);
          });
        };

        deepScan(); // Check right now in case it's already there

        // Also watch for new ones being added
        const observer = new MutationObserver((mutationsList) => {
          for (const mutation of mutationsList) {
            for (const node of mutation.addedNodes) {
              if (node.nodeType === 1) {
                if (node.tagName === 'IFRAME' && node.src.includes("forms.fillout.com")) {
                  console.log("[LOG] Fillout iframe added to DOM:", node.src);
                  updateIframeSrc(node);
                } else {
                  // Also check inside nested nodes (like modals)
                  const nestedIframes = node.querySelectorAll?.('iframe[src*="forms.fillout.com"]');
                  nestedIframes?.forEach((iframe) => {
                    console.log("[LOG] Nested iframe added:", iframe.src);
                    updateIframeSrc(iframe);
                  });
                }
              }
            }
          }
        });

        observer.observe(document.body, { childList: true, subtree: true });
        console.log("[LOG] MutationObserver activated.");
      } else {
        console.log("[LOG] Waiting for Record ID...");
      }
    }, 200);
  });
</script>```

Espero les sea de gran utilidad ;)

Buena semana para todos!
1 Like

Thank you for sharing the script with the community.