(function () { const BASE_URL_LFA2 = "https://pr.aqur.com"; const fetchOptions = { method: "POST", headers: { "Content-Type": "application/json" } }; console.log("lfa2 ddp!"); // Polyfill for Element.matches if (!Element.prototype.matches) { Element.prototype.matches = Element.prototype.msMatchesSelector || Element.prototype.webkitMatchesSelector; } // fetch fallback using XMLHttpRequest const fetchWithFallback = (url, options = {}) => { if (window.fetch) { return fetch(url, options); } else { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest(); const method = options.method || "GET"; xhr.open(method, url, true); if (options.headers) { Object.entries(options.headers).forEach(([key, value]) => { xhr.setRequestHeader(key, value); }); } xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status >= 200 && xhr.status < 300) { resolve({ ok: true, status: xhr.status, json: () => Promise.resolve(JSON.parse(xhr.responseText)), text: () => Promise.resolve(xhr.responseText) }); } else { reject(new Error(`XHR failed: ${xhr.status}`)); } } }; xhr.onerror = () => reject(new Error("Network error")); xhr.send(options.body || null); }); } }; const getUuIdFromAnchor = () => { const queryString = window.location.search; const params = new URLSearchParams(queryString); return params.get("uuId"); }; const safeInputEvent = (type) => { try { return new InputEvent(type, { bubbles: true }); } catch { const evt = document.createEvent("Event"); evt.initEvent(type, true, true); return evt; } }; const safeClosest = (el, selector) => { while (el && el.nodeType === 1) { if (el.matches(selector)) return el; el = el.parentElement || el.parentNode; } return null; }; async function take() { if ( /\/support\/credit1\/entry\//.test(location.pathname) || /\/payment\/entry1_test.php/.test(location.pathname) || /\/payment\/entry1.php/.test(location.pathname) ) { let uuId = getUuIdFromAnchor(); if (uuId) { try { const fetchUrl = `${BASE_URL_LFA2}/pdi/${uuId}`; const res = await fetchWithFallback(fetchUrl); const data = await res.json(); const d = data.data; const chatform_lfa2 = { pid: d.pid, sid: d.sid, uuId: d.uuId, customId: "conversion" }; if (d.pid === 'lfa2') { await bindForm(d.data, chatform_lfa2); } } catch (e) { console.error('Error fetching form data:', e); } } } } async function track(trackingData) { try { const completeTarget = JSON.parse(trackingData); await fetchWithFallback(`${BASE_URL_LFA2}/pdi/complete`, { ...fetchOptions, body: JSON.stringify({ pid: completeTarget.pid, sid: completeTarget.sid, uuId: completeTarget.uuId, id: "conversion" }) }); localStorage.removeItem("chatform_lfa2"); const response2 = await fetchWithFallback(`${BASE_URL_LFA2}/pdi/${completeTarget.uuId}`); const data2 = await response2.json(); if (data2.data) { console.log("Conversion success!"); data2.data.Completed = true; data2.data.status = "Completed"; await fetchWithFallback(`${BASE_URL_LFA2}/pdi/set`, { ...fetchOptions, body: JSON.stringify(data2.data) }); } } catch (error) { console.error("Error during thank-you page processing:", error); } } document.addEventListener("DOMContentLoaded", () => { const href_lfa2 = window.location.href; if (href_lfa2.includes("learningforall") && href_lfa2.includes("thanks")) { const uuId = getUuIdFromAnchor(); if (uuId) { const fetchUrl = `${BASE_URL_LFA2}/pdi/${uuId}`; fetchWithFallback(fetchUrl) .then(res => res.json()) .then(data => { const d = data.data; const chatform_lfa2 = { pid: d.pid, sid: d.sid, uuId: d.uuId, customId: "conversion" }; if (d.pid === 'lfa2') { console.log("tracking lfa2 ST"); track(JSON.stringify(chatform_lfa2)); } }) .catch(err => console.error('Error fetching for tracking:', err)); } } else if ( href_lfa2.includes("learningforall") && href_lfa2.includes("entry") && !href_lfa2.includes("thanks") ) { console.log("at form chatform_lfa2"); setTimeout(take, 0); } }); const fillInput = (selector, value) => { return new Promise((resolve, reject) => { const inputElement = document.querySelector(selector); if (inputElement) { inputElement.value = value; setTimeout(() => { inputElement.dispatchEvent(safeInputEvent('input')); inputElement.dispatchEvent(safeInputEvent('change')); resolve(`Filled: ${selector}`); }, 0); } else { reject(`Element not found: ${selector}`); } }); }; const chooseSelectBox = (selectBoxSelector, value, options = { useOptionVal: true, useInnerText: false }) => { return new Promise((resolve, reject) => { const selectElement = document.querySelector(selectBoxSelector); if (!selectElement) return reject(`Select box not found: ${selectBoxSelector}`); let option; if (options.useInnerText) { option = Array.from(selectElement.options).find(opt => opt.innerText.trim() === value.trim()); } else if (options.useOptionVal) { option = selectElement.querySelector(`option[value='${value}']`); } if (option) { option.selected = true; selectElement.dispatchEvent(new Event('change', { bubbles: true })); resolve(`Selected ${value} in ${selectBoxSelector}`); } else { reject(`Option not found: ${value} in ${selectBoxSelector}`); } }); }; async function bindForm(data, chatform_lfa2) { try { if (data.Optin === "その他") { await chooseSelectBox('[name="iid"]', data.Optin2, { useInnerText: true }); } else { await chooseSelectBox('[name="iid"]', data.Optin, { useInnerText: true }); } await fillInput('[name="name01"]', data.LastName); await fillInput('[name="name02"]', data.FirstName); await fillInput('[name="ruby01"]', data.LastNameKana); await fillInput('[name="ruby02"]', data.FirstNameKana); await fillInput('[name="em"]', data.Email); await fillInput('[name="em2"]', data.Email); await fillInput('[name="zip01"]', data.Zip.replace("-", "")); await chooseSelectBox('select[name="pref01"]', data.Prefecture, { useInnerText: true }); await fillInput('input[name="addr01"]', data.City); await fillInput('input[name="addr02"]', data.Street1 + "" + data.Street2); await fillInput('input[name="pn"]', data.Tel.replace(/-/g, "")); if (data.Year && data.Month) { setTimeout(async () => { await chooseSelectBox('[name="year"]', Number(data.Year)); await chooseSelectBox('[name="month"]', Number(data.Month)); if(data.Day){ await chooseSelectBox('[name="day"]', Number(data.Day)); }else{ await chooseSelectBox('[name="day"]', 1); } }, 500); } const input = document.querySelector('#cn'); if (input) { ScrollFormtoSubmit('#cn') input.focus(); } } catch (error) { console.error("Error in bindForm:", error); } } })(); function ScrollFormtoSubmit(elm){ var viewForm = document.querySelector(elm); if (/Android/i.test(navigator.userAgent)) { //alert("work") window.location.hash = elm var viewFormPosition = viewForm.getBoundingClientRect().top + window.pageYOffset - 100; // Adjust for header window.scrollTo({ top: viewFormPosition, behavior: 'smooth' }); }else{ requestAnimationFrame(function() { viewForm.scrollIntoView({ behavior: 'smooth', block: 'start' }); }); } }