// Copyright 2023 C&G Soluciones IT Argentina. Todos los derechos reservados // // File: config.js // // Description: File for webchat integration using an inline iframe // // Initialize the webchat service /* Initializes the web chat service URL and available "product" choices. // Each choice is mapped to a router or starter configured in BCT server. // It is possible to map multiple choices to the same router or starter. // // This is an example initialization that is done when the page is loaded; // in a production environment, you may want to do re-initialization on the fly (e.g. change the language), // based on how your web site is intended to work. // // Note: It is also possible to give no choices and send all chats to the same router or starter. // To do that, specify routerName=[name of BCT router] or starterName=[name of BCT starter] in the URL query string. // See also index-modal.html where this approach is used. */ var timer; var iframeId = "webchat.iframe"; var iframeDomain = "https://webchat01-sommiercentercom.cyg.ar"; var bctServerUrl = "https://webchat01-sommiercentercom.cyg.ar"; var webchatUrl = bctServerUrl + "/index.html#"; // SECTOR ZENDESK //window.addEventListener("load", function() { // CreateIframe(); //}); // // window.addEventListener("load", function() { // // Obtén todos los elementos con la clase 'zopim' // var elementosZopim = document.querySelectorAll("div.zopim"); // // Recorre y elimina cada elemento // elementosZopim.forEach(function(elemento) { // elemento.parentNode.removeChild(elemento); // }); // }); ///////////////////////////////////////// window.addEventListener("load", function() { CreateIframe(); initWebchatService(); }); window.addEventListener("load", (event) => { startTimer(); //console.log("DOM fully loaded and parsed"); }); window.addEventListener("load", function() { // Ejecuta la función cuando se carga la página applyMobileStyle(); // Opcional: Vuelve a aplicar la lógica si cambia el tamaño de la ventana window.addEventListener('resize', applyMobileStyle); }); function applyMobileStyle() { const webchatElement = document.querySelector('.webchat-position'); const addToCartButton = document.getElementById('product_addtocart_form'); const isMobile = window.matchMedia("(max-width: 768px)").matches; if (addToCartButton && isMobile) { webchatElement.style.marginBottom = '6rem'; } else { webchatElement.style.marginBottom = ''; // Restablece el estilo original } } function CreateIframe(){ // Encuentra el div con id="chats" var chatsDiv = document.getElementById("chats"); // // Crea los elementos necesarios var embeddedDiv = document.createElement("div"); embeddedDiv.id = "embedded"; embeddedDiv.className = "webchat-position"; var iframe = document.createElement("iframe"); iframe.id = "webchat.iframe"; //iframe.style.display = "none"; iframe.scrolling = "no"; iframe.frameBorder = "0"; iframe.allowTransparency = "true"; iframe.height = "40px"; iframe.width = "300px"; // Anexa los elementos embeddedDiv.appendChild(iframe); chatsDiv.appendChild(embeddedDiv); } function GetCurrentProduct() { var currentProduct; try { // currentProduct = window.location.href.substring( // window.location.href.lastIndexOf("/") + 1); currentProduct = document.title; } catch (e) { console.log(e); } if (currentProduct == undefined) currentProduct = "NINGUN PRODUCTO"; return currentProduct; } function startTimer() { timer = setInterval(function () { var currentProduct = GetCurrentProduct(); postToWebChatIframe("save", "currentProduct", currentProduct); //console.log("Timer executed, product stored in localStorage"); }, 2000); } function toggleBusinessConneCTChat(){ const iframe = document.getElementById('webchat.iframe'); if (iframe.style.display === 'none') { iframe.style.display = 'block'; } else { iframe.style.display = 'none'; } } function postToWebChatIframe(action, key, value) { const iframe = document.getElementById(iframeId); iframe.contentWindow.postMessage({ action: action, key: key, value: value, },iframeDomain); } function Dispose() { clearInterval(timer); } function initWebchatService() { var webchatService = document.getElementById("webchat.iframe"); // var queryString = Object.keys(chatQueryParams).map(function(key) { // return key + '=' + chatQueryParams[key]; // }).join('&'); // Attach postmessage listener, see function receivePostMessage if (typeof window.addEventListener !== 'undefined') { window.addEventListener('message', receivePostMessage, false); } else { window.attachEvent('onmessage', receivePostMessage); } // Link the source of the iframe to the webchat application webchatService.src = webchatUrl; //+ '?' + queryString; } /* // The webchat client sends a postmessage with 'command' data 'sendsize' to resize the iframe so it will fit. // // The webchat client sends a postmessage with 'command' data 'cobrowsestatus', 'cobrowsestart' or 'cobrowsestop' to request co-browse actions. // For co-browse requests (commands) a (result) message is posted back to child (iframe) window // // For information about javascript window.postmessage, see e.g. Mozilla documentation: https://developer.mozilla.org/en-US/docs/DOM/window.postMessage */ function resizeElement(element, event) { if (typeof event.data.height !== 'undefined' && typeof event.data.width !== 'undefined') { var regExp = new RegExp(/^\d{1,3}$/); if (regExp.test(event.data.height.replace('px','')) && regExp.test(event.data.width.replace('px',''))) { element.style.height = event.data.height ; element.style.width = event.data.width ; } } } function receivePostMessage(event) { console.log(event); // Check if the message comes from the webchat application. if (event.origin.toLowerCase() == bctServerUrl.toLowerCase()) { // Command received in postMessage var command = event.data.data.command; switch (command) { case 'sendsize' : // Get reference to iframe window, validate the received data and update size resizeElement(document.getElementById("embedded"),event.data); resizeElement(document.getElementById("webchat.iframe"),event.data); break; case 'cobrowsestatus' : var status = (typeof CV !== 'undefined') ? 'enabled' : 'disabled'; event.source.postMessage({ 'command': 'cobrowsestatus', 'status': status}, event.origin); break; case 'cobrowsestart' : if (typeof CV !== 'undefined') { if (CV.cobrowse.sessionIsActive()) { // Co-browse session is already active so re-inform webchat client by passing the share ID var shareId = CV.liveview.shareId(); event.source.postMessage({ 'command': 'cobrowsestart', 'shareId': shareId }, event.origin); } else { // Co-browse session not yet active so start it. When session is started the add co-browse // started listener is called back. Listener will inform webchat client with share ID. CV.cobrowse.start(); } } else { event.source.postMessage({ 'command': 'cobrowsestart', 'shareId': 'undefined' }, event.origin); } break; case 'cobrowsestop' : if (typeof CV !== 'undefined') { if (CV.cobrowse.sessionIsActive()) { CV.cobrowse.stop(); } } break; case 'toggleChatWindow' : try{ if(checkIfIframeIsHidden()) toggleBusinessConneCTChat(); } catch(e) { console.log(e); } break; } } }