(function () { //DDP var take = async function () { console.log("crich ddp loaded - 15Dec25-03"); // Wait for DOM to be fully loaded before removing href const removeHrefs = () => { const elements = document.querySelectorAll(".chatformBtn a"); elements.forEach((a) => { console.log("remove href!"); a.removeAttribute("href"); a.style.cursor = "pointer"; }); return elements.length > 0; }; // Function to wait for elements using MutationObserver const waitForElementsAndRemoveHrefs = () => { // Try immediately first if (removeHrefs()) { return; // Elements found and processed } // Set up MutationObserver to watch for new elements being added to the DOM // This will automatically detect when .chatformBtn a elements appear const observer = new MutationObserver(() => { if (removeHrefs()) { observer.disconnect(); // Stop observing once elements are found and processed } }); // Start observing the document for changes observer.observe(document.body || document.documentElement, { childList: true, // Watch for new child elements subtree: true, // Watch in all nested elements too }); }; const pathname = window.location.pathname; // Only run when pathname is "/lp" if (window.location.pathname === "/lp") { if (document.readyState === "loading") { document.addEventListener( "DOMContentLoaded", waitForElementsAndRemoveHrefs ); } else { waitForElementsAndRemoveHrefs(); } } const BASE_URL = "https://pr.aqur.com"; const fetchOptions = { method: "POST", headers: { "Content-Type": "application/json" }, }; const chatform = localStorage.getItem("chatform"); if (!chatform) return; let chatformData; try { chatformData = JSON.parse(chatform); } catch (e) { console.warn("Invalid chatform JSON", e); localStorage.removeItem("chatform"); // Clear invalid data return; } // Check if chatform data is valid using sessionStorage flag set by Send.html // Send.html sets this flag when chatform data exists, indicating chat was used in this session const SESSION_FLAG = `chatform_seen_in_session-${chatformData?.pid}`; // Check if chatform data is valid (not stale from previous session) const isChatformValid = () => { // Check if Send.html set the session flag (indicating chat was used in this session) const seenInSession = sessionStorage.getItem(SESSION_FLAG) === 'true'; if (seenInSession) { // Flag exists - Send.html confirmed chat was used in this session return true; } // No flag - data is likely stale from previous session // User didn't use chat in this session, or Send.html hasn't run yet return false; }; if (chatform) { if (chatformData.pid !== "crich") return; if(!isChatformValid()) { // Data is stale - user didn't use chat in this session console.log("Chatform data is stale - clearing cache"); localStorage.removeItem("chatform"); sessionStorage.removeItem(SESSION_FLAG); return; } else { // ────────── Auto-confirm click ────────── if (/\/lp\/confirm/.test(pathname)) { const confirmExec = () => { const confirmInterval = setInterval(() => { const submitBtn = document.querySelector("input#submit"); if (submitBtn) { clearInterval(confirmInterval); submitBtn.click(); } }, 300); }; confirmExec(); } // ────────── Completion tracking ────────── if (/\/lp\/complete/.test(pathname) || /\/lp\/cv_upsell/.test(pathname)) { const track = async (data) => { try { await fetch(`${BASE_URL}/track`, { ...fetchOptions, body: JSON.stringify(data), }); localStorage.removeItem("chatform"); const orderId = new URLSearchParams(window.location.search).get( "order_id" ); if (orderId) { const response = await fetch(`${BASE_URL}/pdi/${data.uuId}`); const result = await response.json(); result.data.data.order_id = orderId; result.data.Completed = true; result.data.status = "Completed"; await fetch(`${BASE_URL}/pdi/set`, { ...fetchOptions, body: JSON.stringify(result.data), }); console.log("Conversion tracked!"); } } catch (err) { console.error("Error in tracking:", err); } }; track(chatformData); } // ────────── Error check on form page ────────── if (/\/lp/.test(pathname) || /\/lp\/new/.test(pathname)) { await new Promise((resolve) => setTimeout(resolve, 5000)); const checkErrors = () => { const alertBox = document.querySelector("#alert-box"); return alertBox ? alertBox.textContent : ""; }; const error = checkErrors(); if (error) { // show form document.querySelector("#lp-form").parentElement.style.display = "block"; // scroll to error document .querySelector("#alert-box") .scrollIntoView({ behavior: "smooth", block: "start" }); try { const response = await fetch( `${BASE_URL}/pdi/${chatformData.uuId}` ); const result = await response.json(); result.data.Completed = false; if (/カード情報を確認してください/.test(error)) { result.data.status = "creditcardCheckNG"; } else if (/与信審査が通りませんでした/.test(error)) { result.data.status = "postpaymentCheckNG"; } else { result.data.message = error; } await fetch(`${BASE_URL}/pdi/set`, { ...fetchOptions, body: JSON.stringify(result.data), }); } catch (err) { console.error("Error sending error tracking data:", err); } } } // ────────── Error check on incomplete page ────────── if (/\/incomplete/.test(pathname)) { await new Promise((resolve) => setTimeout(resolve, 5000)); const checkIncompleteErrors = () => { let errorMessages = []; const titleEl = document.querySelector("h1.error__title"); const subtitleEl = document.querySelector("h2.error__subtitle"); const contentEl = document.querySelector("p.error__content"); if (titleEl) errorMessages.push(titleEl.innerText.trim()); if (subtitleEl) errorMessages.push( subtitleEl.innerText.replace(/\s+/g, " ").trim() ); if (contentEl) errorMessages.push(contentEl.innerText.replace(/\s+/g, " ").trim()); return errorMessages.length > 0 ? errorMessages.join(" | ") : "Retry Fail - Order submission incomplete - redirected to incomplete page"; }; const error = checkIncompleteErrors(); if (error) { try { const response = await fetch( `${BASE_URL}/pdi/${chatformData.uuId}` ); const result = await response.json(); result.data.Completed = false; result.data.status = "incomplete"; result.data.message = error; await fetch(`${BASE_URL}/pdi/set`, { ...fetchOptions, body: JSON.stringify(result.data), }); } catch (err) { console.error("Error sending incomplete page tracking data:", err); } } } } } }; setTimeout(take, 0); })();