diff --git a/tryModal.js b/tryModal.js index 1016156..8614bae 100644 --- a/tryModal.js +++ b/tryModal.js @@ -1,72 +1,79 @@ -const TRYNOW_WEBHOOK_URL = "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c25169c6-4234-4b47-8e74-612b9539da0a"; +const TRYNOW_WEBHOOK_URL = + "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c25169c6-4234-4b47-8e74-612b9539da0a"; // --- Helper: Get cookie value --- function getCookie(name) { - const value = `; ${document.cookie}`; - const parts = value.split(`; ${name}=`); - if (parts.length === 2) return parts.pop().split(';').shift(); - return null; + const value = `; ${document.cookie}`; + const parts = value.split(`; ${name}=`); + if (parts.length === 2) return parts.pop().split(";").shift(); + return null; } // --- Add UTM fields to form --- function addUtmFields(form) { - const utmParams = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"]; + const utmParams = [ + "utm_source", + "utm_medium", + "utm_campaign", + "utm_term", + "utm_content", + ]; - const defaults = { - utm_source: "homepage", - utm_medium: "direct", - utm_campaign: "none" - }; + const defaults = { + utm_source: "homepage", + utm_medium: "direct", + utm_campaign: "none", + }; - utmParams.forEach(param => { - let input = form.querySelector(`input[name="${param}"]`); - if (!input) { - input = document.createElement("input"); - input.type = "hidden"; - input.name = param; - form.appendChild(input); - } - const cookieValue = getCookie(param); - input.value = cookieValue || defaults[param] || ""; - }); + utmParams.forEach((param) => { + let input = form.querySelector(`input[name="${param}"]`); + if (!input) { + input = document.createElement("input"); + input.type = "hidden"; + input.name = param; + form.appendChild(input); + } + const cookieValue = getCookie(param); + input.value = cookieValue || defaults[param] || ""; + }); } // --- Create modal --- function tryNow_createModal() { - const modal = document.createElement("div"); - modal.id = "trynowModal"; - modal.style.position = "fixed"; - modal.style.top = "0"; - modal.style.left = "0"; - modal.style.width = "100%"; - modal.style.height = "100%"; - modal.style.backgroundColor = "rgba(0,0,0,0.6)"; - modal.style.display = "none"; - modal.style.justifyContent = "center"; - modal.style.alignItems = "center"; - modal.style.zIndex = "1000"; + const modal = document.createElement("div"); + modal.id = "trynowModal"; + modal.style.position = "fixed"; + modal.style.top = "0"; + modal.style.left = "0"; + modal.style.width = "100%"; + modal.style.height = "100%"; + modal.style.backgroundColor = "rgba(0,0,0,0.6)"; + modal.style.display = "none"; + modal.style.justifyContent = "center"; + modal.style.alignItems = "center"; + modal.style.zIndex = "1000"; - modal.innerHTML = ` + modal.innerHTML = `
× - +

Order Details

@@ -84,17 +91,16 @@ function tryNow_createModal() { @@ -126,87 +132,90 @@ function tryNow_createModal() {
`; - document.body.appendChild(modal); + document.body.appendChild(modal); - // Add UTM fields after form exists - const form = modal.querySelector("#trynowForm"); - addUtmFields(form); + // Add UTM fields after form exists + const form = modal.querySelector("#trynowForm"); + addUtmFields(form); - // Close modal handlers - document.getElementById("trynowCloseModal").onclick = () => { modal.style.display = "none"; }; - document.getElementById("trynowCloseConfirmation").onclick = () => { - document.getElementById("trynowConfirmation").style.display = "none"; - modal.style.display = "none"; - }; - modal.onclick = (e) => { if (e.target === modal) modal.style.display = "none"; }; + // Close modal handlers + document.getElementById("trynowCloseModal").onclick = () => { + modal.style.display = "none"; + }; + document.getElementById("trynowCloseConfirmation").onclick = () => { + document.getElementById("trynowConfirmation").style.display = "none"; + modal.style.display = "none"; + }; + modal.onclick = (e) => { + if (e.target === modal) modal.style.display = "none"; + }; - return modal; + return modal; } // --- Handle form submission --- function tryNow_handleFormSubmit() { - const form = document.getElementById("trynowForm"); - const confirmation = document.getElementById("trynowConfirmation"); + const form = document.getElementById("trynowForm"); + const confirmation = document.getElementById("trynowConfirmation"); - form.addEventListener("submit", async (e) => { - e.preventDefault(); + form.addEventListener("submit", async (e) => { + e.preventDefault(); - // Update UTM fields from cookies - addUtmFields(form); + // Update UTM fields from cookies + addUtmFields(form); - const data = {}; - new FormData(form).forEach((value, key) => (data[key] = value)); + const data = {}; + new FormData(form).forEach((value, key) => (data[key] = value)); - try { - const res = await fetch(TRYNOW_WEBHOOK_URL, { - method: "POST", - headers: { "Content-Type": "application/json" }, - body: JSON.stringify(data) - }); + try { + const res = await fetch(TRYNOW_WEBHOOK_URL, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(data), + }); - if (res.ok) { - form.style.display = "none"; - confirmation.style.display = "block"; - form.reset(); - } else { - alert("Failed to submit form."); - } - } catch (err) { - console.error(err); - alert("Error submitting form."); - } - }); + if (res.ok) { + form.style.display = "none"; + confirmation.style.display = "block"; + form.reset(); + } else { + alert("Failed to submit form."); + } + } catch (err) { + console.error(err); + alert("Error submitting form."); + } + }); } // --- Attach "Try Now" buttons --- function tryNow_attachButtons() { - const buttons = Array.from(document.querySelectorAll("a, button")); - buttons.forEach(btn => { - if (btn.textContent && btn.textContent.trim() === "Try Now") { - btn.addEventListener("click", (e) => { - e.preventDefault(); - const modal = document.getElementById("trynowModal"); - if (modal) { - modal.style.display = "flex"; + const buttons = Array.from(document.querySelectorAll("a, button")); + buttons.forEach((btn) => { + if (btn.textContent && btn.textContent.trim() === "Try Now") { + btn.addEventListener("click", (e) => { + e.preventDefault(); + const modal = document.getElementById("trynowModal"); + if (modal) { + modal.style.display = "flex"; - // Reset modal state - const form = document.getElementById("trynowForm"); - const confirmation = document.getElementById("trynowConfirmation"); - if (form && confirmation) { - form.style.display = "flex"; - confirmation.style.display = "none"; - addUtmFields(form); // refresh UTM fields each open - } - } - }); + // Reset modal state + const form = document.getElementById("trynowForm"); + const confirmation = document.getElementById("trynowConfirmation"); + if (form && confirmation) { + form.style.display = "flex"; + confirmation.style.display = "none"; + addUtmFields(form); // refresh UTM fields each open + } } - }); + }); + } + }); } // --- Initialize --- document.addEventListener("DOMContentLoaded", () => { - tryNow_createModal(); - tryNow_handleFormSubmit(); - tryNow_attachButtons(); + tryNow_createModal(); + tryNow_handleFormSubmit(); + tryNow_attachButtons(); }); - diff --git a/tryModal.min.js b/tryModal.min.js index 69d8a86..e210421 100644 --- a/tryModal.min.js +++ b/tryModal.min.js @@ -1 +1 @@ -const TRYNOW_WEBHOOK_URL="https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c25169c6-4234-4b47-8e74-612b9539da0a";function getCookie(n){const t=`; ${document.cookie}`.split(`; ${n}=`);return 2===t.length?t.pop().split(";").shift():null}function addUtmFields(n){const t={utm_source:"homepage",utm_medium:"direct",utm_campaign:"none"};["utm_source","utm_medium","utm_campaign","utm_term","utm_content"].forEach(o=>{let e=n.querySelector(`input[name="${o}"]`);e||(e=document.createElement("input"),e.type="hidden",e.name=o,n.appendChild(e));const i=getCookie(o);e.value=i||t[o]||""})}function tryNow_createModal(){const n=document.createElement("div");n.id="trynowModal",n.style.position="fixed",n.style.top="0",n.style.left="0",n.style.width="100%",n.style.height="100%",n.style.backgroundColor="rgba(0,0,0,0.6)",n.style.display="none",n.style.justifyContent="center",n.style.alignItems="center",n.style.zIndex="1000",n.innerHTML='\n
\n ×\n \n

Order Details

\n\n \n \n\n \n\n \n\n \n \n\n
\n

Thank you for your submission! 🎉

\n \n
\n
\n ',document.body.appendChild(n);return addUtmFields(n.querySelector("#trynowForm")),document.getElementById("trynowCloseModal").onclick=()=>{n.style.display="none"},document.getElementById("trynowCloseConfirmation").onclick=()=>{document.getElementById("trynowConfirmation").style.display="none",n.style.display="none"},n.onclick=t=>{t.target===n&&(n.style.display="none")},n}function tryNow_handleFormSubmit(){const n=document.getElementById("trynowForm"),t=document.getElementById("trynowConfirmation");n.addEventListener("submit",async o=>{o.preventDefault(),addUtmFields(n);const e={};new FormData(n).forEach((n,t)=>e[t]=n);try{(await fetch(TRYNOW_WEBHOOK_URL,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(e)})).ok?(n.style.display="none",t.style.display="block",n.reset()):alert("Failed to submit form.")}catch(n){console.error(n),alert("Error submitting form.")}})}function tryNow_attachButtons(){Array.from(document.querySelectorAll("a, button")).forEach(n=>{n.textContent&&"Try Now"===n.textContent.trim()&&n.addEventListener("click",n=>{n.preventDefault();const t=document.getElementById("trynowModal");if(t){t.style.display="flex";const n=document.getElementById("trynowForm"),o=document.getElementById("trynowConfirmation");n&&o&&(n.style.display="flex",o.style.display="none",addUtmFields(n))}})})}document.addEventListener("DOMContentLoaded",()=>{tryNow_createModal(),tryNow_handleFormSubmit(),tryNow_attachButtons()}); \ No newline at end of file +const TRYNOW_WEBHOOK_URL="https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c25169c6-4234-4b47-8e74-612b9539da0a";function getCookie(n){const t=`; ${document.cookie}`.split(`; ${n}=`);return 2===t.length?t.pop().split(";").shift():null}function addUtmFields(n){const t={utm_source:"homepage",utm_medium:"direct",utm_campaign:"none"};["utm_source","utm_medium","utm_campaign","utm_term","utm_content"].forEach(e=>{let o=n.querySelector(`input[name="${e}"]`);o||(o=document.createElement("input"),o.type="hidden",o.name=e,n.appendChild(o));const i=getCookie(e);o.value=i||t[e]||""})}function tryNow_createModal(){const n=document.createElement("div");n.id="trynowModal",n.style.position="fixed",n.style.top="0",n.style.left="0",n.style.width="100%",n.style.height="100%",n.style.backgroundColor="rgba(0,0,0,0.6)",n.style.display="none",n.style.justifyContent="center",n.style.alignItems="center",n.style.zIndex="1000",n.innerHTML='\n
\n ×\n\n

Order Details

\n\n
\n \n\n \n\n \n\n \n
\n\n
\n

Thank you for your submission! 🎉

\n \n
\n
\n ',document.body.appendChild(n);return addUtmFields(n.querySelector("#trynowForm")),document.getElementById("trynowCloseModal").onclick=()=>{n.style.display="none"},document.getElementById("trynowCloseConfirmation").onclick=()=>{document.getElementById("trynowConfirmation").style.display="none",n.style.display="none"},n.onclick=t=>{t.target===n&&(n.style.display="none")},n}function tryNow_handleFormSubmit(){const n=document.getElementById("trynowForm"),t=document.getElementById("trynowConfirmation");n.addEventListener("submit",async e=>{e.preventDefault(),addUtmFields(n);const o={};new FormData(n).forEach((n,t)=>o[t]=n);try{(await fetch(TRYNOW_WEBHOOK_URL,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(o)})).ok?(n.style.display="none",t.style.display="block",n.reset()):alert("Failed to submit form.")}catch(n){console.error(n),alert("Error submitting form.")}})}function tryNow_attachButtons(){Array.from(document.querySelectorAll("a, button")).forEach(n=>{n.textContent&&"Try Now"===n.textContent.trim()&&n.addEventListener("click",n=>{n.preventDefault();const t=document.getElementById("trynowModal");if(t){t.style.display="flex";const n=document.getElementById("trynowForm"),e=document.getElementById("trynowConfirmation");n&&e&&(n.style.display="flex",e.style.display="none",addUtmFields(n))}})})}document.addEventListener("DOMContentLoaded",()=>{tryNow_createModal(),tryNow_handleFormSubmit(),tryNow_attachButtons()}); \ No newline at end of file