(function() { //DDP const BASE_URL = "https://pr.aqur.com"; const fetchOptions = { method: "POST", headers: { "Content-Type": "application/json" } }; const chatform = localStorage.getItem("chatform"); var take = async function() { function getUuIdFromAnchor() { const queryString = window.location.search; const params = new URLSearchParams(queryString); return params.get("uuId"); } if(/\/support\/credit1\/entry\//.test(location.pathname) || /\/payment\/entry1_test.php/.test(location.pathname)) { let uuId = getUuIdFromAnchor(); if (uuId) { const fetchUrl = `${BASE_URL}/pdi/${uuId}`; fetch(fetchUrl) .then(response => response.json()) .then(async data => { console.log(data) let d = data.data let chatform = { pid: d.pid, sid: d.sid, uuId: d.uuId, customId:"conversion" } if(d.pid === 'lfa2') { localStorage.setItem('chatform', JSON.stringify(chatform)); bindForm(d.data, chatform) } }) .catch(error => { console.error('Error fetching the URL:', error); }); } } } document.addEventListener("DOMContentLoaded", () => { setTimeout(take, 0); }) const fillInput = (selector, value) => { return new Promise((resolve, reject) => { let inputEvent = new InputEvent('input', { bubbles: true, cancelable: false }); let inputEvent2 = new InputEvent('change', { bubbles: true, cancelable: false }); const inputElement = document.querySelector(selector); if (inputElement) { inputElement.value = value; inputElement.dispatchEvent(inputEvent); inputElement.dispatchEvent(inputEvent2); resolve(`Input with name "${selector}" filled and event dispatched`); } else { reject(`Element with name "${selector}" not found.`); } }); }; const chooseSelectBox = (selectBoxSelector, value, options = { useOptionVal: true, useInnerText: false }) => { return new Promise((resolve, reject) => { let option = ""; if (options.useInnerText) { option = Array.from(document.querySelectorAll(`${selectBoxSelector} option`)).find(opt => opt.innerText.trim() === value.trim()); } else if (options.useOptionVal) { option = document.querySelector(`${selectBoxSelector} option[value='${value}']`); } else { option = document.querySelector(`${selectBoxSelector} option[label='${value}']`); } const selectElement = document.querySelector(`${selectBoxSelector}`); if (option && selectElement) { option.selected = true; const event = new Event('change', { bubbles: true, composed: true }); selectElement.dispatchEvent(event); resolve(`Select box with name '${selectBoxSelector}' set to option with ${options.useInnerText ? 'text' : 'value'} '${value}' and event dispatched.`); } else { reject(new Error(`Select box with name '${selectBoxSelector}' and option with ${options.useInnerText ? 'text' : 'value'} '${value}' not found.`)); } }); }; async function track(trackingData) { try { await fetch(`${BASE_URL}/track`, { ...fetchOptions, body: trackingData }); localStorage.removeItem("chatform"); let chatformData = JSON.parse(trackingData); const response2 = await fetch(`${BASE_URL}/pdi/${chatformData.uuId}`); const data2 = await response2.json(); data2.data.Completed = true; data2.data.status = "Completed"; await fetch(`${BASE_URL}/pdi/set`, { ...fetchOptions, body: JSON.stringify(data2.data) }); } catch (error) { console.error('Error:', error); } } async function bindForm(data, chatform) { console.log("bindForm", data) 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 && data.Day) { await chooseSelectBox('[name="year"]', data.Year) await chooseSelectBox('[name="month"]', Number(data.Month)) await chooseSelectBox('[name="day"]', Number(data.Day)) } // Scroll to selector credit form const input = document.querySelector('[name="cn"]'); input.closest('div').scrollIntoView({ behavior: 'smooth' }); setTimeout(() => { input.focus(); }, 300); // Adjust time if needed track(JSON.stringify(chatform)) } catch (error) { console.log(error) } } }())