'); doc.write(element.outerHTML); doc.write(''); doc.close(); const finishOnce = (() => { let done = false; return function() { if (done) return; done = true; try { document.body.removeChild(iframe); } catch {} if (loader) loader.style.display = "none"; const modal = document.getElementById("hip-pdf-modal"); if (modal) { modal.classList.remove("active"); setTimeout(() => modal.style.display = "none", 400); } window.location.href = THANK_YOU_URL; }; })(); // Some browsers (Chrome/Edge/Firefox) fire on the iframe; Safari often fires on the parent const childWin = iframe.contentWindow; // Child afterprint childWin.onafterprint = finishOnce; // Parent afterprint (Safari fallback) const onParentAfter = () => { window.removeEventListener('afterprint', onParentAfter); finishOnce(); }; window.addEventListener('afterprint', onParentAfter); // matchMedia('print') fallback: when it flips to false, printing is done let mql = null; if (childWin.matchMedia) { mql = childWin.matchMedia('print'); const onChange = (e) => { if (!e.matches) { if (mql.removeEventListener) mql.removeEventListener('change', onChange); else if (mql.removeListener) mql.removeListener(onChange); finishOnce(); } }; if (mql.addEventListener) mql.addEventListener('change', onChange); else if (mql.addListener) mql.addListener(onChange); } // Wait for images/fonts in the iframe, then trigger print const waitForIframeImages = () => { const imgs = Array.from(doc.images || []); if (!imgs.length) return Promise.resolve(); return Promise.all(imgs.map(img => (img.complete && img.naturalWidth) ? Promise.resolve() : new Promise(res => { img.onload = img.onerror = res; }))); }; const doPrint = () => { try { childWin.focus(); childWin.print(); } catch (e) { console.warn('print() failed or blocked:', e); // If print cannot open, do not redirect immediately — ask user alert('Please allow printing popups and try again.'); try { document.body.removeChild(iframe); } catch {} if (loader) loader.style.display = "none"; } }; if (doc.readyState === 'complete') { waitForIframeImages().then(() => setTimeout(doPrint, 50)); } else { iframe.onload = () => { waitForIframeImages().then(() => setTimeout(doPrint, 50)); }; } } // ---- child -> parent message handler ---- window.addEventListener("message", function(event) { if (event.data && typeof event.data === "object" && event.data.type === "ghlFormSubmit") { currentFormData = event.data.data || {}; console.log("[Parent] 📥 Received GHL data:", currentFormData); const ghlForm = document.querySelector(".form-shortcode"); if (ghlForm) { ghlForm.style.display = "none"; } populateReferralSheet(currentFormData); const modal = document.getElementById("hip-pdf-modal"); if (modal) { modal.style.display = "flex"; setTimeout(() => modal.classList.add("active"), 10); } const closeBtn = document.getElementById("hip-close-modal"); if (closeBtn) closeBtn.onclick = () => { const modal = document.getElementById("hip-pdf-modal"); if (modal) { modal.classList.remove("active"); setTimeout(() => modal.style.display = "none", 400); } }; if (modal) { modal.addEventListener('click', (e) => { if (e.target === modal) { modal.classList.remove("active"); setTimeout(() => modal.style.display = "none", 400); } }); } const saveBtn = document.getElementById("hip-save-pdf-btn"); if (saveBtn) { saveBtn.onclick = async (e) => { e.preventDefault(); if (!currentFormData) { alert("No data to generate PDF."); return; } await (window.__uploadsReadyPromise || Promise.resolve()); await generatePDF(currentFormData); // this already redirects after save }; } const printBtn = document.getElementById("hip-print-btn"); if (printBtn) printBtn.onclick = (e) => { e.preventDefault(); printReferral(); }; } }); } else { console.log('⏳ Waiting for libs...'); setTimeout(waitForLibs, 500); } } waitForLibs();