;(() => { console.log("[Alma AutoFlow] script chargé sur:", location.href); const path = (location.pathname || "").toLowerCase(); const log = (...a) => console.log("[Alma AutoFlow]", ...a); const visible = el => el && getComputedStyle(el).display !== "none" && getComputedStyle(el).visibility !== "hidden" && el.offsetParent !== null; // ================= ETAPE 1 — DELIVERY (robuste redirection Alma) ================= if (/\/mag\/fr\/delivery\.php$/.test(location.pathname.toLowerCase())) { const log = (...a)=>console.log("[Alma AutoFlow][delivery]", ...a); const visible = el => el && getComputedStyle(el).display!=="none" && getComputedStyle(el).visibility!=="hidden" && el.offsetParent!==null; function runDelivery() { try { const pid = new URLSearchParams(location.search).get("pid"); if (!pid) { log("pas de pid → noop"); return; } if (sessionStorage.getItem("almaOrderCreated")==="1") { log("déjà déclenché → stop"); return; } // marquage flux sessionStorage.setItem("almaFlow","1"); sessionStorage.setItem("almaPid",pid); // boucle de recherche + envoi let tries = 0, max = 40; // ~12s const tick = () => { tries++; try { // radios SF possibles let radios = Array.from(document.querySelectorAll( 'input[type="radio"][name="ctx_delid"], input[type="radio"][name="ctx_delid[]"]' )).filter(visible); log(`try #${tries}, radios=`, radios.length); if (radios.length) { const r = radios.find(x=>x.checked) || radios[0]; if (!r.checked) { r.checked = true; r.dispatchEvent(new Event("change",{bubbles:true})); log("radio cochée value=", r.value); } else { log("radio déjà cochée value=", r.value); } // 3 stratégies d’envoi (dans cet ordre) const tryCall = (fn,arg)=>{ try { if (typeof window[fn]==="function") { log(`call ${fn}(${JSON.stringify(arg)})`); window[fn](arg); return true; } } catch(e){ console.error("[Alma AutoFlow] err", fn, e); } return false; }; if ( tryCall("sendFormDelivery","form") || tryCall("sendFormDelivery","formulaire") || tryCall("sendFormDelivery",undefined) ) { log("→ envoi via sendFormDelivery "); return; } const trigger = Array.from(document.querySelectorAll('[onclick*="sendFormDelivery("]')).find(visible); if (trigger) { try { trigger.scrollIntoView({behavior:"smooth",block:"center"});}catch(_){} log("→ click sur onclick=sendFormDelivery(...)"); trigger.click(); return; } const form = r.closest("form") || document.querySelector("#formulaire form") || document.querySelector("form[action*='validation']"); if (form) { const submitBtn = form.querySelector('button[type="submit"], input[type="submit"]'); if (submitBtn && visible(submitBtn)) { try { submitBtn.scrollIntoView({behavior:"smooth",block:"center"});}catch(_){} log("→ click submit du form"); submitBtn.click(); } else if (typeof form.submit==="function") { log("→ form.submit() (fallback)"); form.submit(); } return; } log("form/trigger introuvable, on retry…"); } if (tries < max) { setTimeout(tick, 300); } else { log("abandon après retries."); } } catch(e) { console.error("[Alma AutoFlow] delivery tick error:", e); } }; // Lancer tout de suite (utile en cas de redirection/restore) tick(); } catch(e) { console.error("[Alma AutoFlow] delivery run error:", e); } } // Déclencheurs multiples (cas redirection, BFCache, hydration SF, etc.) if (document.readyState === "complete" || document.readyState === "interactive") { runDelivery(); } else { document.addEventListener("DOMContentLoaded", runDelivery, {once:true}); } window.addEventListener("load", runDelivery, {once:true}); window.addEventListener("pageshow", (e)=>{ if (e.persisted) runDelivery(); }); // BFCache document.addEventListener("visibilitychange", ()=>{ if (document.visibilityState==="visible") runDelivery(); }); } // ================= ETAPE 2 — VALIDATION ================= if (/\/mag\/fr\/validation\.php$/.test(path)) { log("validation: actif"); const fromAlma = sessionStorage.getItem("almaFlow") === "1"; if (!fromAlma) { log("validation: pas un flux Alma → on ne fait rien"); return; } if (sessionStorage.getItem("almaOrderCreated") === "1") { log("validation: déjà déclenché → stop"); return; } window.addEventListener("load", () => { // 1) cocher CGV (retry) let t1 = 0, max1 = 20; const cgvTimer = setInterval(() => { t1++; try { const cgv = document.getElementById("ctx_accept_cgv"); if (cgv) { if (!cgv.checked) { cgv.checked = true; cgv.dispatchEvent(new Event("change", { bubbles: true })); log("validation: CGV cochée"); } else { log("validation: CGV déjà cochée"); } clearInterval(cgvTimer); // 2) cliquer le bouton virement (retry) let t2 = 0, max2 = 20; const clickTimer = setInterval(() => { t2++; try { const anchors = Array.from( document.querySelectorAll('a[onclick*="redirectbnk(\'transfer\'"]') ).filter(visible); log(`validation: tentative bouton virement #${t2}, candidats=`, anchors.length); if (anchors.length) { const target = anchors[anchors.length - 1]; try { target.scrollIntoView({ behavior: "smooth", block: "center" }); } catch(_) {} log("validation: clic sur redirectbnk('transfer')"); target.click(); sessionStorage.setItem("almaOrderCreated", "1"); clearInterval(clickTimer); return; } if (t2 === max2) { if (typeof clickRedirectBnk === "function") { log("validation: fallback → clickRedirectBnk()"); const ok = !!clickRedirectBnk(); log("validation: clickRedirectBnk() →", ok); if (ok) sessionStorage.setItem("almaOrderCreated", "1"); } else { log("validation: aucun bouton ni clickRedirectBnk dispo"); } clearInterval(clickTimer); } } catch (e) { console.error("[Alma AutoFlow] validation click error:", e); clearInterval(clickTimer); } }, 300); } else if (t1 >= max1) { log("validation: CGV introuvable après", t1, "essais"); clearInterval(cgvTimer); } } catch (e) { console.error("[Alma AutoFlow] validation CGV error:", e); clearInterval(cgvTimer); } }, 300); }, { once: true }); return; // ne pas exécuter la suite sur validation } log("page ignorée :", path); })();