const CHAT_WIDGET = { API_URL: "", AGENT_ID: "", AUTO_OPEN: true, DISPLAY_MODE: 1, USER_ID: "", USER_NAME: "", USER_AVATAR: "", isChatOpen: false, originalPageTitle: document.title, chatWindowTitle: "Chat with us", isOffline: false, iframeId: "chat-widget-iframe", containerId: "chat-widget-container" }; CHAT_WIDGET.initialize = function(config) { // Apply configuration for (let key in config) { if (this.hasOwnProperty(key)) { this[key] = config[key]; } } // Normalize URL by removing trailing slash if (this.API_URL) { this.API_URL = this.API_URL.replace(/\/$/, ""); } // Add required CSS styles this.injectStyles(); // Display the chat button this.createChatButton(); // Set up event handlers this.setupEventHandlers(); }; CHAT_WIDGET.injectStyles = function() { const style = document.createElement('style'); style.textContent = ` #chat-widget-button { position: fixed; bottom: 20px; right: 20px; width: 60px; height: 60px; background-color: #1E88E5; color: white; border-radius: 50%; display: flex; align-items: center; justify-content: center; cursor: pointer; box-shadow: 0 2px 10px rgba(0,0,0,0.2); z-index: 9999; } #chat-widget-button .notification-badge { position: absolute; top: -5px; right: -5px; background-color: #FF5722; color: white; border-radius: 50%; width: 20px; height: 20px; display: flex; align-items: center; justify-content: center; font-size: 12px; } #chat-widget-container { position: fixed; bottom: 90px; right: 20px; width: 350px; height: 500px; background: white; border-radius: 8px; box-shadow: 0 5px 20px rgba(0,0,0,0.2); display: none; flex-direction: column; z-index: 9998; overflow: hidden; } #chat-widget-header { padding: 15px; background: #1E88E5; color: white; display: flex; align-items: center; } #chat-widget-iframe { flex: 1; border: none; } .close-button { margin-left: auto; cursor: pointer; font-size: 20px; } @media (max-width: 800px) { #chat-widget-container { width: 100% !important; right: 0 !important; bottom: 80px !important; } } `; document.head.appendChild(style); }; CHAT_WIDGET.createChatButton = function() { const button = document.createElement('div'); button.id = 'chat-widget-button'; button.innerHTML = `
`; document.body.appendChild(button); button.addEventListener('click', () => { this.openChatWindow(); }); // Open automatically if configured if (this.AUTO_OPEN) { setTimeout(() => { this.openChatWindow(); }, 3000); } }; CHAT_WIDGET.openChatWindow = function() { if (this.isChatOpen) return; const badge = document.querySelector('#chat-widget-button .notification-badge'); badge.style.display = 'none'; badge.textContent = '0'; // Create container if it doesn't exist if (!document.getElementById(this.containerId)) { const container = document.createElement('div'); container.id = this.containerId; container.innerHTML = `