(function() { // ====== CONFIG ====== const API_URL = "https://snugly.cloud/services/aiexpert/api/"; //const AGENT_ID = "AGENT_ID_HERE"; // Replace or inject dynamically const currentScript = document.currentScript; // Read the custom attribute const AGENT_ID = currentScript.getAttribute('data-agent-id'); // ====== SESSION HANDLING ====== let sessionId = localStorage.getItem("myai_expert_session"); if (!sessionId) { sessionId = (crypto.randomUUID && crypto.randomUUID()) || 'sess-' + Math.random().toString(36).substr(2, 9); localStorage.setItem("myai_expert_session", sessionId); } var currentcol = currentScript.getAttribute('data-color'); var currentpos = currentScript.getAttribute('data-pos'); if (!currentcol) currentcol="#4A90E2"; if (!currentpos) currentpos="br"; var poscode=''; if (currentpos=='br') { poscode='bottom: 20px; right: 20px;'; } if (currentpos=='bl') { poscode='bottom: 20px; left: 20px;'; } if (currentpos=='bm') { poscode='bottom: 20px; left: 50%;'; } // ====== STYLES ====== const style = document.createElement("style"); style.innerHTML = ` .myai-messages { background-color: #eee!important; color: black!important; } #myai-chat-button { position: fixed; ${poscode}; background: #${currentcol}; color: white; width: 60px; height: 60px; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; box-shadow: 0 4px 8px rgba(0,0,0,0.2); font-size: 28px; z-index: 999999; } #myai-chat-popup { position: fixed; bottom: 90px; right: 20px; width: 320px; height: 420px; background: white; border-radius: 10px; box-shadow: 0 4px 12px rgba(0,0,0,0.3); display: none; flex-direction: column; z-index: 999999; } #myai-chat-header { background: #${currentcol}; color: white; padding: 10px; border-top-left-radius: 10px; border-top-right-radius: 10px; font-weight: bold; font-size: 16px; } #myai-chat-description { font-size: 12px; padding: 6px 10px; border-bottom: 1px solid #eee; color: #555; } #myai-chat-messages { flex: 1; overflow-y: auto; padding: 10px; font-size: 14px; display: flex; flex-direction: column; position: relative; } .myai-msg { margin-bottom: 10px; padding: 8px; border-radius: 6px; max-width: 85%; word-break: break-word; color: black!important; } .myai-msg.user { background: #DCF8C6; align-self: flex-end; } .myai-msg.bot { background: #EEE; align-self: flex-start; } #myai-chat-input { display: flex; border-top: 1px solid #ddd; } #myai-chat-input input { flex: 1; border: none; padding: 8px; font-size: 14px; } #myai-chat-input button { background: #${currentcol}; border: none; color: white; padding: 0 12px; cursor: pointer; } `; document.head.appendChild(style); // ====== HTML ELEMENTS ====== const button = document.createElement("div"); button.id = "myai-chat-button"; button.innerHTML = '';//"💬"; const popup = document.createElement("div"); popup.id = "myai-chat-popup"; popup.innerHTML = `