(function() { window.AqurBaseURL = 'https://pr.aqur.com'; var take = function() { //vframe start var vframe; window.addEventListener("chatform_initialized", (event) => { createVframe() //for debug const errElm = document.querySelector("#submit_error_message"); if (errElm && errElm.innerText.trim() !== "" || localStorage.getItem("has_redirect")) { localStorage.removeItem("has_redirect") if (hasChatformLaunchAuto()) { activateChatformWidget(); } } const as = document.querySelectorAll('.chatformBtn'); as.forEach((a, i) => { const anchor = a.closest("a"); if (anchor) { console.log("remove href!"); if (anchor.hasAttribute("target")) { anchor.removeAttribute("target"); } anchor.classList.add("checkChk_" + i); if (anchor.hasAttribute("href")) { anchor.setAttribute("data-link", anchor.getAttribute("href")); anchor.removeAttribute("href"); } } }); }) function hasChatformLaunchAuto() { return Array.from(document.querySelectorAll('script[src]')).some(script => { try { const scriptURL = new URL(script.src); return ( scriptURL.pathname.includes('/chatform') && scriptURL.searchParams.get('pid') === 'ktrb' && scriptURL.searchParams.get('launch') === 'auto' ); } catch (e) { return false; } }); } function createVframe(){ var url = new URL(window.location.href); let userChatbot = url.searchParams.get('usechatbot') if(userChatbot){ return } // Check if the iframe already exists var existingVframe = document.getElementById("vframe"); // If the iframe already exists, remove it if(existingVframe) { existingVframe.parentNode.removeChild(existingVframe); } vframe = document.createElement("iframe"); vframe.id="vframe"; vframe.sandbox.add('allow-storage-access-by-user-activation'); vframe.sandbox.add('allow-forms'); vframe.sandbox.add('allow-top-navigation'); vframe.sandbox.add('allow-scripts'); vframe.sandbox.add('allow-same-origin'); vframe.width="1px"; vframe.height="1px"; let linkElement = null; let REF = null; const path = window.location.pathname; // Try to find the image element by alt attribute const targetElement = document.querySelector('[alt="税制優遇も 月1,000円〜寄付する"]'); // If the element exists, get the closest anchor tag if (targetElement) { linkElement = targetElement.closest("a"); } if (linkElement) { const attributeHref = linkElement.getAttribute("href"); const dataLink = linkElement.getAttribute("data-link"); if (attributeHref) { REF = new URL(attributeHref, window.location.origin); } else if (dataLink) { REF = new URL(dataLink), window.location.origin; } } else if (path.includes("/monthly/")) { REF = new URL(window.location.href); } else { const isStaging = window.location.href.includes("staging.katariba.or.jp"); const hasQueryParams = window.location.search const baseURL = (isStaging ? "https://staging.katariba.or.jp/spirit/monthly/index.php" : "https://www.katariba.or.jp/spirit/monthly/index.php"); const fullURL = (hasQueryParams !="" ? `${baseURL}?${hasQueryParams}`: baseURL); console.log("hasQueryParams",hasQueryParams) console.log("fullURL",fullURL) REF = new URL(fullURL); } REF.searchParams.append('usechatbot', "no"); vframe.src = REF; document.body.appendChild(vframe); let vfmode = url.searchParams.get('vfmode') vframe.width="70%"; vframe.height="650px" vframe.style.display= "block"; vframe.style.zIndex= 100; vframe.style.position= "fixed"; vframe.style.backgroundColor= "white"; vframe.style.top=0; vframe.style.left=0; vframe.style.boxSizing="border-box"; vframe.style.border="2px solid red"; vframe.style.marginTop = "20px"; vframe.style.opacity=0; vframe.style.zIndex = -1000; //for debug mode if(vfmode=="debug"){ vframe.style.opacity=1; vframe.style.zIndex=1000; vframe.scrollIntoView({ behavior: "smooth", block: "start", inline: "nearest" }); //vframe load event vframe.addEventListener("load", (e) => { //console.log("event vframe =>", e.target.src) // var insertDDP =()=>{ // var script = vframe.contentWindow.document.createElement('script'); // script.src = ${window.AqurBaseURL}/ktrb/client/ddp; // console.log("ddp inserted") // vframe.contentWindow.document.head.appendChild(script); // } // var url = new URL(vframe.contentWindow.location.href) // console.log(url.href) // switch (url.origin) { // default: // insertDDP() // } }) //debug postMessage var param={ event: "chatform_vframe_debug" } vframe.contentWindow.postMessage(param, "*"); } } //vframe end function render(template, data) { return template.replace(/{{(.*?)}}/g, (match) => { return data[match.split(/{{|}}/).filter(Boolean)[0].trim()] }) } function activateChatformWidget() { if (!chatform_initialized) { console.log("Aqur is not ready") return } const w = document.querySelector('.chatform_widget'); w.classList.toggle("close"); if (w.classList.contains("close")) { document.body.classList.remove("chat_open"); } else { document.body.classList.add("chat_open"); } } var chatform_initialized = false const pid = "ktrb"; const target = "production"; const mode = "release"; const device = JSON.parse("false".toLowerCase()) ? "mobile" : "pc"; var launch = "click"; const client = "https://form.aqur.com/"; let REF = new URL(window.location); let userChatbot = REF.searchParams.get('usechatbot') if (userChatbot == "no") { console.log("no chatbot") return } REF.searchParams.append('pid', pid); REF.searchParams.append('mode', mode); REF.searchParams.append('target', target); REF.searchParams.append('launch', launch); REF.searchParams.append('hash', "a3c38128-13fc-4489-a4af-1faf9599b085"); REF.searchParams.append('browser', "undefined"); REF.searchParams.append('device', device); REF.searchParams.append('os', "undefined"); REF.searchParams.append('ua', "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)"); REF.searchParams.append('ip', "3.147.83.1"); REF.searchParams.append('ref', REF.href); REF.searchParams.append('path', REF.pathname); //for UI REF.searchParams.append('CLIENT_ID', 'PR'); REF.searchParams.append('BASE_URL', 'https://pr.aqur.com'); REF.searchParams.append('HEADER_LABEL', ''); REF.searchParams.append('HEADER_BG_COLOR', '#fff'); REF.searchParams.append('HEADER_LABEL_COLOR', '#fff'); REF.searchParams.append('SHOW_TIMER', 'false'); REF.searchParams.append('ICON_PATH', 'https://res.cloudinary.com/dts6mbw86/image/upload/v1713319985/aqur/common/icon_azvikf.png'); const params = new URLSearchParams(REF.search.slice(1)); let search = REF.search.slice(1) //console.log(search) launch = params.get('launch'); let widget = render( '' + '', { device: device, client: client, search: (search) ? "&" + search : "", } ); const body = document.body; if (body) { var filterid; const chatbotProgressBar = "
" body.insertAdjacentHTML('beforeend', chatbotProgressBar); body.classList.add(device) body.insertAdjacentHTML('beforeend', widget); const target = document.querySelector('.chatform_widget'); const observer = new MutationObserver((mutations) => { mutations.forEach((mutation) => { if (!target.classList.contains("close")) { var contentWindow = target.contentWindow; if (contentWindow) { contentWindow.postMessage("chat_open", "*"); contentWindow.postMessage( { type: "filterIdChanged", data: filterid }, "*"); } } }); }); const config = { characterData: true, attributes: true, subtree: true }; observer.observe(target, config); } let creationComplete = function () { if (device == "pc") { var resize = () => { let zoom_level = window.devicePixelRatio || window.screen.availWidth / document.documentElement.clientWidth * 100; const targets = document.querySelectorAll('body,.chatform_widget'); if (zoom_level >= 3) { targets[0].classList.add("mobile") targets[0].classList.remove("pc") targets[1].classList.add("mobile") targets[1].classList.remove("pc") } else { targets[0].classList.add("pc") targets[0].classList.remove("mobile") targets[1].classList.add("pc") targets[1].classList.remove("mobile") } } resize() window.addEventListener("resize", () => { resize() }) } document.addEventListener('click', (event) => { if (event.target && event.target.closest('.chatformBtn') || event.target.classList.contains("chatformBtn") || event.target.closest('.bot_open') || event.target.classList.contains("bot_open") || event.target.id == "chatform_widget_cancelbtn" ) { activateChatformWidget() } }); document.dispatchEvent(new CustomEvent("chatform_widget_creation_complete")); }; setTimeout(creationComplete, 250); window.addEventListener("message", (event) => { //console.log("event.data.event", event) if(event.data.event == "redirect_thankPage"){ //console.log("redirect_chatform", event.data.event) const target = document.querySelector('.chatform_widget'); var param={ event: "chatform_clear" } target.contentWindow.postMessage(param, "*"); let redirect = function(){ window.location.href= event.data.url } setTimeout(redirect, 500); } /* Default Actions*/ if(event.data.event == "chatform_start"){ console.log(event.data.uuId) } if(event.data.event == "chatform_close"){ activateChatformWidget(); } if(event.data.event == "chatform_evalScript"){ window.eval(event.data.script) } if(event.data.event == "chatform_insertTag"){ body.insertAdjacentHTML( "beforeend", event.data.tag ); event.data.scripts.forEach((item)=>{ eval(item.textContent)}) } if(event.data.event == "chatform_initialized"){ //console.log("Aqur Initialized") document.getElementById("chatbotProgressBar").style.width = "100%"; var opacity = function(){ document.getElementById("chatbotProgressBar").style.opacity = "0"; }; setTimeout(opacity, 1000); chatform_initialized = true; if(launch=="auto"){ activateChatformWidget(); } window.dispatchEvent(new CustomEvent("chatform_initialized")); } if(event.data.event == "chatform_vframe_bind"){ //console.log("chatform_vframe_bind",event.data.project) var project = event.data.project; var param={ event: "chatform_bindForm", data:project } vframe.contentWindow.postMessage(param, "*"); } if(event.data.event == "chatform_vframe_submit"){ //console.log("chatform_vframe_submit", event.data.project.data) var data = event.data.project.data; var param={ event: "chatform_bindFormSubmit", data : data } vframe.contentWindow.postMessage(param, "*"); } if(event.data.event == "chatform_bindFormErrorFound"){ //console.log("chatform_bindFormErrorFound") const target = document.querySelector('.chatform_widget'); var param={ event: "chatform_bindFormErrorFound", text:event.data.text } target.contentWindow.postMessage(param, "*"); } if(event.data.event == "chatform_bindClick"){ //console.log("chatform_bindClick",event.data) document.getElementById(event.data.elementId).click(); } if(event.data.event == "chatform_bindCheck"){ //console.log("chatform_bindCheck",event.data) document.getElementById(event.data.elementId).click(); } },false) } setTimeout(take, 0); }())