PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.3.3
MxChat – AI Chatbot & Content Generation for WordPress v2.3.3
3.2.21 3.2.20 3.2.19 3.2.18 3.2.17 3.2.16 3.2.15 3.2.14 3.2.12 3.2.13 3.2.11 3.2.10 3.2.9 3.2.8 3.2.7 3.2.6 3.2.5 3.2.4 3.2.3 3.2.2 3.2.1 2.0.3 2.0.4 2.0.5 2.0.6 All 152 releases
← All changes | js/chat-script.js +1187 -3618 3.2.212.3.3 View file →
@@ -1,394 +1,12 @@
1 1 jQuery(document).ready(function($) {
2 -
3 - // Nonce refresh — v2 (plan-6a68c9).
4 - //
5 - // The widget no longer relies on a nonce embedded in inline cached HTML.
6 - // Before each chat-send / stream-send / upload, we call the REST endpoint
7 - // GET /wp-json/mxchat/v1/nonce and use the freshly-issued value. The
8 - // endpoint creates the nonce with action `mxchat_chat_send`; the server-side
9 - // verifier ALSO still accepts the legacy `mxchat_chat_nonce` action for a
10 - // 30-day backwards-compat window so cached pages still in users' browsers
11 - // (which carry the legacy inline-localized nonce) keep working.
12 - //
13 - // Cache: a single module-scoped slot. TTL 12h conservatively (WP nonces are
14 - // 24h but we refetch at half-life so a freshly-cached-page user never sees
15 - // a borderline-stale nonce).
16 - var cachedFreshNonce = null;
17 - var cachedFreshNonceFetchedAt = 0;
18 - var NONCE_TTL_MS = 12 * 60 * 60 * 1000;
19 - var nonceRefreshState = 'idle'; // 'idle' | 'pending' | 'done'
20 - var nonceRefreshCallbacks = [];
21 -
22 - function getRestNonceUrl() {
23 - if (typeof mxchatChat !== 'undefined' && mxchatChat.rest_url) {
24 - return mxchatChat.rest_url.replace(/\/+$/, '') + '/nonce';
25 - }
26 - // Fallback: derive from current origin if mxchatChat.rest_url isn't set.
27 - return window.location.origin + '/wp-json/mxchat/v1/nonce';
28 - }
29 -
30 - function fetchFreshNonceFromRest() {
31 - return fetch(getRestNonceUrl(), {
32 - credentials: 'same-origin',
33 - headers: { 'Accept': 'application/json' }
34 - }).then(function (resp) {
35 - if (!resp.ok) {
36 - throw new Error('REST nonce fetch failed: ' + resp.status);
37 - }
38 - return resp.json();
39 - }).then(function (data) {
40 - if (data && data.nonce) {
41 - return data.nonce;
42 - }
43 - throw new Error('REST nonce response had no nonce field.');
44 - });
45 - }
46 -
47 - /**
48 - * withFreshNonce(cb) — invoke cb() after ensuring mxchatChat.nonce is fresh.
49 - * Tries REST endpoint first (cache-bypass design); falls back to the legacy
50 - * admin-ajax refresh path if REST is unavailable. Idempotent — concurrent
51 - * calls share the same in-flight refresh.
52 - */
53 - function withFreshNonce(callback) {
54 - if (typeof mxchatChat === 'undefined') {
55 - if (callback) callback();
56 - return;
57 - }
58 - var now = Date.now();
59 - if (cachedFreshNonce && (now - cachedFreshNonceFetchedAt) < NONCE_TTL_MS) {
60 - mxchatChat.nonce = cachedFreshNonce;
61 - if (callback) callback();
62 - return;
63 - }
64 - if (callback) nonceRefreshCallbacks.push(callback);
65 - if (nonceRefreshState === 'pending') return;
66 - nonceRefreshState = 'pending';
67 -
68 - var resolved = function (nonce) {
69 - if (nonce) {
70 - cachedFreshNonce = nonce;
71 - cachedFreshNonceFetchedAt = Date.now();
72 - mxchatChat.nonce = nonce;
73 - }
74 - nonceRefreshState = 'done';
75 - var pending = nonceRefreshCallbacks;
76 - nonceRefreshCallbacks = [];
77 - pending.forEach(function (cb) { try { cb(); } catch (e) {} });
78 - };
79 -
80 - fetchFreshNonceFromRest()
81 - .then(resolved)
82 - .catch(function () {
83 - // Fallback to the legacy admin-ajax refresh path (issued with the
84 - // old action `mxchat_chat_nonce`; the server still accepts both
85 - // during the compat window).
86 - if (mxchatChat.ajax_url) {
87 - $.post(mxchatChat.ajax_url, { action: 'mxchat_refresh_nonce' })
88 - .done(function (res) {
89 - if (res && res.success && res.data && res.data.nonce) {
90 - resolved(res.data.nonce);
91 - return;
92 - }
93 - resolved(null);
94 - })
95 - .fail(function () { resolved(null); });
96 - } else {
97 - resolved(null);
98 - }
99 - });
100 - }
101 -
102 - // Backwards-compat alias — every existing caller in this file (and any
103 - // out-of-tree consumer that hit this internal API) keeps working unchanged.
104 - function refreshNonceIfNeeded(callback) {
105 - return withFreshNonce(callback);
106 - }
107 -
108 - // Dynamic-settings refresh (plan-32db95).
109 - //
110 - // Every widget setting ships inline in cached page HTML, so behind a
111 - // full-page cache the site owner can't purge (host cache, CDN, the
112 - // browser itself) a toggled setting looks broken until the cache turns
113 - // over. Same distrust-cached-HTML reasoning as the per-request nonce:
114 - // on the FIRST widget open per page load we ask the nonce endpoint for
115 - // the current behavior-gate settings (?with_settings=1), merge them over
116 - // mxchatChat, and rebuild the header menu. Colors are NOT refreshed —
117 - // they're server-inline-styled, so a runtime swap would visibly flash.
118 - // On any failure we keep the inline values silently (nonce-fallback
119 - // posture). At most one request per page load, only if a widget opens.
120 - var dynamicSettingsState = 'idle'; // 'idle' | 'pending' | 'done'
121 -
122 - function mxchatRefreshDynamicSettings() {
123 - if (dynamicSettingsState !== 'idle') return;
124 - if (typeof mxchatChat === 'undefined') return;
125 - dynamicSettingsState = 'pending';
126 -
127 - var applied = function (data) {
128 - dynamicSettingsState = 'done';
129 - if (!data) return; // endpoint unavailable — inline values stand.
130 - if (data.nonce) {
131 - // Seed the nonce cache too: saves the first send's REST
132 - // round-trip and keeps us under the endpoint's rate limit.
133 - cachedFreshNonce = data.nonce;
134 - cachedFreshNonceFetchedAt = Date.now();
135 - mxchatChat.nonce = data.nonce;
136 - }
137 - if (data.settings && typeof data.settings === 'object') {
138 - $.extend(mxchatChat, data.settings);
139 - mxchatRebuildHeaderMenus();
140 - }
141 - };
142 -
143 - fetch(getRestNonceUrl() + '?with_settings=1', {
144 - credentials: 'same-origin',
145 - headers: { 'Accept': 'application/json' }
146 - }).then(function (resp) {
147 - if (!resp.ok) throw new Error('settings refresh failed: ' + resp.status);
148 - return resp.json();
149 - }).then(applied).catch(function () {
150 - // Fallback: legacy admin-ajax refresh path, same as withFreshNonce.
151 - if (mxchatChat.ajax_url) {
152 - $.post(mxchatChat.ajax_url, { action: 'mxchat_refresh_nonce', with_settings: 1 })
153 - .done(function (res) {
154 - applied(res && res.success && res.data ? res.data : null);
155 - })
156 - .fail(function () { applied(null); });
157 - } else {
158 - applied(null);
159 - }
160 - });
161 - }
162 -
2 +
163 3 // ====================================
164 - // MULTI-INSTANCE MANAGEMENT SYSTEM
165 - // ====================================
166 -
167 - // Instance registry - tracks all chatbot instances on the page
168 - const MxChatInstances = {
169 - instances: {},
170 -
171 - // Initialize an instance for a bot
172 - init: function(botId) {
173 - if (!this.instances[botId]) {
174 - // When persistence is OFF, track when this session started
175 - // so the AI only sees messages from this page load
176 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
177 -
178 - this.instances[botId] = {
179 - botId: botId,
180 - sessionId: null,
181 - lastSeenMessageId: '',
182 - notificationCheckInterval: null,
183 - pollingInterval: null,
184 - processedMessageIds: new Set(),
185 - activePdfFile: null,
186 - activeWordFile: null,
187 - chatHistoryLoaded: false,
188 - isStreaming: false,
189 - // Fresh context timestamp - only used when persistence is OFF
190 - sessionStartTimestamp: chatPersistenceEnabled ? 0 : Date.now()
191 - };
192 - }
193 - return this.instances[botId];
194 - },
195 -
196 - // Get instance by botId
197 - get: function(botId) {
198 - return this.instances[botId] || this.init(botId);
199 - },
200 -
201 - // Get all active bot IDs
202 - getAllBotIds: function() {
203 - return Object.keys(this.instances);
204 - },
205 -
206 - // Session management per bot
207 - // Returns existing session ID from cookie or localStorage (with in-memory fallback),
208 - // or null if none exists. Does NOT create a new session — use ensureSession() for that.
209 - getChatSession: function(botId) {
210 - var cookieName = 'mxchat_session_id_' + botId;
211 - var storageKey = 'mxchat_session_id_' + botId;
212 - var sessionId = getCookie(cookieName);
213 -
214 - // Fallback to localStorage if cookie is missing (e.g. cleared by browser/consent)
215 - if (!sessionId) {
216 - try { sessionId = localStorage.getItem(storageKey); } catch (e) {}
217 - }
218 -
219 - // Fallback to in-memory instance when cookie AND localStorage are both blocked
220 - // (Safari ITP, strict tracking prevention, cross-origin iframes with partitioned
221 - // storage). Without this, ensureSession() can generate and store an ID that
222 - // getChatSession() then can't read back, causing null session_ids on send.
223 - if (!sessionId && this.instances[botId] && this.instances[botId].sessionId) {
224 - sessionId = this.instances[botId].sessionId;
225 - }
226 -
227 - // Guard against stored sentinel values that indicate earlier broken writes.
228 - if (sessionId === 'null' || sessionId === 'undefined') {
229 - sessionId = null;
230 - }
231 -
232 - // Re-sync cookie from localStorage if cookie was lost
233 - if (sessionId && !getCookie(cookieName)) {
234 - document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
235 - }
236 -
237 - return sessionId || null;
238 - },
239 -
240 - // Lazy session initializer — called on first user interaction
241 - ensureSession: function(botId) {
242 - botId = botId || 'default';
243 - var instance = this.instances[botId] || this.init(botId);
244 -
245 - if (instance.sessionId) {
246 - return instance.sessionId;
247 - }
248 -
249 - // Check for existing session from cookie or localStorage
250 - var existingSession = this.getChatSession(botId);
251 -
252 - if (existingSession) {
253 - instance.sessionId = existingSession;
254 - } else {
255 - // Brand new session
256 - var newId = generateSessionId();
257 - this.setChatSession(botId, newId);
258 - instance.sessionId = newId;
259 - }
260 -
261 - // Now that we have a session, do the deferred work
262 - refreshNonceIfNeeded();
263 - trackOriginatingPage();
264 -
265 - // Note: loadChatHistory is handled by showChatContainerForBot with loader UI,
266 - // so we do NOT call it here to avoid a race condition.
267 -
268 - return instance.sessionId;
269 - },
270 -
271 - setChatSession: function(botId, sessionId) {
272 - var cookieName = 'mxchat_session_id_' + botId;
273 - var storageKey = 'mxchat_session_id_' + botId;
274 - document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
275 - try { localStorage.setItem(storageKey, sessionId); } catch (e) {}
276 - if (this.instances[botId]) {
277 - this.instances[botId].sessionId = sessionId;
278 - }
279 - },
280 -
281 - resetChatSession: function(botId) {
282 - // Clear old session from localStorage before setting new one
283 - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {}
284 - var newSessionId = generateSessionId();
285 - this.setChatSession(botId, newSessionId);
286 - var $chatBox = getElement(botId, 'chat-box');
287 - if ($chatBox.length) {
288 - // Keep the greeting, drop everything else. Identify the greeting
289 - // by its marker class, NOT by position (plan a1a79b): after a
290 - // chat-persistence restore the first .bot-message is a real
291 - // reply, so ":not(:first)" left a stale answer sitting at the
292 - // top of an otherwise empty box. The positional fallback only
293 - // runs when the marker is absent — a page served from HTML cache
294 - // that predates this release — and behaves exactly as before.
295 - if ($chatBox.find('.mxchat-intro-message').length) {
296 - $chatBox.find('.user-message, .bot-message:not(.mxchat-intro-message), .agent-message').remove();
297 - } else {
298 - $chatBox.find('.user-message, .bot-message:not(:first), .agent-message').remove();
299 - }
300 - }
301 - if (this.instances[botId]) {
302 - this.instances[botId].chatHistoryLoaded = false;
303 - this.instances[botId].processedMessageIds = new Set();
304 - }
305 - },
306 -
307 - // Silent reset — new session ID without clearing the chat UI
308 - // Used when IP changes mid-conversation so the user doesn't see messages vanish
309 - silentResetSession: function(botId) {
310 - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {}
311 - var newSessionId = generateSessionId();
312 - this.setChatSession(botId, newSessionId);
313 - if (this.instances[botId]) {
314 - this.instances[botId].sessionId = newSessionId;
315 - }
316 - return newSessionId;
317 - }
318 - };
319 -
320 - // ====================================
321 - // ELEMENT SELECTOR HELPERS
322 - // ====================================
323 -
324 - // Check if a specific bot has an AI theme assigned (skip inline colors)
325 - function shouldSkipInlineColors(botId) {
326 - // If global AI theme is active, skip inline colors for all bots
327 - if (mxchatChat.skip_inline_colors) {
328 - return true;
329 - }
330 - // Check if this specific bot has a theme assignment
331 - var botAssignments = mxchatChat.bot_theme_assignments || {};
332 - return botAssignments.hasOwnProperty(botId);
333 - }
334 -
335 - // Get element by ID with bot suffix - returns jQuery object
336 - function getElement(botId, elementName) {
337 - return $('#' + elementName + '-' + botId);
338 - }
339 -
340 - // Get element by ID with bot suffix - returns DOM element
341 - function getElementDOM(botId, elementName) {
342 - return document.getElementById(elementName + '-' + botId);
343 - }
344 -
345 - // Get bot ID from any element within a chatbot instance
346 - function getBotIdFromElement(element) {
347 - var $wrapper = $(element).closest('.mxchat-chatbot-wrapper');
348 - if ($wrapper.length) {
349 - return $wrapper.data('bot-id') || 'default';
350 - }
351 - // Fallback: try to find from floating container
352 - var $floating = $(element).closest('.floating-chatbot');
353 - if ($floating.length) {
354 - var id = $floating.attr('id') || '';
355 - var match = id.match(/floating-chatbot-(.+)/);
356 - if (match) return match[1];
357 - }
358 - // Fallback: the pre-chat teaser bubble (#pre-chat-message-{bot_id}) is a SIBLING
359 - // outside .mxchat-chatbot-wrapper / .floating-chatbot, so its children — e.g. the
360 - // .close-pre-chat-message button, which carries only a class and no id — miss both
361 - // branches above. Walk to the nearest ancestor whose id is pre-chat-message-{bot_id}
362 - // and read the suffix. (closest() includes the element itself, so a click directly on
363 - // #pre-chat-message-{bot_id} resolves here too.)
364 - var $preChat = $(element).closest('[id^="pre-chat-message-"]');
365 - if ($preChat.length) {
366 - var preId = $preChat.attr('id') || '';
367 - var preMatch = preId.match(/^pre-chat-message-(.+)$/);
368 - if (preMatch) return preMatch[1];
369 - }
370 - // Fallback: check if element itself has an ID with bot suffix (e.g., floating-chatbot-button-{bot_id})
371 - var elementId = $(element).attr('id') || '';
372 - if (elementId) {
373 - // Match patterns like: floating-chatbot-button-{bot_id}, pre-chat-message-{bot_id}
374 - var idMatch = elementId.match(/^(?:floating-chatbot-button|pre-chat-message|chat-notification-badge)-(.+)$/);
375 - if (idMatch) return idMatch[1];
376 - }
377 - return 'default';
378 - }
379 -
380 - // Get wrapper element for a bot
381 - function getWrapper(botId) {
382 - return getElement(botId, 'mxchat-chatbot-wrapper');
383 - }
384 -
385 - // ====================================
386 4 // GLOBAL VARIABLES & CONFIGURATION
387 5 // ====================================
388 6 const toolbarIconColor = mxchatChat.toolbar_icon_color || '#212121';
389 -
390 - // Initialize color settings (these are global as they come from PHP)
7 +
8 + // Initialize color settings
391 9 var userMessageBgColor = mxchatChat.user_message_bg_color;
392 10 var userMessageFontColor = mxchatChat.user_message_font_color;
393 11 var botMessageBgColor = mxchatChat.bot_message_bg_color;
394 12 var botMessageFontColor = mxchatChat.bot_message_font_color;
@@ -393,82 +11,53 @@
393 11 var botMessageBgColor = mxchatChat.bot_message_bg_color;
394 12 var botMessageFontColor = mxchatChat.bot_message_font_color;
395 13 var liveAgentMessageBgColor = mxchatChat.live_agent_message_bg_color;
396 14 var liveAgentMessageFontColor = mxchatChat.live_agent_message_font_color;
15 +
16 + var linkTarget = mxchatChat.link_target_toggle === 'on' ? '_blank' : '_self';
17 + let lastSeenMessageId = '';
18 + let notificationCheckInterval;
19 + let notificationBadge;
20 + var sessionId = getChatSession();
21 + let pollingInterval;
22 + let processedMessageIds = new Set();
23 + let activePdfFile = null;
24 + let activeWordFile = null;
397 25
398 - var linkTarget = mxchatChat.link_target_toggle === 'on' ? '_blank' : '_self';
399 26
400 27 // ====================================
401 - // SESSION MANAGEMENT (Legacy compatibility)
28 + // SESSION MANAGEMENT
402 29 // ====================================
403 -
30 +
31 + function getChatSession() {
32 + var sessionId = getCookie('mxchat_session_id');
33 + //console.log("Session ID retrieved from cookie: ", sessionId);
34 +
35 + if (!sessionId) {
36 + sessionId = generateSessionId();
37 + //console.log("Generated new session ID: ", sessionId);
38 + setChatSession(sessionId);
39 + }
40 +
41 + //console.log("Final session ID: ", sessionId);
42 + return sessionId;
43 + }
44 +
45 + function setChatSession(sessionId) {
46 + // Set the cookie with a 24-hour expiration (86400 seconds)
47 + document.cookie = "mxchat_session_id=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
48 + }
49 +
404 50 function getCookie(name) {
405 51 let value = "; " + document.cookie;
406 52 let parts = value.split("; " + name + "=");
407 53 if (parts.length == 2) return parts.pop().split(";").shift();
408 54 }
409 -
55 +
410 56 function generateSessionId() {
411 - // Session IDs function as the de-facto bearer token for an anonymous
412 - // chat, so generate them with a CSPRNG when available. Math.random is a
413 - // legacy fallback for ancient/sandboxed environments that lack
414 - // window.crypto. The 'mxchat_chat_' prefix is preserved exactly (other
415 - // code pattern-matches on it). (plan-0c17b5)
416 - var rand;
417 - try {
418 - if (window.crypto && window.crypto.getRandomValues) {
419 - var buf = new Uint8Array(16); // 128 bits
420 - window.crypto.getRandomValues(buf);
421 - rand = Array.prototype.map.call(buf, function (b) {
422 - return ('0' + b.toString(16)).slice(-2);
423 - }).join('');
424 - }
425 - } catch (e) {}
426 - if (!rand) {
427 - rand = Math.random().toString(36).substr(2, 9); // legacy fallback
428 - }
429 - return 'mxchat_chat_' + rand;
57 + return 'mxchat_chat_' + Math.random().toString(36).substr(2, 9);
430 58 }
431 59
432 - // Legacy function - now delegates to instance manager
433 - function getChatSession(botId) {
434 - botId = botId || 'default';
435 - return MxChatInstances.getChatSession(botId);
436 - }
437 -
438 - function setChatSession(sessionId, botId) {
439 - botId = botId || 'default';
440 - MxChatInstances.setChatSession(botId, sessionId);
441 - }
442 -
443 - function resetChatSession(botId) {
444 - botId = botId || 'default';
445 - MxChatInstances.resetChatSession(botId);
446 - }
447 -
448 - // ====================================
449 - // INITIALIZE ALL CHATBOT INSTANCES
450 - // ====================================
451 -
452 - function initializeAllInstances() {
453 - // Find all chatbot wrappers on the page
454 - $('.mxchat-chatbot-wrapper').each(function() {
455 - var botId = $(this).data('bot-id') || 'default';
456 - MxChatInstances.init(botId);
457 - initializeBotInstance(botId);
458 - });
459 - }
460 -
461 - function initializeBotInstance(botId) {
462 - var instance = MxChatInstances.get(botId);
463 -
464 - // Initialize quick questions state for this bot
465 - checkQuickQuestionsState(botId);
466 -
467 - // Note: Event handlers use event delegation with class selectors,
468 - // so they work automatically for all instances without per-bot setup
469 - }
470 -
471 60 // ====================================
472 61 // CONTEXTUAL AWARENESS FUNCTIONALITY
473 62 // ====================================
474 63
@@ -544,9 +133,9 @@
544 133 const elements = clone.querySelectorAll(selector);
545 134 elements.forEach(el => el.remove());
546 135 });
547 136
548 - // Extract MxChat context data attributes before getting text content
137 + // NEW: Extract MxChat context data attributes before getting text content
549 138 const contextData = [];
550 139 clone.querySelectorAll('[data-mxchat-context]').forEach(el => {
551 140 const contextValue = el.dataset.mxchatContext;
552 141 if (contextValue && contextValue.trim()) {
@@ -580,272 +169,73 @@
580 169 content: pageContent
581 170 };
582 171 }
583 172
584 -// Track originating page when chat starts
585 -function trackOriginatingPage() {
586 - const sessionId = getChatSession();
587 - const pageUrl = window.location.href;
588 - const pageTitle = document.title || 'Untitled Page';
589 -
590 - // Only track once per session
591 - const trackingKey = 'mxchat_originating_tracked_' + sessionId;
592 - if (sessionStorage.getItem(trackingKey)) {
593 - return;
594 - }
595 -
596 - $.ajax({
597 - url: mxchatChat.ajax_url,
598 - type: 'POST',
599 - data: {
600 - action: 'mxchat_track_originating_page',
601 - session_id: sessionId,
602 - page_url: pageUrl,
603 - page_title: pageTitle,
604 - nonce: mxchatChat.nonce
605 - },
606 - success: function(response) {
607 - if (response.success) {
608 - sessionStorage.setItem(trackingKey, 'true');
609 - }
610 - }
611 - });
612 -}
613 -
614 173 // ====================================
615 174 // CORE CHAT FUNCTIONALITY
616 175 // ====================================
617 -
618 -// Helper functions to disable/enable chat input while waiting for response
619 -function disableChatInput(botId) {
620 - botId = botId || 'default';
621 - var chatInput = getElementDOM(botId, 'chat-input');
622 - var sendButton = getElementDOM(botId, 'send-button');
623 - if (chatInput) {
624 - chatInput.disabled = true;
625 - chatInput.style.opacity = '0.6';
626 - }
627 - if (sendButton) {
628 - sendButton.disabled = true;
629 - sendButton.style.opacity = '0.5';
630 - sendButton.style.pointerEvents = 'none';
631 - }
632 -}
633 -
634 -// Whether the input may grab focus after a completed reply (plan 03799f).
635 -// On coarse-pointer devices focusing a text input summons the on-screen
636 -// keyboard over the answer the visitor is trying to read, so 'auto' (the
637 -// default) focuses only on fine-pointer devices. The site-wide
638 -// mxchat_autofocus_after_reply PHP filter can force 'on'/'off'.
639 -// NOT used on widget open (:~3424) — that focus is a deliberate act and is
640 -// what makes the widget keyboard-accessible.
641 -function mxchatShouldAutofocusAfterReply() {
642 - var pref = (typeof mxchatChat !== 'undefined' && mxchatChat.autofocus_after_reply) || 'auto';
643 - if (pref === 'on') return true;
644 - if (pref === 'off') return false;
645 - try {
646 - return !window.matchMedia('(pointer: coarse)').matches;
647 - } catch (err) {
648 - return true;
649 - }
650 -}
651 -
652 -function enableChatInput(botId) {
653 - botId = botId || 'default';
654 - var chatInput = getElementDOM(botId, 'chat-input');
655 - var sendButton = getElementDOM(botId, 'send-button');
656 - if (chatInput) {
657 - chatInput.disabled = false;
658 - chatInput.style.opacity = '1';
659 - if (mxchatShouldAutofocusAfterReply()) {
660 - try { chatInput.focus({ preventScroll: true }); } catch (err) { chatInput.focus(); }
661 - }
662 - }
663 - if (sendButton) {
664 - sendButton.disabled = false;
665 - sendButton.style.opacity = '1';
666 - sendButton.style.pointerEvents = 'auto';
667 - }
668 - // Every completion path re-enables input, so this is the single restore
669 - // point for the streaming Stop affordance (no-op when not in stop mode).
670 - mxchatRestoreSendButton(botId);
671 -}
672 -
673 -// --- Streaming Stop control -------------------------------------------------
674 -// One live stream handle per bot instance, so Stop on one widget never aborts
675 -// another bot on the same page.
676 -var mxchatActiveStreams = {};
677 -// Original send-button markup, captured once per bot the first time the Stop
678 -// state is shown (never captured while already in stop mode, so a rapid
679 -// stop-then-resend can't save the stop glyph as the "original").
680 -var mxchatSendMarkup = {};
681 -
682 -function mxchatShowStopButton(botId) {
683 - var btn = getElementDOM(botId, 'send-button');
684 - if (!btn) return;
685 - if (!btn.classList.contains('mxchat-stop-mode')) {
686 - mxchatSendMarkup[botId] = {
687 - html: btn.innerHTML,
688 - label: btn.getAttribute('aria-label')
689 - };
690 - }
691 -
692 - // Mirror the send icon's rendered size + color so the stop glyph looks
693 - // native, including custom send images/colors and theme overrides.
694 - var child = btn.querySelector('svg, img');
695 - var size = 25;
696 - var color = '';
697 - if (child) {
698 - var rect = child.getBoundingClientRect();
699 - if (rect.width) {
700 - size = Math.round(Math.min(rect.width, rect.height));
701 - }
702 - var cs = window.getComputedStyle(child);
703 - color = (child.tagName.toLowerCase() === 'svg' ? cs.fill : cs.color) || '';
704 - }
705 - var stopLabel = (typeof mxchatChat !== 'undefined' && mxchatChat.stop_button_label) || 'Stop response';
706 - btn.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" style="width:' + size + 'px;height:' + size + 'px;' + (color ? 'fill:' + color + ';' : '') + '"><rect x="5" y="5" width="14" height="14" rx="3"></rect></svg>';
707 - // An add-on's DIRECT send-button handler (e.g. mxchat-vision's rebind) can
708 - // start a stream synchronously while the originating click is still
709 - // bubbling up to our delegated handler. Without this guard, that handler
710 - // reads the just-added stop-mode class as a user Stop press and aborts the
711 - // brand-new stream — the user's message renders but no reply ever fires
712 - // (plan-4bba64 silent message loss). The flag only spans the current event
713 - // dispatch: cleared on the next macrotask, long before a real Stop click.
714 - btn.__mxchatStopJustShown = true;
715 - setTimeout(function () { btn.__mxchatStopJustShown = false; }, 0);
716 - btn.classList.add('mxchat-stop-mode');
717 - btn.setAttribute('aria-label', stopLabel);
718 - btn.setAttribute('title', stopLabel);
719 - // disableChatInput() ran when the turn was sent; the Stop control itself
720 - // must stay clickable while the textarea remains disabled.
721 - btn.disabled = false;
722 - btn.style.opacity = '1';
723 - btn.style.pointerEvents = 'auto';
724 -}
725 -
726 -function mxchatRestoreSendButton(botId) {
727 - var btn = getElementDOM(botId, 'send-button');
728 - var saved = mxchatSendMarkup[botId];
729 - if (!btn || !btn.classList.contains('mxchat-stop-mode') || !saved) return;
730 - btn.innerHTML = saved.html;
731 - btn.classList.remove('mxchat-stop-mode');
732 - btn.removeAttribute('title');
733 - if (saved.label) {
734 - btn.setAttribute('aria-label', saved.label);
735 - }
736 -}
737 -
738 -function mxchatStopStreaming(botId) {
739 - var entry = mxchatActiveStreams[botId];
740 - if (!entry || !entry.controller) return;
741 - entry.aborted = true;
742 - try { entry.controller.abort(); } catch (e) {}
743 -}
744 -
745 -// Returns true when a stream rejection came from an intentional Stop click:
746 -// keep the partial text as the turn's answer — no error UI, no fallback resend.
747 -function mxchatHandleStreamAbort(botId, accumulatedContent, callback) {
748 - var entry = mxchatActiveStreams[botId];
749 - if (!entry || !entry.aborted) return false;
750 - delete mxchatActiveStreams[botId];
751 - if (!accumulatedContent) {
752 - // Stopped before the first chunk: drop the thinking bubble, no orphan message.
753 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
754 - }
755 - enableChatInput(botId); // also restores the send icon
756 - if (callback) {
757 - callback(accumulatedContent || '');
758 - }
759 - return true;
760 -}
761 -
762 176 // Update your existing sendMessage function
763 -function sendMessage(botId) {
764 - botId = botId || 'default';
765 - MxChatInstances.ensureSession(botId);
766 - var $chatInput = getElement(botId, 'chat-input');
767 - var message = $chatInput.val();
768 -
177 +function sendMessage() {
178 + var message = $('#chat-input').val();
179 +
769 180 // ADD PROMPT HOOK HERE
770 - if (typeof customMxChatFilter === 'function') {
771 - message = customMxChatFilter(message, "prompt");
181 + if (typeof customMxChatFilter === 'function') {
182 + message = customMxChatFilter(message, "prompt");
772 183 }
773 -
184 +
774 185 if (message) {
775 - // Don't disable input in live agent mode - let users chat freely
776 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
777 - var isAgentMode = modeIndicator && modeIndicator.textContent === 'Live Agent';
778 - if (!isAgentMode) {
779 - disableChatInput(botId);
780 - }
186 + appendMessage("user", message);
187 + $('#chat-input').val('');
188 + $('#chat-input').css('height', 'auto');
781 189
782 - appendMessage("user", message, '', [], false, botId);
783 - $chatInput.val('');
784 - mxchatUpdateCharCounter($chatInput[0]); // reset the char counter after send (plan 7091a2)
785 - $chatInput.css('height', 'auto');
786 -
787 - if (hasQuickQuestions(botId)) {
788 - collapseQuickQuestions(botId);
190 + if (hasQuickQuestions()) {
191 + collapseQuickQuestions();
789 192 }
790 - appendThinkingMessage(botId);
791 - scrollToBottom(botId);
193 + appendThinkingMessage();
194 + scrollToBottom();
792 195
793 - const currentModel = mxchatChat.model || 'gpt-5.6-sol';
196 + const currentModel = mxchatChat.model || 'gpt-4o';
794 197
795 198 // Check if streaming is enabled AND supported for this model
796 199 if (shouldUseStreaming(currentModel)) {
797 200 callMxChatStream(message, function(response) {
798 - // Content is final: releasing aria-busy lets the live region
799 - // announce the completed reply once (plan 67f126).
800 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').removeClass('temporary-message').attr('aria-busy', 'false');
801 - }, botId);
201 + $('.bot-message.temporary-message').removeClass('temporary-message');
202 + });
802 203 } else {
803 204 callMxChat(message, function(response) {
804 - replaceLastMessage("bot", response, '', [], botId);
805 - }, botId);
205 + replaceLastMessage("bot", response);
206 + });
806 207 }
807 208 }
808 209 }
809 210
810 211 // Update your existing sendMessageToChatbot function
811 -function sendMessageToChatbot(message, botId) {
812 - botId = botId || 'default';
813 - MxChatInstances.ensureSession(botId);
814 -
212 +function sendMessageToChatbot(message) {
815 213 // ADD PROMPT HOOK HERE
816 214 if (typeof customMxChatFilter === 'function') {
817 215 message = customMxChatFilter(message, "prompt");
818 216 }
217 +
218 + var sessionId = getChatSession();
819 219
820 - // Don't disable input in live agent mode - let users chat freely
821 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
822 - var isAgentMode = modeIndicator && modeIndicator.textContent === 'Live Agent';
823 - if (!isAgentMode) {
824 - disableChatInput(botId);
220 + if (hasQuickQuestions()) {
221 + collapseQuickQuestions();
825 222 }
223 + appendThinkingMessage();
224 + scrollToBottom();
826 225
827 - var sessionId = getChatSession(botId);
226 + const currentModel = mxchatChat.model || 'gpt-4o';
828 227
829 - if (hasQuickQuestions(botId)) {
830 - collapseQuickQuestions(botId);
831 - }
832 - appendThinkingMessage(botId);
833 - scrollToBottom(botId);
834 -
835 - const currentModel = mxchatChat.model || 'gpt-5.6-sol';
836 -
837 228 // Check if streaming is enabled AND supported for this model
838 229 if (shouldUseStreaming(currentModel)) {
839 230 callMxChatStream(message, function(response) {
840 - // Final content — release aria-busy so the reply announces once (67f126).
841 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').removeClass('temporary-message').attr('aria-busy', 'false');
842 - }, botId);
231 + $('.bot-message.temporary-message').removeClass('temporary-message');
232 + });
843 233 } else {
844 234 callMxChat(message, function(response) {
845 - getElement(botId, 'chat-box').find('.temporary-message').remove();
846 - replaceLastMessage("bot", response, '', [], botId);
847 - }, botId);
235 + $('.temporary-message').remove();
236 + replaceLastMessage("bot", response);
237 + });
848 238 }
849 239 }
850 240
851 241 // Updated shouldUseStreaming function with debugging
@@ -860,100 +250,25 @@
860 250 // Only use streaming if both enabled and supported
861 251 return streamingEnabled && streamingSupported;
862 252 }
863 253
864 -// Helper function to handle chat mode updates
865 -function handleChatModeUpdates(response, responseText) {
866 - // Check for explicit chat mode in response (THIS IS THE KEY FIX)
867 - if (response.chat_mode) {
868 - updateChatModeIndicator(response.chat_mode);
869 - return; // Return early since we found explicit mode
870 - }
871 - // Check for fallback response chat mode
872 - else if (response.fallbackResponse && response.fallbackResponse.chat_mode) {
873 - updateChatModeIndicator(response.fallbackResponse.chat_mode);
874 - return; // Return early since we found explicit mode
875 - }
876 -
877 - // Only do text-based detection if no explicit mode was provided
878 - // Check for specific AI chatbot response text
879 - if (responseText === 'You are now chatting with the AI chatbot.' ||
880 - responseText.includes('now chatting with the AI') ||
881 - responseText.includes('switched to AI mode') ||
882 - responseText.includes('AI chatbot is now')) {
883 - updateChatModeIndicator('ai');
884 - }
885 - // Check for agent transfer messages
886 - else if (responseText.includes('agent') &&
887 - (responseText.includes('transfer') || responseText.includes('connected'))) {
888 - updateChatModeIndicator('agent');
889 - }
890 -}
891 -
892 -// Function to get bot ID from any element or wrapper
893 -// If element is provided, finds the bot ID from its wrapper
894 -// If no element, returns 'default' (for backward compatibility)
895 -function getMxChatBotId(element) {
896 - if (element) {
897 - return getBotIdFromElement(element);
898 - }
899 - // Fallback: find first chatbot wrapper on page
900 - const chatbotWrapper = document.querySelector('.mxchat-chatbot-wrapper');
901 - return chatbotWrapper ? chatbotWrapper.getAttribute('data-bot-id') || 'default' : 'default';
902 -}
903 -
904 -function callMxChat(message, callback, botId) {
905 - botId = botId || getMxChatBotId();
906 -
907 - // Streaming fallbacks land here: drop any leftover stream handle and
908 - // return the button to its send state (no-op for plain non-stream turns).
909 - if (mxchatActiveStreams[botId]) {
910 - delete mxchatActiveStreams[botId];
911 - }
912 - mxchatRestoreSendButton(botId);
913 -
914 - // Store the message in case we need to retry after session reset
915 - getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', message);
916 -
254 +function callMxChat(message, callback) {
917 255 // Get page context if contextual awareness is enabled
918 256 const pageContext = getPageContext();
919 -
920 - // Get instance for session start timestamp (used when persistence is OFF)
921 - var instance = MxChatInstances.get(botId);
922 -
923 - // Guarantee a non-null session_id before the AJAX leaves. ensureSession() is idempotent
924 - // and returns the guaranteed-present session id from the in-memory instance even when
925 - // cookie/localStorage writes are silently blocked by the browser.
926 - var sessionId = MxChatInstances.ensureSession(botId);
927 - if (!sessionId || sessionId === 'null' || sessionId === 'undefined') {
928 - // Last-resort generation to ensure we never POST a null marker.
929 - sessionId = generateSessionId();
930 - MxChatInstances.setChatSession(botId, sessionId);
931 - }
932 -
933 - // Wait for the page-cache nonce refresh to complete before firing the
934 - // chat-send AJAX. On cached pages the inline mxchatChat.nonce is stale
935 - // until refreshNonceIfNeeded() returns; constructing ajaxData inside the
936 - // callback guarantees we read the fresh value. See plan-c5457f.
937 - refreshNonceIfNeeded(function() {
257 +
938 258 // Prepare AJAX data
939 259 const ajaxData = {
940 260 action: 'mxchat_handle_chat_request',
941 261 message: message,
942 - session_id: sessionId,
943 - nonce: mxchatChat.nonce,
944 - current_page_url: window.location.href,
945 - current_page_title: document.title,
946 - bot_id: botId,
947 - // Pass session start timestamp so AI context matches what user sees
948 - session_start_timestamp: instance.sessionStartTimestamp || 0
262 + session_id: getChatSession(),
263 + nonce: mxchatChat.nonce
949 264 };
950 -
265 +
951 266 // Add page context if available
952 267 if (pageContext) {
953 268 ajaxData.page_context = JSON.stringify(pageContext);
954 269 }
955 -
270 +
956 271 // CHECK FOR VISION FLAGS AND ADD THEM
957 272 if (window.mxchatVisionProcessed) {
958 273 ajaxData.vision_processed = true;
959 274 ajaxData.original_user_message = window.mxchatOriginalMessage || message;
@@ -962,9 +277,9 @@
962 277 window.mxchatVisionProcessed = false;
963 278 window.mxchatOriginalMessage = null;
964 279 window.mxchatVisionImagesCount = 0;
965 280 }
966 -
281 +
967 282 $.ajax({
968 283 url: mxchatChat.ajax_url,
969 284 type: 'POST',
970 285 dataType: 'json',
@@ -969,99 +284,37 @@
969 284 type: 'POST',
970 285 dataType: 'json',
971 286 data: ajaxData,
972 287 success: function(response) {
973 - // IMMEDIATE CHAT MODE UPDATE - This should be FIRST
974 - if (response.chat_mode) {
975 - updateChatModeIndicator(response.chat_mode, botId);
976 - }
288 + // Log the full response for debugging
289 + //console.log("API Response:", response);
977 290
978 - // Also check in data property if response is wrapped
979 - if (response.data && response.data.chat_mode) {
980 - updateChatModeIndicator(response.data.chat_mode, botId);
981 - }
982 -
983 - // SECURITY FIX: Check for errors FIRST before checking for success
984 - // This ensures API errors (quota exceeded, invalid key, rate limit) are properly displayed
985 - if (response.success === false || (response.data && response.data.error_message)) {
986 - let errorMessage = "";
987 - let errorCode = "";
988 -
989 - // Check various possible error locations in the response
990 - if (response.data && response.data.error_message) {
991 - errorMessage = response.data.error_message;
992 - errorCode = response.data.error_code || "";
993 - } else if (response.error_message) {
994 - errorMessage = response.error_message;
995 - errorCode = response.error_code || "";
996 - } else if (response.message) {
997 - errorMessage = response.message;
998 - } else if (typeof response.data === 'string') {
999 - errorMessage = response.data;
1000 - } else {
1001 - // Fallback for any other unexpected response format
1002 - errorMessage = "An error occurred. Please try again or contact support.";
1003 - }
1004 -
1005 - // Handle session reset action (IP changed, session expired, etc.)
1006 - // Silent reset — keep chat UI intact, just get a new session and retry
1007 - if (response.data && response.data.action === 'reset_session') {
1008 - MxChatInstances.silentResetSession(botId);
1009 - // Re-send the original message with the new session (user message is already displayed)
1010 - var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message');
1011 - if (originalMessage) {
1012 - getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null);
1013 - var currentModel = mxchatChat.model || 'gpt-5.6-sol';
1014 - if (shouldUseStreaming(currentModel)) {
1015 - callMxChatStream(originalMessage, function(response) {
1016 - // Final content — release aria-busy so the reply announces once (67f126).
1017 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').removeClass('temporary-message').attr('aria-busy', 'false');
1018 - }, botId);
1019 - } else {
1020 - callMxChat(originalMessage, function(response) {
1021 - replaceLastMessage("bot", response, '', [], botId);
1022 - }, botId);
1023 - }
1024 - }
1025 - return;
1026 - }
1027 -
1028 - // Format user-friendly error message
1029 - let displayMessage = errorMessage;
1030 -
1031 - // Customize message for admin users
1032 - if (mxchatChat.is_admin) {
1033 - // For admin users, show more technical details including error code
1034 - displayMessage = errorMessage + (errorCode ? " (Error code: " + errorCode + ")" : "");
1035 - }
1036 -
1037 - replaceLastMessage("bot", displayMessage, '', [], botId);
1038 - return; // Exit early for errors
1039 - }
1040 -
1041 - // NOW check if this is a successful response by looking for text, html, or message fields
291 + // First check if this is a successful response by looking for text, html, or message fields
1042 292 // This preserves compatibility with your server response format
1043 - if (response.text !== undefined || response.html !== undefined || response.message !== undefined ||
293 + if (response.text !== undefined || response.html !== undefined || response.message !== undefined ||
1044 294 (response.success === true && response.data && response.data.status === 'waiting_for_agent')) {
1045 295
1046 296 // Handle successful response - this is your original success handling code
1047 297
1048 - // Handle other responses
1049 - let responseText = response.text || '';
1050 - let responseHtml = response.html || '';
1051 - let responseMessage = response.message || '';
298 + // Existing chat mode check
299 + if (response.chat_mode) {
300 + updateChatModeIndicator(response.chat_mode);
301 + }
302 + else if (response.fallbackResponse && response.fallbackResponse.chat_mode) {
303 + updateChatModeIndicator(response.fallbackResponse.chat_mode);
304 + }
1052 305
1053 306 // Add PDF filename handling
1054 307 if (response.data && response.data.filename) {
1055 - showActivePdf(response.data.filename, botId);
1056 - var instance = MxChatInstances.get(botId);
1057 - instance.activePdfFile = response.data.filename;
308 + showActivePdf(response.data.filename);
309 + activePdfFile = response.data.filename;
1058 310 }
1059 311
1060 312 // Add redirect check here
1061 313 if (response.redirect_url) {
314 + let responseText = response.text || '';
1062 315 if (responseText) {
1063 - replaceLastMessage("bot", responseText, '', [], botId);
316 + replaceLastMessage("bot", responseText);
1064 317 }
1065 318 setTimeout(() => {
1066 319 window.location.href = response.redirect_url;
1067 320 }, 1500);
@@ -1069,17 +322,24 @@
1069 322 }
1070 323
1071 324 // Check for live agent response
1072 325 if (response.success && response.data && response.data.status === 'waiting_for_agent') {
1073 - removeThinkingDots(botId);
1074 - updateChatModeIndicator('agent', botId);
1075 - enableChatInput(botId);
326 + updateChatModeIndicator('agent');
1076 327 return;
1077 328 }
1078 329
330 + // Handle other responses
331 + let responseText = response.text || '';
332 + let responseHtml = response.html || '';
333 + let responseMessage = response.message || '';
334 +
335 + if (responseText === 'You are now chatting with the AI chatbot.') {
336 + updateChatModeIndicator('ai');
337 + }
338 +
1079 339 // Handle the message and show notification if chat is hidden
1080 340 if (responseText || responseHtml || responseMessage) {
1081 -
341 +
1082 342 // ADD RESPONSE HOOKS HERE - BEFORE DISPLAYING
1083 343 if (responseText && typeof customMxChatFilter === 'function') {
1084 344 responseText = customMxChatFilter(responseText, "response");
1085 345 }
@@ -1085,53 +345,89 @@
1085 345 }
1086 346 if (responseMessage && typeof customMxChatFilter === 'function') {
1087 347 responseMessage = customMxChatFilter(responseMessage, "response");
1088 348 }
1089 -
349 +
1090 350 // Update the messages as before
1091 351 if (responseText && responseHtml) {
1092 - replaceLastMessage("bot", responseText, responseHtml, [], botId);
352 + replaceLastMessage("bot", responseText, responseHtml);
1093 353 } else if (responseText) {
1094 - replaceLastMessage("bot", responseText, '', [], botId);
354 + replaceLastMessage("bot", responseText);
1095 355 } else if (responseHtml) {
1096 - replaceLastMessage("bot", "", responseHtml, [], botId);
356 + replaceLastMessage("bot", "", responseHtml);
1097 357 } else if (responseMessage) {
1098 - replaceLastMessage("bot", responseMessage, '', [], botId);
358 + replaceLastMessage("bot", responseMessage);
1099 359 }
1100 360
1101 361 // Check if chat is hidden and show notification
1102 - var $floatingChatbot = getElement(botId, 'floating-chatbot');
1103 - if ($floatingChatbot.hasClass('hidden')) {
1104 - var $badge = getElement(botId, 'chat-notification-badge');
1105 - if ($badge.length) {
1106 - $badge.show();
362 + if ($('#floating-chatbot').hasClass('hidden')) {
363 + const badge = $('#chat-notification-badge');
364 + if (badge.length) {
365 + badge.show();
1107 366 }
1108 367 }
1109 368 } else {
1110 - var emptyMsg = "I received an empty response. Please try again or contact support if this persists.";
1111 - if (response.vectorstore_error) {
1112 - emptyMsg = "I received an empty response. Debug info: " + response.vectorstore_error;
1113 - }
1114 - replaceLastMessage("bot", emptyMsg, '', [], botId);
369 + ////console.error("Unexpected response format:", response);
370 + replaceLastMessage("bot", "I received an empty response. Please try again or contact support if this persists.");
1115 371 }
1116 372
1117 373 if (response.message_id) {
1118 - var instance = MxChatInstances.get(botId);
1119 - instance.lastSeenMessageId = response.message_id;
374 + lastSeenMessageId = response.message_id;
1120 375 }
1121 376
1122 377 return;
1123 378 }
1124 379
1125 - // Fallback for truly unexpected response formats
1126 - replaceLastMessage("bot", "Unexpected response format. Please try again or contact support.", '', [], botId);
380 + // If we got here, it's likely an error response
381 + // Now we can check for error conditions with our robust error handling
382 +
383 + let errorMessage = "";
384 + let errorCode = "";
385 +
386 + // Check various possible error locations in the response
387 + if (response.data && response.data.error_message) {
388 + errorMessage = response.data.error_message;
389 + errorCode = response.data.error_code || "";
390 + } else if (response.error_message) {
391 + errorMessage = response.error_message;
392 + errorCode = response.error_code || "";
393 + } else if (response.message) {
394 + errorMessage = response.message;
395 + } else if (typeof response.data === 'string') {
396 + errorMessage = response.data;
397 + } else if (!response.success) {
398 + // Explicit check for success: false without other error info
399 + errorMessage = "An error occurred. Please try again or contact support.";
400 + } else {
401 + // Fallback for any other unexpected response format
402 + errorMessage = "Unexpected response received. Please try again or contact support.";
403 + }
404 +
405 + // Log the error with code for debugging
406 + //console.log("Response data:", response.data);
407 + ////console.error("API Error:", errorMessage, "Code:", errorCode);
408 +
409 + // Format user-friendly error message
410 + let displayMessage = errorMessage;
411 +
412 + // Customize message for admin users
413 + if (mxchatChat.is_admin) {
414 + // For admin users, show more technical details including error code
415 + displayMessage = errorMessage + (errorCode ? " (Error code: " + errorCode + ")" : "");
416 + }
417 +
418 + replaceLastMessage("bot", displayMessage);
1127 419 },
1128 420 error: function(xhr, status, error) {
421 + //console.error("AJAX Error:", status, error);
422 + //console.log("Response Text:", xhr.responseText);
423 +
1129 424 let errorMessage = "An unexpected error occurred.";
1130 425
1131 426 // Try to parse the response if it's JSON
1132 427 try {
1133 428 const responseJson = JSON.parse(xhr.responseText);
429 + //console.log("Parsed error response:", responseJson);
1134 430
1135 431 if (responseJson.data && responseJson.data.error_message) {
1136 432 errorMessage = responseJson.data.error_message;
1137 433 } else if (responseJson.message) {
@@ -1151,23 +447,20 @@
1151 447 errorMessage = "Server error: The server encountered an issue. Please try again later.";
1152 448 }
1153 449 }
1154 450
1155 - replaceLastMessage("bot", errorMessage, '', [], botId);
451 + replaceLastMessage("bot", errorMessage);
1156 452 }
1157 453 });
1158 - }); // refreshNonceIfNeeded
1159 454 }
1160 455
1161 -function callMxChatStream(message, callback, botId) {
1162 - botId = botId || getMxChatBotId();
1163 -
1164 - // Store the message in case we need to retry after session reset
1165 - getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', message);
1166 -
1167 - const currentModel = mxchatChat.model || 'gpt-5.6-sol';
456 +function callMxChatStream(message, callback) {
457 + //console.log("Using streaming for message:", message);
458 +
459 + const currentModel = mxchatChat.model || 'gpt-4o';
1168 460 if (!isStreamingSupported(currentModel)) {
1169 - callMxChat(message, callback, botId);
461 + //console.log("Streaming not supported, falling back to regular call");
462 + callMxChat(message, callback);
1170 463 return;
1171 464 }
1172 465
1173 466 // Get page context if contextual awareness is enabled
@@ -1172,35 +465,14 @@
1172 465
1173 466 // Get page context if contextual awareness is enabled
1174 467 const pageContext = getPageContext();
1175 468
1176 - // Get instance for session start timestamp (used when persistence is OFF)
1177 - var instance = MxChatInstances.get(botId);
1178 -
1179 - // Guarantee a non-null session_id before the fetch. FormData.append() stringifies any
1180 - // non-string value via String(), so passing `null` would POST the literal string "null"
1181 - // and land in the transcripts table as a ghost session. ensureSession() always returns
1182 - // a real string even when cookies/localStorage are blocked.
1183 - var streamSessionId = MxChatInstances.ensureSession(botId);
1184 - if (!streamSessionId || streamSessionId === 'null' || streamSessionId === 'undefined') {
1185 - streamSessionId = generateSessionId();
1186 - MxChatInstances.setChatSession(botId, streamSessionId);
1187 - }
1188 -
1189 - // Wait for the page-cache nonce refresh before constructing formData (which
1190 - // captures mxchatChat.nonce by value). Mirrors callMxChat's wrapping. See plan-c5457f.
1191 - refreshNonceIfNeeded(function() {
1192 469 const formData = new FormData();
1193 470 formData.append('action', 'mxchat_stream_chat');
1194 471 formData.append('message', message);
1195 - formData.append('session_id', streamSessionId);
472 + formData.append('session_id', getChatSession());
1196 473 formData.append('nonce', mxchatChat.nonce);
1197 - formData.append('current_page_url', window.location.href);
1198 - formData.append('current_page_title', document.title);
1199 - formData.append('bot_id', botId);
1200 - // Pass session start timestamp so AI context matches what user sees
1201 - formData.append('session_start_timestamp', instance.sessionStartTimestamp || 0);
1202 -
474 +
1203 475 // Add page context if available
1204 476 if (pageContext) {
1205 477 formData.append('page_context', JSON.stringify(pageContext));
1206 478 }
@@ -1217,60 +489,74 @@
1217 489 }
1218 490
1219 491 let accumulatedContent = '';
1220 492 let testingDataReceived = false;
1221 - let streamingStarted = false;
1222 - // Server-pushed html to append as its OWN bot bubble once the stream
1223 - // finishes (e.g. the consent-safe YouTube embed, plan 03ba33). Rendering is
1224 - // deferred to [DONE] so the embed always lands BELOW the streamed text.
1225 - let pendingAppendHtml = '';
1226 493
1227 - // Abortable stream: a fresh controller per turn, keyed by bot instance.
1228 - // The Stop control (send button swapped in place) aborts both the read
1229 - // loop and the underlying request.
1230 - var streamControl = { controller: new AbortController(), aborted: false };
1231 - mxchatActiveStreams[botId] = streamControl;
1232 - mxchatShowStopButton(botId);
1233 -
1234 494 fetch(mxchatChat.ajax_url, {
1235 495 method: 'POST',
1236 496 body: formData,
1237 - credentials: 'same-origin',
1238 - signal: streamControl.controller.signal
497 + credentials: 'same-origin'
1239 498 })
1240 499 .then(response => {
1241 - // Store the response for potential fallback handling
1242 - const responseClone = response.clone();
1243 -
500 + //console.log("Streaming response received:", response);
501 +
1244 502 if (!response.ok) {
1245 - // Try to get error details from response
1246 - return responseClone.json().then(errorData => {
1247 - throw { isServerError: true, data: errorData };
1248 - }).catch(() => {
1249 - throw new Error('Network response was not ok');
1250 - });
503 + throw new Error('Network response was not ok');
1251 504 }
1252 505
1253 - // Check if response is JSON instead of streaming
1254 - const contentType = response.headers.get('content-type');
1255 - if (contentType && contentType.includes('application/json')) {
1256 - return responseClone.json().then(data => {
1257 - // IMMEDIATE CHAT MODE UPDATE for JSON response
1258 - if (data.chat_mode) {
1259 - updateChatModeIndicator(data.chat_mode, botId);
1260 - }
506 + // Check if response is JSON instead of streaming
507 + const contentType = response.headers.get('content-type');
508 + if (contentType && contentType.includes('application/json')) {
509 + //console.log("Received JSON response instead of stream, handling as regular response");
510 + return response.json().then(data => {
1261 511
1262 - // Check for testing panel
1263 - if (window.mxchatTestPanelInstance && data.testing_data) {
1264 - window.mxchatTestPanelInstance.handleTestingData(data.testing_data);
1265 - }
512 + // FIXED: Always check for testing panel, not just in testing mode
513 + if (window.mxchatTestPanelInstance && data.testing_data) {
514 + //console.log('Testing data found in streaming JSON response:', data.testing_data);
515 + window.mxchatTestPanelInstance.handleTestingData(data.testing_data);
516 + }
517 +
518 + // Handle as regular JSON response
519 + $('.bot-message.temporary-message').remove();
520 +
521 + // Handle different response formats (including intent responses)
522 + if (data.text || data.html || data.message) {
523 +
524 + // ADD RESPONSE HOOKS HERE - FOR STREAMING JSON RESPONSES
525 + if (data.text && typeof customMxChatFilter === 'function') {
526 + data.text = customMxChatFilter(data.text, "response");
527 + }
528 + if (data.message && typeof customMxChatFilter === 'function') {
529 + data.message = customMxChatFilter(data.message, "response");
530 + }
531 +
532 + if (data.text && data.html) {
533 + replaceLastMessage("bot", data.text, data.html);
534 + } else if (data.text) {
535 + replaceLastMessage("bot", data.text);
536 + } else if (data.html) {
537 + replaceLastMessage("bot", "", data.html);
538 + } else if (data.message) {
539 + replaceLastMessage("bot", data.message);
540 + }
541 + }
542 +
543 + // Handle other response properties
544 + if (data.chat_mode) {
545 + updateChatModeIndicator(data.chat_mode);
546 + }
547 +
548 + if (data.data && data.data.filename) {
549 + showActivePdf(data.data.filename);
550 + activePdfFile = data.data.filename;
551 + }
552 +
553 + if (callback) {
554 + callback(data.text || data.message || '');
555 + }
556 + });
557 + }
1266 558
1267 - // Handle the JSON response directly
1268 - handleNonStreamResponse(data, callback, botId);
1269 - return Promise.resolve(); // Prevent further processing
1270 - });
1271 - }
1272 -
1273 559 // Continue with streaming processing
1274 560 const reader = response.body.getReader();
1275 561 const decoder = new TextDecoder();
1276 562 let buffer = '';
@@ -1277,44 +563,9 @@
1277 563
1278 564 function processStream() {
1279 565 reader.read().then(({ done, value }) => {
1280 566 if (done) {
1281 - // If streaming completed but no content was received, try to get response as fallback
1282 - if (!streamingStarted || !accumulatedContent) {
1283 - // Try to read the response as JSON
1284 - responseClone.text().then(text => {
1285 - try {
1286 - const data = JSON.parse(text);
1287 - if (data.text || data.message || data.html) {
1288 - handleNonStreamResponse(data, callback, botId);
1289 - } else {
1290 - // No valid data, fall back to regular call
1291 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1292 - callMxChat(message, callback, botId);
1293 - }
1294 - } catch (e) {
1295 - // Could not parse, fall back to regular call
1296 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1297 - callMxChat(message, callback, botId);
1298 - }
1299 - }).catch(() => {
1300 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1301 - callMxChat(message, callback, botId);
1302 - });
1303 - return;
1304 - }
1305 -
1306 - // Re-enable chat input when stream ends with content
1307 - enableChatInput(botId);
1308 -
1309 - // Scroll the user's last message to the top now that the
1310 - // bot's full reply has rendered (gives max reading room).
1311 - var $chatBoxDone = getElement(botId, 'chat-box');
1312 - var $lastUserMsgDone = $chatBoxDone.find('.user-message').last();
1313 - if ($lastUserMsgDone.length) {
1314 - scrollElementToTop($lastUserMsgDone, botId);
1315 - }
1316 -
567 + //console.log("Streaming completed, final content:", accumulatedContent);
1317 568 if (callback) {
1318 569 callback(accumulatedContent);
1319 570 }
1320 571 return;
@@ -1328,34 +579,9 @@
1328 579 if (line.startsWith('data: ')) {
1329 580 const data = line.substring(6);
1330 581
1331 582 if (data === '[DONE]') {
1332 - if (!accumulatedContent) {
1333 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1334 - callMxChat(message, callback, botId);
1335 - return;
1336 - }
1337 -
1338 - // Re-enable chat input after streaming completes
1339 - enableChatInput(botId);
1340 -
1341 - // Render any server-pushed appendix html (e.g. the
1342 - // YouTube embed) as its own bot bubble below the
1343 - // streamed text — mirrors how it is saved in the
1344 - // transcript, so history replays identically.
1345 - if (pendingAppendHtml) {
1346 - appendMessage("bot", "", pendingAppendHtml, [], false, botId);
1347 - pendingAppendHtml = '';
1348 - }
1349 -
1350 - // Scroll the user's last message to the top now
1351 - // that the bot's full reply has rendered.
1352 - var $chatBoxStreamDone = getElement(botId, 'chat-box');
1353 - var $lastUserMsgStreamDone = $chatBoxStreamDone.find('.user-message').last();
1354 - if ($lastUserMsgStreamDone.length) {
1355 - scrollElementToTop($lastUserMsgStreamDone, botId);
1356 - }
1357 -
583 + //console.log("Received [DONE] signal");
1358 584 if (callback) {
1359 585 callback(accumulatedContent);
1360 586 }
1361 587 return;
@@ -1362,16 +588,12 @@
1362 588 }
1363 589
1364 590 try {
1365 591 const json = JSON.parse(data);
1366 -
1367 - // IMMEDIATE CHAT MODE UPDATE FOR STREAMING
1368 - if (json.chat_mode) {
1369 - updateChatModeIndicator(json.chat_mode, botId);
1370 - }
1371 -
1372 - // Handle testing data
592 +
593 + // FIXED: Always check for testing panel and handle testing data properly
1373 594 if (json.testing_data && !testingDataReceived) {
595 + //console.log('Testing data received in stream:', json.testing_data);
1374 596 if (window.mxchatTestPanelInstance) {
1375 597 window.mxchatTestPanelInstance.handleTestingData(json.testing_data);
1376 598 testingDataReceived = true;
1377 599 }
@@ -1377,1330 +599,359 @@
1377 599 }
1378 600 }
1379 601 // Handle content streaming
1380 602 else if (json.content) {
1381 - streamingStarted = true;
1382 603 accumulatedContent += json.content;
1383 - updateStreamingMessage(accumulatedContent, botId);
1384 - }
1385 - // Stash appendix html (e.g. video embed) for [DONE]
1386 - else if (json.append_html) {
1387 - pendingAppendHtml = json.append_html;
1388 - }
1389 - // Server-side final pass changed the assembled text
1390 - // (ffef6f: dead-link stripping) — swap the rendered
1391 - // bubble for the validated version. Arrives at most
1392 - // once, just before [DONE].
1393 - else if (json.replace_content) {
1394 - streamingStarted = true;
1395 - accumulatedContent = json.replace_content;
1396 - updateStreamingMessage(accumulatedContent, botId);
1397 - }
1398 - // Handle complete response in stream (fallback response)
1399 - else if (json.text || json.message || json.html) {
1400 - handleNonStreamResponse(json, callback, botId);
1401 - return;
1402 - }
604 + updateStreamingMessage(accumulatedContent);
605 + }
1403 606 // Handle errors
1404 607 else if (json.error) {
1405 -
1406 - // Get error message from various possible fields
1407 - let errorMessage = json.error_message || json.message || json.text ||
1408 - (typeof json.error === 'string' ? json.error : 'An error occurred. Please try again.');
1409 -
1410 - // Re-enable chat input on error
1411 - enableChatInput(botId);
1412 -
1413 - // Display the error directly in the chat
1414 - replaceLastMessage("bot", errorMessage, '', [], botId);
1415 -
1416 - if (callback) {
1417 - callback(errorMessage);
1418 - }
608 + //console.error("Streaming error:", json.error);
609 + replaceLastMessage("bot", "Error: " + json.error);
1419 610 return;
1420 611 }
1421 612 } catch (e) {
1422 - // SSE data parsing error - silently continue
613 + //console.error('Error parsing SSE data:', e, 'Data:', data);
1423 614 }
1424 615 }
1425 616 }
1426 617
1427 618 processStream();
1428 - }).catch(streamError => {
1429 - if (mxchatHandleStreamAbort(botId, accumulatedContent, callback)) return;
1430 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1431 - callMxChat(message, callback, botId);
1432 619 });
1433 620 }
1434 621
1435 622 processStream();
1436 623 })
1437 - .catch(error => {
1438 - if (mxchatHandleStreamAbort(botId, accumulatedContent, callback)) return;
1439 - // Check if we have server error data with chat mode
1440 - if (error && error.isServerError && error.data) {
1441 - // Check for chat mode in error data
1442 - if (error.data.chat_mode) {
1443 - updateChatModeIndicator(error.data.chat_mode, botId);
1444 - }
1445 -
1446 - handleNonStreamResponse(error.data, callback, botId);
1447 - } else {
1448 - // Only fall back to regular call if we don't have any response data
1449 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1450 - callMxChat(message, callback, botId);
1451 - }
1452 - });
1453 - }); // refreshNonceIfNeeded
624 + .catch(error => {
625 + //console.error('Streaming error:', error);
626 + callMxChat(message, callback);
627 + });
1454 628 }
1455 629
1456 -// Helper function to handle non-streaming responses
1457 -function handleNonStreamResponse(data, callback, botId) {
1458 - botId = botId || 'default';
1459 -
1460 - // IMMEDIATE CHAT MODE UPDATE FOR NON-STREAMING RESPONSES
1461 - if (data.chat_mode) {
1462 - updateChatModeIndicator(data.chat_mode, botId);
1463 - }
1464 -
1465 - // Also check in data property if response is wrapped
1466 - if (data.data && data.data.chat_mode) {
1467 - updateChatModeIndicator(data.data.chat_mode, botId);
1468 - }
1469 -
1470 - // NOTE: Don't remove temporary message here - let replaceLastMessage handle it
1471 - // This prevents a visual gap between thinking dots disappearing and content appearing
1472 -
1473 - // SECURITY FIX: Check for errors FIRST
1474 - if (data.success === false || (data.data && data.data.error_message)) {
1475 - let errorMessage = "";
1476 - let errorCode = "";
1477 -
1478 - // Check various possible error locations
1479 - if (data.data && data.data.error_message) {
1480 - errorMessage = data.data.error_message;
1481 - errorCode = data.data.error_code || "";
1482 - } else if (data.error_message) {
1483 - errorMessage = data.error_message;
1484 - errorCode = data.error_code || "";
1485 - } else if (data.message) {
1486 - errorMessage = data.message;
1487 - } else if (typeof data.data === 'string') {
1488 - errorMessage = data.data;
1489 - } else {
1490 - errorMessage = "An error occurred. Please try again or contact support.";
1491 - }
1492 -
1493 - // Handle session reset action (IP changed, session expired, etc.)
1494 - // Silent reset — keep chat UI intact, just get a new session and retry
1495 - if (data.data && data.data.action === 'reset_session') {
1496 - MxChatInstances.silentResetSession(botId);
1497 - // Re-send the original message with the new session (user message is already displayed)
1498 - var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message');
1499 - if (originalMessage) {
1500 - getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null);
1501 - var currentModel = mxchatChat.model || 'gpt-5.6-sol';
1502 - if (shouldUseStreaming(currentModel)) {
1503 - callMxChatStream(originalMessage, callback, botId);
1504 - } else {
1505 - callMxChat(originalMessage, callback, botId);
1506 - }
1507 - }
1508 - return;
1509 - }
1510 -
1511 - // Format user-friendly error message
1512 - let displayMessage = errorMessage;
1513 - if (mxchatChat.is_admin) {
1514 - displayMessage = errorMessage + (errorCode ? " (Error code: " + errorCode + ")" : "");
1515 - }
1516 -
1517 - replaceLastMessage("bot", displayMessage, '', [], botId);
1518 -
1519 - if (callback) {
1520 - callback('');
1521 - }
1522 - return; // Exit early for errors
1523 - }
1524 -
1525 - // Check for live agent response
1526 - if (data.success && data.data && data.data.status === 'waiting_for_agent') {
1527 - removeThinkingDots(botId);
1528 - // Also remove any leftover bot-message that lost its temporary-message class
1529 - var $chatBox = getElement(botId, 'chat-box');
1530 - $chatBox.find('.bot-message .thinking-dots').closest('.bot-message').remove();
1531 - updateChatModeIndicator('agent', botId);
1532 - enableChatInput(botId);
1533 - if (callback) {
1534 - callback('');
1535 - }
1536 - return;
1537 - }
1538 -
1539 - // Handle different response formats
1540 - if (data.text || data.html || data.message) {
1541 -
1542 - // Apply response hooks
1543 - if (data.text && typeof customMxChatFilter === 'function') {
1544 - data.text = customMxChatFilter(data.text, "response");
1545 - }
1546 - if (data.message && typeof customMxChatFilter === 'function') {
1547 - data.message = customMxChatFilter(data.message, "response");
1548 - }
1549 -
1550 - // Display the response
1551 - if (data.text && data.html) {
1552 - replaceLastMessage("bot", data.text, data.html, [], botId);
1553 - } else if (data.text) {
1554 - replaceLastMessage("bot", data.text, '', [], botId);
1555 - } else if (data.html) {
1556 - replaceLastMessage("bot", "", data.html, [], botId);
1557 - } else if (data.message) {
1558 - replaceLastMessage("bot", data.message, '', [], botId);
1559 - }
1560 - }
1561 -
1562 - // Handle other response properties
1563 - if (data.data && data.data.filename) {
1564 - showActivePdf(data.data.filename, botId);
1565 - var instance = MxChatInstances.get(botId);
1566 - instance.activePdfFile = data.data.filename;
1567 - }
1568 -
1569 - if (data.redirect_url) {
1570 - setTimeout(() => {
1571 - window.location.href = data.redirect_url;
1572 - }, 1500);
1573 - }
1574 -
1575 - // Ensure chat input is re-enabled (safety net for edge cases)
1576 - enableChatInput(botId);
1577 -
1578 - if (callback) {
1579 - callback(data.text || data.message || '');
1580 - }
1581 -}
1582 -
1583 -// Enhanced updateChatModeIndicator function for immediate DOM updates
1584 -function updateChatModeIndicator(mode, botId) {
1585 - botId = botId || 'default';
1586 - const indicator = getElementDOM(botId, 'chat-mode-indicator');
1587 - if (indicator) {
1588 - const oldText = indicator.textContent;
1589 -
1590 - if (mode === 'agent') {
1591 - indicator.textContent = 'Live Agent';
1592 - startPolling(botId);
1593 - } else {
1594 - // Everything else is AI mode
1595 - const customAiText = indicator.getAttribute('data-ai-text') || 'AI Agent';
1596 - indicator.textContent = customAiText;
1597 - stopPolling(botId);
1598 - }
1599 -
1600 - // Force immediate DOM update and reflow
1601 - if (oldText !== indicator.textContent) {
1602 - // Force a reflow to ensure the change is visible immediately
1603 - indicator.style.display = 'none';
1604 - indicator.offsetHeight; // Trigger reflow
1605 - indicator.style.display = '';
1606 -
1607 - // Double-check after a brief moment to ensure the change stuck
1608 - setTimeout(() => {
1609 - if (mode === 'agent' && indicator.textContent !== 'Live Agent') {
1610 - indicator.textContent = 'Live Agent';
1611 - } else if (mode !== 'agent' && indicator.textContent === 'Live Agent') {
1612 - const customAiText = indicator.getAttribute('data-ai-text') || 'AI Agent';
1613 - indicator.textContent = customAiText;
1614 - }
1615 - }, 50);
1616 - }
1617 - }
1618 -}
1619 -
1620 630 // Function to update message during streaming
1621 -function updateStreamingMessage(content, botId) {
1622 - botId = botId || 'default';
1623 -
631 +function updateStreamingMessage(content) {
1624 632 // ADD RESPONSE HOOK FOR REAL-TIME STREAMING
1625 633 if (typeof customMxChatFilter === 'function') {
1626 634 content = customMxChatFilter(content, "response");
1627 635 }
636 +
637 + const formattedContent = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(content))));
1628 638
1629 - const formattedContent = linkify(content);
639 + // Find the temporary message
640 + const tempMessage = $('.bot-message.temporary-message').last();
1630 641
1631 - // Find the temporary message in this bot's chat box
1632 - var $chatBox = getElement(botId, 'chat-box');
1633 - const tempMessage = $chatBox.find('.bot-message.temporary-message').last();
1634 -
1635 642 if (tempMessage.length) {
1636 - // aria-busy=true for the whole stream: the bubble is rewritten on
1637 - // every chunk, and without busy a polite live region announces those
1638 - // rewrites continuously. Flipped false once the reply is final, so
1639 - // assistive tech announces the completed message ONCE (plan 67f126).
1640 - if (tempMessage.attr('aria-busy') !== 'true') {
1641 - tempMessage.attr('aria-busy', 'true');
1642 - }
1643 643 // Update existing message
1644 644 tempMessage.html(formattedContent);
1645 645 } else {
1646 646 // Create new temporary message if it doesn't exist
1647 - appendMessage("bot", content, '', [], true, botId);
647 + appendMessage("bot", content, '', [], true);
1648 648 }
1649 649 }
1650 650
651 +// UPGRADE: Function to check if streaming is supported for the current model
1651 652 function isStreamingSupported(model) {
1652 653 if (!model) return false;
1653 654
655 + //console.log("Checking streaming support for model:", model); // Debug log
656 +
657 + // Get the model prefix
1654 658 const modelPrefix = model.split('-')[0].toLowerCase();
659 +
660 + //console.log("Model prefix:", modelPrefix); // Debug log
1655 661
1656 - // Support streaming for OpenAI, Claude, Grok, DeepSeek, and OpenRouter models
1657 - const isSupported = modelPrefix === 'gpt' ||
1658 - modelPrefix === 'o1' ||
1659 - modelPrefix === 'claude' ||
1660 - modelPrefix === 'grok' ||
1661 - modelPrefix === 'deepseek' ||
1662 - model === 'openrouter'; // Add this line - check full model name for OpenRouter
662 + // Support streaming for OpenAI, Claude, and Grok models
663 + const isSupported = modelPrefix === 'gpt' || modelPrefix === 'o1' || modelPrefix === 'claude' || modelPrefix === 'grok';
1663 664
665 + //console.log("Streaming supported:", isSupported); // Debug log
666 +
1664 667 return isSupported;
1665 668 }
1666 669
1667 -// Update the event handlers to use the correct function names (using event delegation)
1668 -// Use class-based selectors for multi-instance support
1669 -$(document).on('click', '.send-button', function() {
1670 - var botId = getBotIdFromElement(this);
1671 - // While a response is streaming the button is a Stop control.
1672 - if (this.classList.contains('mxchat-stop-mode')) {
1673 - // Same click that just started this stream (an add-on's direct handler
1674 - // ran before this delegated one) — not a Stop press. See
1675 - // mxchatShowStopButton for the full story (plan-4bba64).
1676 - if (this.__mxchatStopJustShown) return;
1677 - mxchatStopStreaming(botId);
1678 - return;
1679 - }
1680 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
1681 - if (!(modeIndicator && modeIndicator.textContent === 'Live Agent')) {
1682 - disableChatInput(botId);
1683 - }
1684 - sendMessage(botId);
670 +// Update the event handlers to use the correct function names
671 +$('#send-button').off('click').on('click', function() {
672 + sendMessage(); // Use the updated sendMessage function
1685 673 });
1686 674
1687 -// Override enter key handler (using event delegation)
1688 -$(document).on('keypress', '.chat-input', function(e) {
675 +// Override enter key handler
676 +$('#chat-input').off('keypress').on('keypress', function(e) {
1689 677 if (e.which == 13 && !e.shiftKey) {
1690 678 e.preventDefault();
1691 - var botId = getBotIdFromElement(this);
1692 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
1693 - if (!(modeIndicator && modeIndicator.textContent === 'Live Agent')) {
1694 - disableChatInput(botId);
1695 - }
1696 - sendMessage(botId);
679 + sendMessage(); // Use the updated sendMessage function
1697 680 }
1698 681 });
1699 682
1700 -// Chat input character counter + soft limit feedback (plan 7091a2).
1701 -// Language-neutral: numbers + color only, no translatable strings. The counter
1702 -// reveals near the cap and ramps neutral -> amber -> red; an over-limit keystroke
1703 -// or trimmed paste produces a brief border-flash/shake so the maxlength cap (plan
1704 -// a3fae2) is never a silent "input jumps back". Per-bot scoped via .input-container.
1705 -function mxchatUpdateCharCounter(inputEl) {
1706 - if (!inputEl || !inputEl.closest) return;
1707 - var max = parseInt(inputEl.getAttribute('maxlength'), 10);
1708 - var container = inputEl.closest('.input-container');
1709 - if (!container || !max || max <= 0) return;
1710 - var counter = container.querySelector('.mxchat-char-counter');
1711 - if (!counter) return;
1712 - var len = inputEl.value.length;
1713 - var ratio = len / max;
1714 - var nearThreshold = 0.8; // start surfacing the counter at 80% of the cap
1715 - var cur = counter.querySelector('.mxchat-char-counter-current');
1716 - if (cur) cur.textContent = len;
1717 - var warn = ratio >= nearThreshold && len < max;
1718 - var full = len >= max;
1719 - counter.classList.toggle('is-visible', ratio >= nearThreshold);
1720 - counter.classList.toggle('is-warn', warn);
1721 - counter.classList.toggle('is-full', full);
1722 - container.classList.toggle('mxchat-input-near-limit', warn);
1723 - container.classList.toggle('mxchat-input-at-limit', full);
1724 -}
1725 -
1726 -function mxchatBumpInput(inputEl) {
1727 - var container = inputEl && inputEl.closest ? inputEl.closest('.input-container') : null;
1728 - if (!container) return;
1729 - container.classList.remove('mxchat-input-bump');
1730 - void container.offsetWidth; // reflow so a rapid second hit retriggers the animation
1731 - container.classList.add('mxchat-input-bump');
1732 - clearTimeout($(container).data('mxchatBumpTimeout'));
1733 - var t = setTimeout(function() { container.classList.remove('mxchat-input-bump'); }, 220);
1734 - $(container).data('mxchatBumpTimeout', t);
1735 -}
1736 -
1737 -// Live counter update on every input.
1738 -$(document).on('input', '.chat-input', function() {
1739 - mxchatUpdateCharCounter(this);
1740 -});
1741 -
1742 -// Visible "you've hit the edge" feedback when a printable keystroke is about to be
1743 -// rejected at the cap (maxlength silently swallows it otherwise).
1744 -$(document).on('keydown', '.chat-input', function(e) {
1745 - var max = parseInt(this.getAttribute('maxlength'), 10);
1746 - if (!max || max <= 0 || this.value.length < max) return;
1747 - if (e.ctrlKey || e.metaKey || e.altKey) return;
1748 - // A single printable char with no selection to overwrite WILL be rejected.
1749 - if (e.key && e.key.length === 1 && this.selectionStart === this.selectionEnd) {
1750 - mxchatBumpInput(this);
1751 - }
1752 -});
1753 -
1754 -// A paste that gets trimmed to the cap also bumps, so truncation is never silent.
1755 -$(document).on('paste', '.chat-input', function() {
1756 - var el = this;
1757 - var max = parseInt(el.getAttribute('maxlength'), 10);
1758 - if (!max || max <= 0) return;
1759 - setTimeout(function() {
1760 - mxchatUpdateCharCounter(el);
1761 - if (el.value.length >= max) mxchatBumpInput(el);
1762 - }, 0);
1763 -});
1764 -
1765 -// Builds the list of overflow-menu items for a given bot.
1766 -// Adding a future item is one push to this array — do NOT hardcode "only download."
1767 -function mxchatGetHeaderMenuItems(botId) {
1768 - var items = [];
1769 - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {};
1770 -
1771 - // The `print_button_*` keys still gate this item for back-compat with
1772 - // existing user options. The action is now a transcript download, not print.
1773 - if (settings.print_button_enabled === 'on') {
1774 - items.push({
1775 - id: 'download-transcript',
1776 - label: settings.print_button_label || 'Download Transcript',
1777 - icon: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/></svg>',
1778 - action: function() {
1779 - mxchatDownloadTranscript(botId);
683 +
684 + function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false) {
685 + try {
686 + // Determine styles based on sender type
687 + let messageClass, bgColor, fontColor;
688 +
689 + if (sender === "user") {
690 + messageClass = "user-message";
691 + bgColor = userMessageBgColor;
692 + fontColor = userMessageFontColor;
693 + // Only sanitize user input
694 + messageText = sanitizeUserInput(messageText);
695 + } else if (sender === "agent") {
696 + messageClass = "agent-message";
697 + bgColor = liveAgentMessageBgColor;
698 + fontColor = liveAgentMessageFontColor;
699 + } else {
700 + messageClass = "bot-message";
701 + bgColor = botMessageBgColor;
702 + fontColor = botMessageFontColor;
1780 703 }
1781 - });
1782 - }
1783 -
1784 - // "Start new chat" — surfaces the EXISTING per-conversation reset
1785 - // (MxChatInstances.resetChatSession) so a visitor can start a fresh thread
1786 - // without the site owner disabling chat persistence globally. Default OFF;
1787 - // gated by the reset_chat_enabled option. plan ac2e81.
1788 - if (settings.reset_chat_enabled === 'on') {
1789 - items.push({
1790 - id: 'reset-chat',
1791 - label: settings.reset_chat_label || 'Start new chat',
1792 - icon: '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 2.13-9.36L1 10"/></svg>',
1793 - action: function() {
1794 - var confirmMsg = settings.reset_chat_confirm || 'Start a new chat? This clears the current conversation.';
1795 - if (window.confirm(confirmMsg)) {
1796 - MxChatInstances.resetChatSession(botId);
704 +
705 + const messageDiv = $('<div>')
706 + .addClass(messageClass)
707 + .attr('dir', 'auto') // Add dir="auto" for automatic text direction
708 + .css({
709 + 'background': bgColor,
710 + 'color': fontColor,
711 + 'margin-bottom': '1em'
712 + });
713 +
714 + // Process the message content based on sender
715 + let fullMessage;
716 + if (sender === "user") {
717 + // For user messages, apply linkify after sanitization
718 + fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(messageText))));
719 + } else {
720 + // For bot/agent messages, preserve HTML
721 + fullMessage = messageText;
722 + }
723 +
724 + // Add images if provided
725 + if (images && images.length > 0) {
726 + fullMessage += '<div class="image-gallery" dir="auto">'; // Add dir="auto" to image gallery
727 + images.forEach(img => {
728 + // Ensure image URLs and titles are properly escaped
729 + const safeTitle = sanitizeUserInput(img.title);
730 + const safeUrl = encodeURI(img.image_url);
731 + const safeThumbnail = encodeURI(img.thumbnail_url);
732 +
733 + fullMessage += `
734 + <div style="margin-bottom: 10px;">
735 + <strong>${safeTitle}</strong><br>
736 + <a href="${safeUrl}" target="_blank">
737 + <img src="${safeThumbnail}" alt="${safeTitle}" style="max-width: 100px; height: auto; margin: 5px;" />
738 + </a>
739 + </div>`;
740 + });
741 + fullMessage += '</div>';
742 + }
743 +
744 + // Append HTML content if provided
745 + if (messageHtml && sender !== "user") {
746 + fullMessage += '<br><br>' + messageHtml;
747 + }
748 +
749 + messageDiv.html(fullMessage);
750 +
751 + if (isTemporary) {
752 + messageDiv.addClass('temporary-message');
753 + }
754 +
755 + messageDiv.hide().appendTo('#chat-box').fadeIn(300, function() {
756 + if (sender === "bot") {
757 + const lastUserMessage = $('#chat-box').find('.user-message').last();
758 + if (lastUserMessage.length) {
759 + scrollElementToTop(lastUserMessage);
760 + }
1797 761 }
762 + });
763 +
764 + if (messageText.id) {
765 + lastSeenMessageId = messageText.id;
766 + hideNotification();
1798 767 }
1799 - });
1800 - }
1801 -
1802 - return items;
1803 -}
1804 -
1805 -// Builds a clean markdown transcript of the current conversation and triggers
1806 -// a file download. Used by the "Download Transcript" menu item.
1807 -function mxchatDownloadTranscript(botId) {
1808 - var $chatBox = getElement(botId, 'chat-box');
1809 - if (!$chatBox || !$chatBox.length) return;
1810 -
1811 - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {};
1812 - var headerTitle = settings.print_header_title || 'Chat transcript';
1813 - var now = new Date();
1814 - var stamp = now.toLocaleString();
1815 -
1816 - var lines = [];
1817 - lines.push('# ' + headerTitle);
1818 - lines.push('');
1819 - lines.push('Exported: ' + stamp);
1820 - lines.push('');
1821 - lines.push('---');
1822 - lines.push('');
1823 -
1824 - $chatBox.find('.user-message, .bot-message, .agent-message').each(function() {
1825 - var $msg = $(this);
1826 - // Skip thinking placeholders and any in-flight temporary messages.
1827 - if ($msg.find('.thinking-dots').length) return;
1828 - if ($msg.hasClass('temporary-message')) return;
1829 -
1830 - var sender;
1831 - if ($msg.hasClass('user-message')) sender = 'User';
1832 - else if ($msg.hasClass('agent-message')) sender = 'Live Agent';
1833 - else sender = 'AI Agent';
1834 -
1835 - // Strip interactive UI from the cloned message so we get the conversation text.
1836 - var $clone = $msg.clone();
1837 - $clone.find('.copy-button, .message-toolbar, .mxchat-copy, button, script, style').remove();
1838 - var text = $clone.text().replace(/ /g, ' ').replace(/[ \t]+\n/g, '\n').replace(/\n{3,}/g, '\n\n').trim();
1839 - if (!text) return;
1840 -
1841 - lines.push('**' + sender + '**');
1842 - lines.push('');
1843 - lines.push(text);
1844 - lines.push('');
1845 - });
1846 -
1847 - var content = lines.join('\n');
1848 - var iso = now.toISOString().replace(/[:.]/g, '-').slice(0, 19);
1849 - var fname = 'mxchat-transcript-' + iso + '.md';
1850 - var blob = new Blob([content], { type: 'text/markdown;charset=utf-8' });
1851 - var url = URL.createObjectURL(blob);
1852 - var a = document.createElement('a');
1853 - a.href = url;
1854 - a.download = fname;
1855 - a.style.display = 'none';
1856 - document.body.appendChild(a);
1857 - a.click();
1858 - setTimeout(function() {
1859 - if (a.parentNode) a.parentNode.removeChild(a);
1860 - URL.revokeObjectURL(url);
1861 - }, 100);
1862 -}
1863 -
1864 -// Reads the bot bubble's actual computed bg+fg and writes them as CSS vars
1865 -// on the menu wrap, so the dropdown matches whatever paints the bubble —
1866 -// saved options, AI theme CSS, or the mxchat-theme add-on.
1867 -function mxchatSyncMenuColors(botId, $wrap) {
1868 - if (!$wrap || !$wrap.length) return;
1869 - var $bot = $wrap.closest('.mxchat-chatbot-wrapper').find('.bot-message').not('.temporary-message').first();
1870 - if (!$bot.length) return;
1871 - var cs = window.getComputedStyle($bot[0]);
1872 - if (cs.backgroundColor && cs.backgroundColor !== 'rgba(0, 0, 0, 0)' && cs.backgroundColor !== 'transparent') {
1873 - $wrap[0].style.setProperty('--mxchat-menu-bg', cs.backgroundColor);
1874 - }
1875 - // Bot text color usually lives on a child div, not .bot-message itself.
1876 - var $textChild = $bot.find('[style*="color"]').first();
1877 - var fg = ($textChild.length ? window.getComputedStyle($textChild[0]).color : cs.color);
1878 - if (fg) $wrap[0].style.setProperty('--mxchat-menu-fg', fg);
1879 -}
1880 -
1881 -// Renders (or re-renders) the item list for one menu wrap. Split out of
1882 -// mxchatInitHeaderMenu so the dynamic-settings merge (plan-32db95) can
1883 -// rebuild items + trigger visibility WITHOUT re-binding the one-time
1884 -// open/close/keyboard wiring. closeMenu is passed in by the init closure;
1885 -// a rebuild before init (never happens, but harmless) just skips it.
1886 -function mxchatRenderHeaderMenuItems(botId, $wrap, closeMenuFn) {
1887 - var $trigger = $wrap.find('.mxchat-menu-trigger');
1888 - var $menu = $wrap.find('.mxchat-header-menu');
1889 - var items = mxchatGetHeaderMenuItems(botId);
1890 -
1891 - $menu.empty();
1892 -
1893 - if (!items.length) {
1894 - $trigger.hide();
1895 - $menu.hide();
1896 - return;
1897 - }
1898 -
1899 - // Clear any inline display:none a previous zero-item render left behind —
1900 - // open/close visibility is governed by the hidden prop + is-open class.
1901 - $trigger.css('display', '');
1902 - $menu.css('display', '');
1903 -
1904 - items.forEach(function(item, idx) {
1905 - var $btn = $('<button>', {
1906 - type: 'button',
1907 - 'class': 'mxchat-menu-item',
1908 - 'role': 'menuitem',
1909 - 'tabindex': '-1',
1910 - 'data-menu-id': item.id,
1911 - html: '<span class="mxchat-menu-item-icon">' + item.icon + '</span>' +
1912 - '<span class="mxchat-menu-item-label"></span>'
1913 - });
1914 - $btn.find('.mxchat-menu-item-label').text(item.label);
1915 - $btn.on('click', function(e) {
1916 - e.preventDefault();
1917 - e.stopPropagation();
1918 - if (closeMenuFn) closeMenuFn();
1919 - try { item.action(); } catch (err) { /* no-op */ }
1920 - });
1921 - $menu.append($btn);
1922 - });
1923 -}
1924 -
1925 -// Re-render every menu on the page after a dynamic-settings merge
1926 -// (multi-bot: each wrap re-reads its items). An OPEN menu is left alone —
1927 -// swapping items under the user mid-interaction yanks focus — and the
1928 -// rebuild runs when it closes instead (closeMenu checks the pending flag).
1929 -function mxchatRebuildHeaderMenus() {
1930 - $('.mxchat-header-menu-wrap').each(function() {
1931 - var $wrap = $(this);
1932 - var botId = $wrap.data('bot-id');
1933 - if (!botId) return;
1934 - if (!$wrap.data('mxchatMenuReady')) {
1935 - mxchatInitHeaderMenu(botId);
1936 - return;
768 + } catch (error) {
769 + //console.error("Error rendering message:", error);
1937 770 }
1938 - if ($wrap.find('.mxchat-header-menu').hasClass('is-open')) {
1939 - $wrap.data('mxchatMenuRebuildPending', true);
1940 - return;
1941 - }
1942 - mxchatRenderHeaderMenuItems(botId, $wrap, $wrap.data('mxchatMenuClose'));
1943 - });
1944 -}
1945 -
1946 -// One-time per-widget init: renders menu items, wires open/close,
1947 -// outside-click, Escape, and arrow-key navigation. If no items, hides the
1948 -// trigger. Wiring happens even when there are zero items at init, so a
1949 -// later dynamic-settings rebuild that adds items has a working trigger.
1950 -function mxchatInitHeaderMenu(botId) {
1951 - var $wrap = $('.mxchat-header-menu-wrap[data-bot-id="' + botId + '"]').first();
1952 - if (!$wrap.length || $wrap.data('mxchatMenuReady')) return;
1953 -
1954 - var $trigger = $wrap.find('.mxchat-menu-trigger');
1955 - var $menu = $wrap.find('.mxchat-header-menu');
1956 -
1957 - // Initial color sync — covers normal page load.
1958 - mxchatSyncMenuColors(botId, $wrap);
1959 -
1960 - function openMenu() {
1961 - // Re-sync each open in case the active theme changed since init.
1962 - mxchatSyncMenuColors(botId, $wrap);
1963 - $menu.prop('hidden', false).attr('aria-hidden', 'false').addClass('is-open');
1964 - $trigger.attr('aria-expanded', 'true');
1965 - // Focus the first item for keyboard users
1966 - setTimeout(function() {
1967 - $menu.find('.mxchat-menu-item').first().attr('tabindex', '0').trigger('focus');
1968 - }, 0);
1969 771 }
1970 - function closeMenu(returnFocus) {
1971 - $menu.prop('hidden', true).attr('aria-hidden', 'true').removeClass('is-open');
1972 - $trigger.attr('aria-expanded', 'false');
1973 - $menu.find('.mxchat-menu-item').attr('tabindex', '-1');
1974 - if (returnFocus) $trigger.trigger('focus');
1975 - // A dynamic-settings rebuild that arrived while the menu was open
1976 - // was deferred (mxchatRebuildHeaderMenus) — run it now.
1977 - if ($wrap.data('mxchatMenuRebuildPending')) {
1978 - $wrap.removeData('mxchatMenuRebuildPending');
1979 - mxchatRenderHeaderMenuItems(botId, $wrap, closeMenu);
1980 - }
1981 - }
1982 -
1983 - // Toggle on trigger click — stop propagation so the .chatbot-top-bar
1984 - // click-to-collapse handler does not fire.
1985 - $trigger.on('click', function(e) {
1986 - e.preventDefault();
1987 - e.stopPropagation();
1988 - if ($menu.hasClass('is-open')) closeMenu();
1989 - else openMenu();
1990 - });
1991 -
1992 - // Don't let clicks inside the menu bubble to the top-bar collapse handler.
1993 - $menu.on('click', function(e) {
1994 - e.stopPropagation();
1995 - });
1996 -
1997 - // Outside click closes the menu.
1998 - $(document).on('click.mxchatMenu-' + botId, function(e) {
1999 - if (!$menu.hasClass('is-open')) return;
2000 - if ($wrap.has(e.target).length || $wrap.is(e.target)) return;
2001 - closeMenu();
2002 - });
2003 -
2004 - // Keyboard: Escape closes and returns focus; arrow keys move focus; Enter activates.
2005 - $menu.on('keydown', '.mxchat-menu-item', function(e) {
2006 - var $items = $menu.find('.mxchat-menu-item');
2007 - var idx = $items.index(this);
2008 - if (e.key === 'Escape') {
2009 - e.preventDefault();
2010 - closeMenu(true);
2011 - } else if (e.key === 'ArrowDown') {
2012 - e.preventDefault();
2013 - var $next = $items.eq((idx + 1) % $items.length);
2014 - $items.attr('tabindex', '-1');
2015 - $next.attr('tabindex', '0').trigger('focus');
2016 - } else if (e.key === 'ArrowUp') {
2017 - e.preventDefault();
2018 - var $prev = $items.eq((idx - 1 + $items.length) % $items.length);
2019 - $items.attr('tabindex', '-1');
2020 - $prev.attr('tabindex', '0').trigger('focus');
2021 - } else if (e.key === 'Enter' || e.key === ' ') {
2022 - e.preventDefault();
2023 - $(this).trigger('click');
2024 - }
2025 - });
2026 - $trigger.on('keydown', function(e) {
2027 - if (e.key === 'Escape' && $menu.hasClass('is-open')) {
2028 - e.preventDefault();
2029 - closeMenu(true);
2030 - } else if ((e.key === 'ArrowDown' || e.key === 'Enter' || e.key === ' ') && !$menu.hasClass('is-open')) {
2031 - e.preventDefault();
2032 - openMenu();
2033 - }
2034 - });
2035 -
2036 - // Expose closeMenu for out-of-closure re-renders (mxchatRebuildHeaderMenus),
2037 - // then do the initial item render.
2038 - $wrap.data('mxchatMenuClose', closeMenu);
2039 - mxchatRenderHeaderMenuItems(botId, $wrap, closeMenu);
2040 -
2041 - $wrap.data('mxchatMenuReady', true);
2042 -}
2043 -
2044 -// Initialize header menus for every rendered widget on DOM ready.
2045 -$(function() {
2046 - $('.mxchat-header-menu-wrap').each(function() {
2047 - var botId = $(this).data('bot-id');
2048 - if (botId) mxchatInitHeaderMenu(botId);
2049 - });
2050 -
2051 - // Embedded (non-floating) widgets are open from the moment the page
2052 - // renders — refresh dynamic settings at init (plan-32db95). Floating
2053 - // widgets refresh on first launcher open instead.
2054 - var hasEmbeddedWidget = $('.mxchat-chatbot-wrapper').filter(function() {
2055 - return !$(this).closest('.floating-chatbot').length;
2056 - }).length > 0;
2057 - if (hasEmbeddedWidget) {
2058 - mxchatRefreshDynamicSettings();
2059 - }
2060 -});
2061 -
2062 -function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false, botId = 'default') {
2063 - try {
2064 - // Determine styles based on sender type
2065 - let messageClass, bgColor, fontColor;
2066 -
772 +
773 + function replaceLastMessage(sender, responseText, responseHtml = '', images = []) {
774 + var messageClass = sender === "user" ? "user-message" : sender === "agent" ? "agent-message" : "bot-message";
775 + var lastMessageDiv = $('#chat-box').find('.bot-message.temporary-message, .agent-message.temporary-message').last();
776 +
777 + // Determine styles
778 + let bgColor, fontColor;
2067 779 if (sender === "user") {
2068 - messageClass = "user-message";
2069 780 bgColor = userMessageBgColor;
2070 781 fontColor = userMessageFontColor;
2071 - // Only sanitize user input
2072 - messageText = sanitizeUserInput(messageText);
2073 782 } else if (sender === "agent") {
2074 - messageClass = "agent-message";
2075 783 bgColor = liveAgentMessageBgColor;
2076 784 fontColor = liveAgentMessageFontColor;
2077 785 } else {
2078 - messageClass = "bot-message";
2079 786 bgColor = botMessageBgColor;
2080 787 fontColor = botMessageFontColor;
2081 788 }
2082 -
2083 - const messageDiv = $('<div>')
2084 - .addClass(messageClass)
2085 - .attr('dir', 'auto');
2086 -
2087 - // Only apply inline colors if AI theme is not active (let CSS handle it)
2088 - var skipColors = shouldSkipInlineColors(botId);
2089 - if (skipColors) {
2090 - messageDiv.css({
2091 - 'margin-bottom': '1em'
2092 - });
2093 - } else {
2094 - messageDiv.css({
2095 - 'background': bgColor,
2096 - 'color': fontColor,
2097 - 'margin-bottom': '1em'
2098 - });
789 +
790 + var fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(responseText))));
791 + if (responseHtml) {
792 + fullMessage += '<br><br>' + responseHtml;
2099 793 }
2100 -
2101 - // Process the message content - always run linkify to convert markdown
2102 - // links and format text. linkify() handles existing HTML safely via
2103 - // negative lookaheads that skip URLs already inside <a> tags.
2104 - let fullMessage = linkify(messageText);
2105 -
2106 - // Add images if provided
2107 - if (images && images.length > 0) {
2108 - fullMessage += '<div class="image-gallery" dir="auto">';
794 +
795 + if (images.length > 0) {
796 + fullMessage += '<div class="image-gallery" dir="auto">'; // Add dir="auto" to image gallery
2109 797 images.forEach(img => {
2110 - const safeTitle = sanitizeUserInput(img.title);
2111 - const safeUrl = encodeURI(img.image_url);
2112 - const safeThumbnail = encodeURI(img.thumbnail_url);
2113 -
2114 798 fullMessage += `
2115 799 <div style="margin-bottom: 10px;">
2116 - <strong>${safeTitle}</strong><br>
2117 - <a href="${safeUrl}" target="_blank">
2118 - <img src="${safeThumbnail}" alt="${safeTitle}" style="max-width: 100px; height: auto; margin: 5px;" />
800 + <strong>${img.title}</strong><br>
801 + <a href="${img.image_url}" target="_blank">
802 + <img src="${img.thumbnail_url}" alt="${img.title}" style="max-width: 100px; height: auto; margin: 5px;" />
2119 803 </a>
2120 804 </div>`;
2121 805 });
2122 806 fullMessage += '</div>';
2123 807 }
2124 -
2125 - // Append HTML content if provided
2126 - if (messageHtml && sender !== "user") {
2127 - // Only add line breaks if there's actual text content before the HTML
2128 - if (fullMessage && fullMessage.trim()) {
2129 - fullMessage += '<br><br>' + messageHtml;
2130 - } else {
2131 - fullMessage = messageHtml;
2132 - }
2133 - }
2134 -
2135 - messageDiv.html(fullMessage);
2136 -
2137 - if (isTemporary) {
2138 - // In-flight bubble: hold aria-busy so the live region stays quiet
2139 - // until the content is finalized (plan 67f126).
2140 - messageDiv.addClass('temporary-message').attr('aria-busy', 'true');
2141 - }
2142 -
2143 - // Append to the correct chatbot instance's chat-box
2144 - var $chatBox = getElement(botId, 'chat-box');
2145 - messageDiv.hide().appendTo($chatBox).fadeIn(300, function() {
2146 - // FIXED: Use event delegation for link tracking
2147 - if (sender === "bot" || sender === "agent") {
2148 - attachLinkTracking(messageDiv, messageText, botId);
2149 - }
2150 -
2151 - if (sender === "bot") {
2152 - const lastUserMessage = $chatBox.find('.user-message').last();
2153 - if (lastUserMessage.length) {
2154 - scrollElementToTop(lastUserMessage, botId);
2155 - }
2156 - }
2157 -
2158 - if ((sender === "bot" || sender === "agent") && !isTemporary) {
2159 - if (typeof mxchatInitHeaderMenu === 'function') mxchatInitHeaderMenu(botId);
2160 - }
2161 - });
2162 -
2163 - if (messageText.id) {
2164 - var instance = MxChatInstances.get(botId);
2165 - instance.lastSeenMessageId = messageText.id;
2166 - hideNotification(botId);
2167 - }
2168 - } catch (error) {
2169 - // Error rendering message - silently continue
2170 - }
2171 -}
2172 -
2173 -// Helper function to attach link tracking with proper event handling
2174 -function attachLinkTracking(messageDiv, messageText, botId) {
2175 - botId = botId || 'default';
2176 - // Use a slight delay to ensure DOM is ready
2177 - setTimeout(function() {
2178 - const links = messageDiv.find('a[href]').not('[data-tracked]');
2179 -
2180 - links.each(function() {
2181 - const $link = $(this);
2182 - const originalHref = $link.attr('href');
2183 -
2184 - // Mark as tracked to avoid duplicate handlers
2185 - $link.attr('data-tracked', 'true');
2186 -
2187 - // Only track external URLs
2188 - if (originalHref && (originalHref.startsWith('http://') || originalHref.startsWith('https://'))) {
2189 - // Remove any existing click handlers first
2190 - $link.off('click.tracking');
2191 -
2192 - // Add new click handler with namespace
2193 - $link.on('click.tracking', function(e) {
2194 - e.preventDefault();
2195 - e.stopPropagation();
2196 -
2197 - const messageContext = typeof messageText === 'string'
2198 - ? messageText.substring(0, 200)
2199 - : '';
2200 -
2201 - // Track the click
2202 - $.ajax({
2203 - url: mxchatChat.ajax_url,
2204 - type: 'POST',
2205 - data: {
2206 - action: 'mxchat_track_url_click',
2207 - session_id: getChatSession(botId),
2208 - url: originalHref,
2209 - message_context: messageContext,
2210 - nonce: mxchatChat.nonce
2211 - },
2212 - complete: function() {
2213 - // Always redirect, even if tracking fails
2214 - if ($link.attr('target') === '_blank' || linkTarget === '_blank') {
2215 - window.open(originalHref, '_blank');
2216 - } else {
2217 - window.location.href = originalHref;
808 +
809 + if (lastMessageDiv.length) {
810 + lastMessageDiv.fadeOut(200, function() {
811 + $(this)
812 + .html(fullMessage)
813 + .removeClass('bot-message user-message')
814 + .addClass(messageClass)
815 + .attr('dir', 'auto') // Add dir="auto" for automatic text direction
816 + .css({
817 + 'background-color': bgColor,
818 + 'color': fontColor,
819 + })
820 + .removeClass('temporary-message')
821 + .fadeIn(200, function() {
822 + if (sender === "bot" || sender === "agent") {
823 + const lastUserMessage = $('#chat-box').find('.user-message').last();
824 + if (lastUserMessage.length) {
825 + scrollElementToTop(lastUserMessage);
2218 826 }
827 + // Show notification if chat is hidden
828 + if ($('#floating-chatbot').hasClass('hidden')) {
829 + showNotification();
830 + }
2219 831 }
2220 832 });
2221 -
2222 - return false; // Extra insurance to prevent default
2223 - });
2224 - }
2225 - });
2226 - }, 100); // Small delay to ensure DOM is ready
2227 -}
2228 -
2229 -function replaceLastMessage(sender, responseText, responseHtml = '', images = [], botId = 'default') {
2230 - var messageClass = sender === "user" ? "user-message" : sender === "agent" ? "agent-message" : "bot-message";
2231 - var $chatBox = getElement(botId, 'chat-box');
2232 - var lastMessageDiv = $chatBox.find('.bot-message.temporary-message, .agent-message.temporary-message').last();
2233 -
2234 - // Determine styles
2235 - let bgColor, fontColor;
2236 - if (sender === "user") {
2237 - bgColor = userMessageBgColor;
2238 - fontColor = userMessageFontColor;
2239 - } else if (sender === "agent") {
2240 - bgColor = liveAgentMessageBgColor;
2241 - fontColor = liveAgentMessageFontColor;
2242 - } else {
2243 - bgColor = botMessageBgColor;
2244 - fontColor = botMessageFontColor;
2245 - }
2246 -
2247 - // Always run linkify to convert markdown links and format text.
2248 - // linkify() already handles existing HTML (its URL patterns use negative lookaheads
2249 - // to avoid double-processing URLs that are already inside <a> tags).
2250 - var fullMessage = linkify(responseText);
2251 -
2252 - if (responseHtml) {
2253 - // Only add line breaks if there's actual text content before the HTML
2254 - if (fullMessage && fullMessage.trim()) {
2255 - fullMessage += '<br><br>' + responseHtml;
833 + });
2256 834 } else {
2257 - fullMessage = responseHtml;
835 + appendMessage(sender, responseText, responseHtml, images);
2258 836 }
2259 837 }
838 +
839 + function appendThinkingMessage() {
840 + // Remove any existing thinking dots first
841 + $('.thinking-dots').remove();
2260 842
2261 - if (images.length > 0) {
2262 - fullMessage += '<div class="image-gallery" dir="auto">';
2263 - images.forEach(img => {
2264 - fullMessage += `
2265 - <div style="margin-bottom: 10px;">
2266 - <strong>${img.title}</strong><br>
2267 - <a href="${img.image_url}" target="_blank">
2268 - <img src="${img.thumbnail_url}" alt="${img.title}" style="max-width: 100px; height: auto; margin: 5px;" />
2269 - </a>
2270 - </div>`;
2271 - });
2272 - fullMessage += '</div>';
2273 - }
2274 -
2275 - if (lastMessageDiv.length) {
2276 - // Replace content immediately to prevent visual gap between thinking dots and response
2277 - // aria-busy released AFTER the final content is set, so the live region
2278 - // announces the finished message once (plan 67f126).
2279 - lastMessageDiv
2280 - .html(fullMessage)
2281 - .removeClass('bot-message user-message temporary-message')
2282 - .addClass(messageClass)
2283 - .attr('dir', 'auto')
2284 - .attr('aria-busy', 'false');
2285 -
2286 - // Only apply inline colors if AI theme is not active (let CSS handle it)
2287 - var skipColors = mxchatChat.skip_inline_colors || shouldSkipInlineColors(botId);
2288 - if (!skipColors) {
2289 - lastMessageDiv.css({
2290 - 'background-color': bgColor,
2291 - 'color': fontColor,
2292 - });
2293 - }
2294 -
2295 - // Handle link tracking and scroll
2296 - if (sender === "bot" || sender === "agent") {
2297 - attachLinkTracking(lastMessageDiv, responseText, botId);
2298 -
2299 - const lastUserMessage = $chatBox.find('.user-message').last();
2300 - if (lastUserMessage.length) {
2301 - scrollElementToTop(lastUserMessage, botId);
2302 - }
2303 - // Show notification if chat is hidden
2304 - var $floatingChatbot = getElement(botId, 'floating-chatbot');
2305 - if ($floatingChatbot.hasClass('hidden')) {
2306 - showNotification(botId);
2307 - }
2308 - }
2309 -
2310 - // Re-enable chat input after response is displayed
2311 - enableChatInput(botId);
2312 -
2313 - if (sender === "bot" || sender === "agent") {
2314 - if (typeof mxchatInitHeaderMenu === 'function') mxchatInitHeaderMenu(botId);
2315 - }
2316 - } else {
2317 - appendMessage(sender, responseText, responseHtml, images, false, botId);
2318 - // Re-enable chat input after response is displayed
2319 - enableChatInput(botId);
2320 - }
2321 -}
2322 -
2323 -
2324 - function appendThinkingMessage(botId) {
2325 - botId = botId || 'default';
2326 -
2327 - // Don't show thinking dots in live agent mode - message is just forwarded to a human
2328 - var indicator = getElementDOM(botId, 'chat-mode-indicator');
2329 - if (indicator && indicator.textContent === 'Live Agent') {
2330 - return;
2331 - }
2332 -
2333 - var $chatBox = getElement(botId, 'chat-box');
2334 -
2335 - // Remove any existing thinking dots in this bot's chat first
2336 - $chatBox.find('.thinking-dots').remove();
2337 -
2338 - // Check if we should skip inline colors (AI theme is active)
2339 - var skipColors = shouldSkipInlineColors(botId);
2340 -
2341 843 // Retrieve the bot message font color and background color
2342 844 var botMessageFontColor = mxchatChat.bot_message_font_color;
2343 845 var botMessageBgColor = mxchatChat.bot_message_bg_color;
2344 846
2345 - // Build thinking dots HTML - skip inline colors if AI theme is active
2346 - // The dots are decorative; the sr-only span is what the live region
2347 - // announces for the waiting state (plan 67f126). Server-localized
2348 - // string — safe to inject (esc_html__ output, no user content).
2349 - var dotStyle = skipColors ? '' : ' style="background-color: ' + botMessageFontColor + ';"';
2350 - var srThinking = mxchatChat.thinking_announcement || 'Assistant is typing';
2351 - var thinkingHtml = '<span class="sr-only">' + srThinking + '</span>' +
2352 - '<div class="thinking-dots-container" aria-hidden="true">' +
847 +
848 + var thinkingHtml = '<div class="thinking-dots-container">' +
2353 849 '<div class="thinking-dots">' +
2354 - '<span class="dot"' + dotStyle + '></span>' +
2355 - '<span class="dot"' + dotStyle + '></span>' +
2356 - '<span class="dot"' + dotStyle + '></span>' +
850 + '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
851 + '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
852 + '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
2357 853 '</div>' +
2358 854 '</div>';
2359 855
2360 - // Append the thinking dots to this bot's chat container - skip inline colors if AI theme is active
2361 - var messageStyle = skipColors ? '' : ' style="background-color: ' + botMessageBgColor + '; color: ' + botMessageFontColor + ';"';
2362 - $chatBox.append('<div class="bot-message temporary-message"' + messageStyle + '>' + thinkingHtml + '</div>');
2363 - scrollToBottom(botId);
856 + // Append the thinking dots to the chat container (or within the temporary message div)
857 + $("#chat-box").append('<div class="bot-message temporary-message" style="background-color: ' + botMessageBgColor + ';">' + thinkingHtml + '</div>');
858 + scrollToBottom();
2364 859 }
860 +
861 + function removeThinkingDots() {
862 + $('.thinking-dots').closest('.temporary-message').remove();
863 + }
2365 864
2366 - function removeThinkingDots(botId) {
2367 - botId = botId || 'default';
2368 - var $chatBox = getElement(botId, 'chat-box');
2369 - // Remove by temporary-message class first, then fall back to any bot-message containing thinking dots
2370 - $chatBox.find('.thinking-dots').closest('.temporary-message').remove();
2371 - $chatBox.find('.bot-message .thinking-dots').closest('.bot-message').remove();
2372 - }
2373 865
2374 866 // ====================================
2375 867 // TEXT FORMATTING & PROCESSING
2376 868 // ====================================
2377 869
870 +
2378 871 function linkify(inputText) {
2379 - if (!inputText) {
2380 - return '';
2381 - }
872 + if (!inputText) return '';
873 +
874 + // Process markdown headers
875 + let processedText = formatMarkdownHeaders(inputText);
876 +
877 + // Process markdown links
878 + const markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s]+)\)/g;
879 + processedText = processedText.replace(markdownLinkPattern, (match, text, url) => {
880 + const safeUrl = encodeURI(url);
881 + const safeText = sanitizeUserInput(text);
882 + return `<a href="${safeUrl}" target="${linkTarget}">${safeText}</a>`;
883 + });
2382 884
2383 - // Helper function to check if URL is already encoded
2384 - function isUrlEncoded(url) {
2385 - // Check for % followed by exactly 2 hex digits
2386 - return /%[0-9a-fA-F]{2}/.test(url);
2387 - }
885 + // Process phone numbers (tel:)
886 + const phonePattern = /\[([^\]]+)\]\((tel:[\d+]+)\)/g;
887 + processedText = processedText.replace(phonePattern, (match, text, phone) => {
888 + const safePhone = encodeURI(phone);
889 + const safeText = sanitizeUserInput(text);
890 + return `<a href="${safePhone}">${safeText}</a>`;
891 + });
2388 892
2389 - // Helper function to safely encode URLs only if needed
2390 - function safeEncodeUrl(url) {
2391 - // If URL already contains encoded characters, return as-is
2392 - if (isUrlEncoded(url)) {
2393 - return url;
2394 - }
2395 - // Otherwise, encode it
2396 - return encodeURI(url);
2397 - }
2398 -
2399 - // Process markdown headers FIRST
2400 - let processedText = formatMarkdownHeaders(inputText);
2401 -
2402 - // Process text styling (bold, italic, strikethrough)
2403 - processedText = formatTextStyling(processedText);
2404 -
2405 - // Process code blocks BEFORE processing links
2406 - processedText = formatCodeBlocks(processedText);
2407 -
2408 - // Process markdown tables BEFORE converting newlines to paragraphs
2409 - processedText = formatMarkdownTables(processedText);
2410 -
2411 - // NOW convert to paragraphs
2412 - processedText = convertNewlinesToBreaks(processedText);
2413 -
2414 - // IMPORTANT: Handle citation-style brackets FIRST [URL]
2415 - // This prevents them from being processed as markdown links
2416 - // Match [URL] where URL is a complete URL in square brackets (common in AI citations)
2417 - processedText = processedText.replace(/\[(https?:\/\/[^\]]+)\]/g, (match, url) => {
2418 - // Clean the URL of any trailing punctuation
2419 - let cleanUrl = url.replace(/[.,;!?]+$/, '');
2420 - const safeUrl = safeEncodeUrl(cleanUrl);
2421 - // Return as a proper link without the brackets
2422 - return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
2423 - });
2424 -
2425 - // Process markdown links: [text](url) and [](url)
2426 - // Uses balanced parenthesis matching to handle URLs containing parens
2427 - // (e.g. PDF filenames with dates like (2025-08-28).pdf)
2428 - processedText = (function(input) {
2429 - var result = '';
2430 - var i = 0;
2431 - while (i < input.length) {
2432 - // Look for [ at current position
2433 - if (input[i] === '[') {
2434 - // Find closing ]
2435 - var closeBracket = input.indexOf(']', i + 1);
2436 - if (closeBracket === -1 || closeBracket + 1 >= input.length || input[closeBracket + 1] !== '(') {
2437 - result += input[i];
2438 - i++;
2439 - continue;
2440 - }
2441 - var linkText = input.substring(i + 1, closeBracket);
2442 - // Check if URL starts with http
2443 - var urlStart = closeBracket + 2;
2444 - if (!input.substring(urlStart).match(/^https?:\/\//)) {
2445 - result += input[i];
2446 - i++;
2447 - continue;
2448 - }
2449 - // Find balanced closing paren
2450 - var depth = 1;
2451 - var j = urlStart;
2452 - while (j < input.length && depth > 0) {
2453 - if (input[j] === '(') depth++;
2454 - else if (input[j] === ')') depth--;
2455 - if (depth > 0) j++;
2456 - }
2457 - if (depth !== 0) {
2458 - result += input[i];
2459 - i++;
2460 - continue;
2461 - }
2462 - var url = input.substring(urlStart, j);
2463 - var cleanUrl = url.replace(/[\].,;!?]+$/, '');
2464 - var encodedUrl = safeEncodeUrl(cleanUrl);
2465 - if (!linkText || !linkText.trim()) {
2466 - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + cleanUrl + '</a>';
2467 - } else {
2468 - var safeText = sanitizeUserInput(linkText);
2469 - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + safeText + '</a>';
2470 - }
2471 - i = j + 1; // Skip past the closing )
2472 - } else {
2473 - result += input[i];
2474 - i++;
2475 - }
2476 - }
2477 - return result;
2478 - })(processedText);
2479 -
2480 - // Process phone numbers: [text](tel:number)
2481 - const phonePattern = /\[([^\]]+)\]\((tel:[\d+\-\s()]+)\)/g;
2482 - processedText = processedText.replace(phonePattern, (match, text, phone) => {
2483 - const safePhone = safeEncodeUrl(phone);
2484 - const safeText = sanitizeUserInput(text);
2485 - return `<a href="${safePhone}">${safeText}</a>`;
2486 - });
2487 -
2488 - // Process mailto links: [text](mailto:email)
2489 - const mailtoPattern = /\[([^\]]+)\]\((mailto:[^\)]+)\)/g;
2490 - processedText = processedText.replace(mailtoPattern, (match, text, mailto) => {
2491 - const safeMailto = safeEncodeUrl(mailto);
2492 - const safeText = sanitizeUserInput(text);
2493 - return `<a href="${safeMailto}">${safeText}</a>`;
2494 - });
2495 -
2496 - // Process standalone URLs - but NOT if they're already in <a> tags or brackets
2497 - // Updated pattern to be more careful about what it matches
2498 - const urlPattern = /(^|[^">=\[\]])(https?:\/\/[^\s<"\[\]]+)(?![^<]*<\/a>)(?!\])/gim;
2499 - processedText = processedText.replace(urlPattern, (match, prefix, url) => {
2500 - // Extra check: make sure this isn't already linked
2501 - if (match.includes('href=') || match.includes('</a>')) {
2502 - return match;
2503 - }
893 + // Process standalone URLs
894 + const urlPattern = /(^|[^">])(https?:\/\/[^\s<]+)/gim;
895 + processedText = processedText.replace(urlPattern, (match, prefix, url) => {
896 + const safeUrl = encodeURI(url);
897 + return `${prefix}<a href="${safeUrl}" target="${linkTarget}">${url}</a>`;
898 + });
2504 899
2505 - // Clean trailing punctuation
2506 - let cleanUrl = url.replace(/[.,;!?)]+$/, '');
2507 - const safeUrl = safeEncodeUrl(cleanUrl);
2508 - return `${prefix}<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
2509 - });
2510 -
2511 - // Process www. URLs - but NOT if they're already in <a> tags or brackets
2512 - const wwwPattern = /(^|[^">/\[\]])(www\.[\S]+)(?![^<]*<\/a>)(?!\])/gim;
2513 - processedText = processedText.replace(wwwPattern, (match, prefix, url) => {
2514 - // Extra check: make sure this isn't already linked
2515 - if (match.includes('href=') || match.includes('</a>')) {
2516 - return match;
2517 - }
900 + // Process www. URLs
901 + const wwwPattern = /(^|[^">])(www\.[\S]+(\b|$))(?![^<]*<\/a>)/gim;
902 + processedText = processedText.replace(wwwPattern, (match, prefix, url) => {
903 + const safeUrl = encodeURI(`http://${url}`);
904 + return `${prefix}<a href="${safeUrl}" target="${linkTarget}">${url}</a>`;
905 + });
2518 906
2519 - // Clean trailing punctuation
2520 - let cleanUrl = url.replace(/[.,;!?)]+$/, '');
2521 - const safeUrl = safeEncodeUrl(`http://${cleanUrl}`);
2522 - return `${prefix}<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
2523 - });
907 + // Add this after your phone pattern
908 + const mailtoPattern = /\[([^\]]+)\]\((mailto:[^\)]+)\)/g;
909 + processedText = processedText.replace(mailtoPattern, (match, text, mailto) => {
910 + const safeMailto = encodeURI(mailto);
911 + const safeText = sanitizeUserInput(text);
912 + return `<a href="${safeMailto}">${safeText}</a>`;
913 + });
2524 914
2525 - return processedText;
2526 -}
915 + return processedText;
916 + }
2527 917
2528 918 function formatMarkdownHeaders(text) {
2529 919 // Handle h1 to h6 headers
2530 - return text.replace(/^(#{1,6})\s+(.+)$/gm, function(match, hashes, content) {
920 + return text.replace(/^(#{1,6})\s(.+)$/gm, function(match, hashes, content) {
2531 921 const level = hashes.length;
2532 - return `<h${level} class="chat-heading chat-heading-${level}">${content.trim()}</h${level}>`;
922 + return `<h${level} class="chat-heading">${content}</h${level}>`;
2533 923 });
2534 924 }
2535 925
2536 -function formatTextStyling(text) {
2537 - // IMPORTANT: Protect BOTH HTML href and Markdown URLs from formatting
2538 - const protectedSegments = [];
2539 - let protectedText = text;
2540 -
2541 - // Step 1a: Protect HTML href="..." attributes
2542 - protectedText = protectedText.replace(/href\s*=\s*["']([^"']+)["']/gi, function(match) {
2543 - const placeholder = `__PROTECTED_${protectedSegments.length}__`;
2544 - protectedSegments.push(match);
2545 - return placeholder;
2546 - });
2547 -
2548 - // Step 1b: Protect Markdown links [text](url)
2549 - // This is crucial - we need to protect the URLs in markdown format
2550 - protectedText = protectedText.replace(/\[([^\]]*)\]\(([^)]+)\)/g, function(match) {
2551 - const placeholder = `__PROTECTED_${protectedSegments.length}__`;
2552 - protectedSegments.push(match);
2553 - return placeholder;
2554 - });
2555 -
2556 - // Step 1c: Also protect bare URLs that might exist
2557 - protectedText = protectedText.replace(/(https?:\/\/[^\s<>"]+)/gi, function(match) {
2558 - const placeholder = `__PROTECTED_${protectedSegments.length}__`;
2559 - protectedSegments.push(match);
2560 - return placeholder;
2561 - });
2562 -
2563 - // Step 2: Now apply text styling to the protected text
2564 - // Handle bold text (**text**)
2565 - protectedText = protectedText.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
2566 -
2567 - // Handle italic text (*text* or _text_) - Safari-compatible (no lookbehind)
2568 - // Match single asterisks that aren't part of bold (**) by checking they're not followed/preceded by another *
2569 - protectedText = protectedText.replace(/(?!\*\*)\*([^*\n]+)\*(?!\*)/g, '<em>$1</em>');
2570 -
2571 - // Handle underscores for italic - Safari-compatible (no lookbehind)
2572 - // Exclude __PROTECTED_N__ placeholders by checking the content doesn't contain PROTECTED
2573 - protectedText = protectedText.replace(/(?!__)_((?!PROTECTED)[^_\n]+)_(?!_)/g, '<em>$1</em>');
2574 -
2575 - // Handle strikethrough (~~text~~)
2576 - protectedText = protectedText.replace(/~~(.*?)~~/g, '<del>$1</del>');
2577 -
2578 - // Step 3: Restore all protected segments
2579 - protectedSegments.forEach((original, index) => {
2580 - const placeholder = `__PROTECTED_${index}__`;
2581 - protectedText = protectedText.replace(placeholder, original);
2582 - });
2583 -
2584 - return protectedText;
2585 -}
2586 926 function formatBoldText(text) {
2587 - // This function is kept for compatibility but now uses formatTextStyling
2588 - return formatTextStyling(text);
927 + return text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
2589 928 }
2590 929
2591 -function convertNewlinesToBreaks(text) {
2592 - // Split the text into paragraphs (marked by double newlines or multiple <br> tags)
2593 - const paragraphs = text.split(/(?:\n\n|\<br\>\s*\<br\>)/g);
930 + function convertNewlinesToBreaks(text) {
931 + // Split the text into paragraphs (marked by double newlines or multiple <br> tags)
932 + const paragraphs = text.split(/(?:\n\n|\<br\>\s*\<br\>)/g);
933 +
934 + // Wrap each paragraph in <p> tags
935 + return paragraphs
936 + .map(para => `<p>${para.trim()}</p>`)
937 + .join('');
938 + }
2594 939
2595 - // Filter out empty paragraphs and wrap each paragraph in <p> tags
2596 - return paragraphs
2597 - .map(para => para.trim())
2598 - .filter(para => para.length > 0) // Remove empty paragraphs
2599 - .map(para => `<p>${para}</p>`)
2600 - .join('');
2601 -}
2602 940 function formatCodeBlocks(text) {
2603 - // Handle fenced code blocks with language specification (```language)
2604 - text = text.replace(/```(\w+)?\n?([\s\S]*?)```/g, (match, language, code) => {
2605 - const lang = language || 'text';
2606 - const escapedCode = escapeHtml(code.trim());
2607 - return `<div class="mxchat-code-block-container">
2608 - <div class="mxchat-code-header">
2609 - <span class="mxchat-code-language">${lang}</span>
2610 - <button class="mxchat-copy-button" aria-label="Copy to clipboard">Copy</button>
2611 - </div>
2612 - <pre class="mxchat-code-block"><code class="language-${lang}">${escapedCode}</code></pre>
2613 - </div>`;
941 + // First handle raw PHP tags
942 + text = text.replace(/(<\?php[\s\S]*?\?>)/g, (match) => {
943 + return `<pre><code class="language-php">${escapeHtml(match)}</code></pre>`;
2614 944 });
2615 -
2616 - // Handle inline code with single backticks
2617 - text = text.replace(/`([^`\n]+)`/g, '<code class="mxchat-inline-code">$1</code>');
2618 -
2619 - // Handle raw PHP tags (legacy support)
2620 - text = text.replace(/(<\?php[\s\S]*?\?>)/g, (match) => {
2621 - const escapedCode = escapeHtml(match);
2622 - return `<div class="mxchat-code-block-container">
2623 - <div class="mxchat-code-header">
2624 - <span class="mxchat-code-language">php</span>
2625 - <button class="mxchat-copy-button" aria-label="Copy to clipboard">Copy</button>
2626 - </div>
2627 - <pre class="mxchat-code-block"><code class="language-php">${escapedCode}</code></pre>
2628 - </div>`;
945 +
946 + // Then handle code blocks with backticks
947 + text = text.replace(/```php5?\n([\s\S]+?)```/gi, (match, code) => {
948 + return `<pre><code class="language-php">${escapeHtml(code)}</code></pre>`;
2629 949 });
2630 -
950 +
2631 951 return text;
2632 952 }
2633 -
2634 - function formatMarkdownTables(text) {
2635 - var lines = text.split('\n');
2636 - var result = [];
2637 - var i = 0;
2638 -
2639 - while (i < lines.length) {
2640 - // Check for a table: current line has pipes AND next line is a separator row
2641 - if (i + 1 < lines.length &&
2642 - lines[i].indexOf('|') !== -1 &&
2643 - /^\s*\|?[\s\-:]+(\|[\s\-:]+)+\|?\s*$/.test(lines[i + 1])) {
2644 -
2645 - var tableLines = [];
2646 - var headerLine = lines[i];
2647 - var separatorLine = lines[i + 1];
2648 - tableLines.push(headerLine);
2649 - tableLines.push(separatorLine);
2650 -
2651 - // Collect remaining table rows
2652 - var j = i + 2;
2653 - while (j < lines.length && lines[j].indexOf('|') !== -1 && lines[j].trim() !== '') {
2654 - tableLines.push(lines[j]);
2655 - j++;
2656 - }
2657 -
2658 - // Parse alignment from separator row
2659 - var sepCells = separatorLine.split('|').filter(function(c) { return c.trim() !== ''; });
2660 - var alignments = sepCells.map(function(cell) {
2661 - var trimmed = cell.trim();
2662 - if (trimmed.charAt(0) === ':' && trimmed.charAt(trimmed.length - 1) === ':') return 'center';
2663 - if (trimmed.charAt(trimmed.length - 1) === ':') return 'right';
2664 - return 'left';
2665 - });
2666 -
2667 - // Build HTML table
2668 - var html = '<div class="mxchat-table-wrapper"><table class="mxchat-table">';
2669 -
2670 - // Header row
2671 - var headerCells = tableLines[0].split('|').filter(function(c) { return c.trim() !== ''; });
2672 - html += '<thead><tr>';
2673 - headerCells.forEach(function(cell, idx) {
2674 - var align = alignments[idx] || 'left';
2675 - html += '<th style="text-align:' + align + '">' + cell.trim() + '</th>';
2676 - });
2677 - html += '</tr></thead>';
2678 -
2679 - // Body rows
2680 - html += '<tbody>';
2681 - for (var r = 2; r < tableLines.length; r++) {
2682 - var rowCells = tableLines[r].split('|').filter(function(c) { return c.trim() !== ''; });
2683 - html += '<tr>';
2684 - rowCells.forEach(function(cell, idx) {
2685 - var align = alignments[idx] || 'left';
2686 - html += '<td style="text-align:' + align + '">' + cell.trim() + '</td>';
2687 - });
2688 - html += '</tr>';
2689 - }
2690 - html += '</tbody></table></div>';
2691 -
2692 - result.push(html);
2693 - i = j;
2694 - } else {
2695 - result.push(lines[i]);
2696 - i++;
2697 - }
2698 - }
2699 -
2700 - return result.join('\n');
2701 - }
2702 -
953 +
2703 954 function sanitizeUserInput(text) {
2704 955 const div = document.createElement('div');
2705 956 div.textContent = text;
2706 957 return div.innerHTML;
@@ -2705,12 +956,12 @@
2705 956 div.textContent = text;
2706 957 return div.innerHTML;
2707 958 }
2708 959
960 +
2709 961 function escapeHtml(unsafe) {
2710 - // Skip escaping if it's already escaped or contains HTML code block markup
2711 - if (unsafe.includes('&lt;') || unsafe.includes('&gt;') ||
2712 - unsafe.includes('<pre><code') || unsafe.includes('</code></pre>')) {
962 + // First check if it's already a code block
963 + if (unsafe.includes('<pre><code') || unsafe.includes('</code></pre>')) {
2713 964 return unsafe;
2714 965 }
2715 966
2716 967 return unsafe
@@ -2726,25 +977,15 @@
2726 977 textArea.innerHTML = text;
2727 978 return textArea.value;
2728 979 }
2729 980
981 +
2730 982 // ====================================
2731 983 // UI & SCROLLING CONTROLS
2732 984 // ====================================
2733 985
2734 - function scrollToBottom(botIdOrInstant, instant) {
2735 - // Handle backward compatibility: scrollToBottom() or scrollToBottom(true/false)
2736 - var botId = 'default';
2737 - if (typeof botIdOrInstant === 'string') {
2738 - botId = botIdOrInstant;
2739 - instant = instant || false;
2740 - } else if (typeof botIdOrInstant === 'boolean') {
2741 - instant = botIdOrInstant;
2742 - } else {
2743 - instant = false;
2744 - }
2745 -
2746 - var chatBox = getElement(botId, 'chat-box');
986 + function scrollToBottom(instant = false) {
987 + var chatBox = $('#chat-box');
2747 988 if (instant) {
2748 989 // Instantly set the scroll position to the bottom
2749 990 chatBox.scrollTop(chatBox.prop("scrollHeight"));
2750 991 } else {
@@ -2753,15 +994,15 @@
2753 994 const scrollHeight = chatBox.prop("scrollHeight");
2754 995 const initialScroll = chatBox.scrollTop();
2755 996 const distance = scrollHeight - initialScroll;
2756 997 const duration = 500; // Duration in ms
2757 -
998 +
2758 999 function smoothScroll(timestamp) {
2759 1000 if (!start) start = timestamp;
2760 1001 const progress = timestamp - start;
2761 1002 const currentScroll = initialScroll + (distance * (progress / duration));
2762 1003 chatBox.scrollTop(currentScroll);
2763 -
1004 +
2764 1005 if (progress < duration) {
2765 1006 requestAnimationFrame(smoothScroll);
2766 1007 } else {
2767 1008 chatBox.scrollTop(scrollHeight); // Ensure it's exactly at the bottom
@@ -2766,37 +1007,33 @@
2766 1007 } else {
2767 1008 chatBox.scrollTop(scrollHeight); // Ensure it's exactly at the bottom
2768 1009 }
2769 1010 }
2770 -
1011 +
2771 1012 requestAnimationFrame(smoothScroll);
2772 1013 }
2773 1014 }
2774 -
2775 - function scrollElementToTop(element, botId, topOffset) {
2776 - botId = botId || 'default';
2777 - topOffset = (typeof topOffset === 'number') ? topOffset : 2;
2778 - var chatBox = getElement(botId, 'chat-box');
1015 +
1016 + function scrollElementToTop(element) {
1017 + var chatBox = $('#chat-box');
2779 1018 var elementTop = element.position().top + chatBox.scrollTop();
2780 - chatBox.animate({ scrollTop: Math.max(0, elementTop - topOffset) }, 500);
1019 + chatBox.animate({ scrollTop: elementTop }, 500);
2781 1020 }
2782 -
2783 - function showChatWidget(botId) {
2784 - botId = botId || 'default';
2785 - var $button = getElement(botId, 'floating-chatbot-button');
1021 +
1022 + function showChatWidget() {
2786 1023 // First ensure display is set
2787 - $button.css('display', 'flex');
1024 + $('#floating-chatbot-button').css('display', 'flex');
2788 1025 // Then handle the fade
2789 - $button.fadeTo(500, 1);
1026 + $('#floating-chatbot-button').fadeTo(500, 1);
2790 1027 // Force visibility
2791 - $button.removeClass('hidden');
1028 + $('#floating-chatbot-button').removeClass('hidden');
1029 + //console.log('Showing widget');
2792 1030 }
2793 -
2794 - function hideChatWidget(botId) {
2795 - botId = botId || 'default';
2796 - var $button = getElement(botId, 'floating-chatbot-button');
2797 - $button.css('display', 'none');
2798 - $button.addClass('hidden');
1031 +
1032 + function hideChatWidget() {
1033 + $('#floating-chatbot-button').css('display', 'none');
1034 + $('#floating-chatbot-button').addClass('hidden');
1035 + //console.log('Hiding widget');
2799 1036 }
2800 1037
2801 1038 function disableScroll() {
2802 1039 if (isMobile()) {
@@ -2825,15 +1062,18 @@
2825 1062 // NOTIFICATION SYSTEM
2826 1063 // ====================================
2827 1064
2828 1065 function createNotificationBadge() {
1066 + //console.log("Creating notification badge...");
2829 1067 const chatButton = document.getElementById('floating-chatbot-button');
2830 -
1068 + //console.log("Chat button found:", !!chatButton);
1069 +
2831 1070 if (!chatButton) return;
2832 -
1071 +
2833 1072 // Remove any existing badge first
2834 1073 const existingBadge = chatButton.querySelector('.chat-notification-badge');
2835 1074 if (existingBadge) {
1075 + //console.log("Removing existing badge");
2836 1076 existingBadge.remove();
2837 1077 }
2838 1078
2839 1079 notificationBadge = document.createElement('div');
@@ -2855,43 +1095,34 @@
2855 1095 chatButton.appendChild(notificationBadge);
2856 1096
2857 1097 }
2858 1098
2859 - function showNotification(botId) {
2860 - botId = botId || 'default';
2861 - const badge = getElementDOM(botId, 'chat-notification-badge');
2862 - var $floatingChatbot = getElement(botId, 'floating-chatbot');
2863 - if (badge && $floatingChatbot.hasClass('hidden')) {
1099 + function showNotification() {
1100 + const badge = document.getElementById('chat-notification-badge');
1101 + if (badge && $('#floating-chatbot').hasClass('hidden')) {
2864 1102 badge.style.display = 'block';
2865 1103 badge.textContent = '1';
2866 1104 }
2867 1105 }
2868 -
2869 - function hideNotification(botId) {
2870 - botId = botId || 'default';
2871 - const badge = getElementDOM(botId, 'chat-notification-badge');
1106 +
1107 + function hideNotification() {
1108 + const badge = document.getElementById('chat-notification-badge');
2872 1109 if (badge) {
2873 1110 badge.style.display = 'none';
2874 1111 }
2875 1112 }
2876 -
2877 - function startNotificationChecking(botId) {
2878 - botId = botId || 'default';
1113 +
1114 + function startNotificationChecking() {
2879 1115 const chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
2880 1116 if (!chatPersistenceEnabled) return;
2881 -
2882 - createNotificationBadge(botId);
2883 - var instance = MxChatInstances.get(botId);
2884 - instance.notificationCheckInterval = setInterval(function() {
2885 - checkForNewMessages(botId);
2886 - }, 30000); // Check every 30 seconds
1117 +
1118 + createNotificationBadge();
1119 + notificationCheckInterval = setInterval(checkForNewMessages, 30000); // Check every 30 seconds
2887 1120 }
2888 -
2889 - function stopNotificationChecking(botId) {
2890 - botId = botId || 'default';
2891 - var instance = MxChatInstances.get(botId);
2892 - if (instance.notificationCheckInterval) {
2893 - clearInterval(instance.notificationCheckInterval);
1121 +
1122 + function stopNotificationChecking() {
1123 + if (notificationCheckInterval) {
1124 + clearInterval(notificationCheckInterval);
2894 1125 }
2895 1126 }
2896 1127
2897 1128 function checkForNewMessages() {
@@ -2917,35 +1148,50 @@
2917 1148 });
2918 1149 }
2919 1150
2920 1151
2921 -// ====================================
2922 -// LIVE AGENT FUNCTIONALITY
2923 -// ====================================
2924 -
2925 -function startPolling(botId) {
2926 - botId = botId || 'default';
2927 - var instance = MxChatInstances.get(botId);
2928 - // Clear any existing interval first
2929 - stopPolling(botId);
2930 - instance.pollingInterval = setInterval(function() {
2931 - checkForAgentMessages(botId);
2932 - }, 5000);
2933 -}
2934 -
2935 -function stopPolling(botId) {
2936 - botId = botId || 'default';
2937 - var instance = MxChatInstances.get(botId);
2938 - if (instance.pollingInterval) {
2939 - clearInterval(instance.pollingInterval);
2940 - instance.pollingInterval = null;
1152 + // ====================================
1153 + // LIVE AGENT FUNCTIONALITY
1154 + // ====================================
1155 +
1156 + function updateChatModeIndicator(mode) {
1157 + const indicator = document.getElementById('chat-mode-indicator');
1158 + if (indicator) {
1159 + // For Live Agent, keep as is; for AI mode, use the customized text
1160 + if (mode === 'agent') {
1161 + indicator.textContent = 'Live Agent';
1162 + } else {
1163 + // Get the custom AI agent text from a data attribute we'll add to the element
1164 + const customAiText = indicator.getAttribute('data-ai-text') || 'AI Agent';
1165 + indicator.textContent = customAiText;
1166 + }
1167 + }
1168 + // Start or stop polling based on mode
1169 + if (mode === 'agent') {
1170 + startPolling();
1171 + } else {
1172 + stopPolling();
1173 + }
2941 1174 }
2942 -}
2943 -
2944 -function checkForAgentMessages(botId) {
2945 - botId = botId || 'default';
2946 - var instance = MxChatInstances.get(botId);
2947 - const sessionId = getChatSession(botId);
1175 +
1176 + function startPolling() {
1177 + // Clear any existing interval first
1178 + stopPolling();
1179 + // Start new polling interval
1180 + pollingInterval = setInterval(checkForAgentMessages, 5000);
1181 + //console.log("Started agent message polling");
1182 + }
1183 +
1184 + function stopPolling() {
1185 + if (pollingInterval) {
1186 + clearInterval(pollingInterval);
1187 + pollingInterval = null;
1188 + //console.log("Stopped agent message polling");
1189 + }
1190 + }
1191 +
1192 +function checkForAgentMessages() {
1193 + const sessionId = getChatSession();
2948 1194 $.ajax({
2949 1195 url: mxchatChat.ajax_url,
2950 1196 type: 'POST',
2951 1197 dataType: 'json',
@@ -2951,44 +1197,35 @@
2951 1197 dataType: 'json',
2952 1198 data: {
2953 1199 action: 'mxchat_fetch_new_messages',
2954 1200 session_id: sessionId,
2955 - last_seen_id: instance.lastSeenMessageId,
2956 - persistence_enabled: 'true',
1201 + last_seen_id: lastSeenMessageId,
1202 + persistence_enabled: 'true', // Add this too
2957 1203 nonce: mxchatChat.nonce
2958 1204 },
2959 1205 success: function (response) {
2960 1206 if (response.success && response.data?.new_messages) {
2961 1207 let hasNewMessage = false;
2962 -
1208 +
2963 1209 response.data.new_messages.forEach(function (message) {
2964 - if (message.role === "agent" && !instance.processedMessageIds.has(message.id)) {
1210 + if (message.role === "agent" && !processedMessageIds.has(message.id)) {
2965 1211 hasNewMessage = true;
2966 - appendMessage("agent", message.content, '', [], false, botId);
2967 - instance.lastSeenMessageId = message.id;
2968 - instance.processedMessageIds.add(message.id);
1212 + // CHANGE THIS LINE:
1213 + appendMessage("agent", message.content); // Instead of replaceLastMessage
1214 + lastSeenMessageId = message.id;
1215 + processedMessageIds.add(message.id);
2969 1216 }
2970 1217 });
2971 1218
2972 - if (hasNewMessage) {
2973 - enableChatInput(botId);
1219 + if (hasNewMessage && $('#floating-chatbot').hasClass('hidden')) {
1220 + showNotification();
2974 1221 }
2975 -
2976 - var $floatingChatbot = getElement(botId, 'floating-chatbot');
2977 - if (hasNewMessage && $floatingChatbot.hasClass('hidden')) {
2978 - showNotification(botId);
2979 - }
2980 -
2981 - scrollToBottom(botId, true);
1222 +
1223 + scrollToBottom(true);
2982 1224 }
2983 -
2984 - // Handle chat mode transitions (e.g. agent ended chat via !endchat)
2985 - if (response.success && response.data?.chat_mode) {
2986 - updateChatModeIndicator(response.data.chat_mode, botId);
2987 - }
2988 1225 },
2989 1226 error: function (xhr, status, error) {
2990 - // Polling error - silently continue
1227 + //console.error("Polling error:", xhr, status, error);
2991 1228 }
2992 1229 });
2993 1230 }
2994 1231
@@ -2994,208 +1231,120 @@
2994 1231
2995 1232 // ====================================
2996 1233 // CHAT HISTORY & PERSISTENCE
2997 1234 // ====================================
2998 -
2999 -function loadChatHistory(botId, onComplete) {
3000 - botId = botId || 'default';
3001 - var instance = MxChatInstances.get(botId);
3002 -
3003 - // Prevent duplicate loading
3004 - if (instance.chatHistoryLoaded) {
3005 - if (onComplete) onComplete();
3006 - return;
3007 - }
3008 -
3009 - // Use getChatSession which returns null if no session exists (does NOT create one)
3010 - var sessionId = getChatSession(botId);
3011 - var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
3012 -
3013 - // No session yet — nothing to load. History will load after first message via ensureSession.
3014 - if (!sessionId) {
3015 - instance.chatHistoryLoaded = true;
3016 - if (onComplete) onComplete();
3017 - return;
3018 - }
3019 -
3020 - if (chatPersistenceEnabled && sessionId) {
3021 - $.ajax({
3022 - url: mxchatChat.ajax_url,
3023 - type: 'POST',
3024 - dataType: 'json',
3025 - data: {
3026 - action: 'mxchat_fetch_conversation_history',
3027 - session_id: sessionId
3028 - },
3029 - success: function(response) {
3030 - // Handle session reset (IP changed while user was away)
3031 - if (response.success === false && response.data && response.data.action === 'reset_session') {
3032 - // Silent reset — new session but don't clear UI
3033 - MxChatInstances.silentResetSession(botId);
3034 - instance.chatHistoryLoaded = true; // Prevent retry loop
3035 - if (onComplete) onComplete();
3036 - return;
3037 - }
3038 -
3039 - // Check if the response indicates success
3040 - if (response.success) {
3041 - // Handle case where conversation data exists and is an array
3042 - if (response.data && Array.isArray(response.data.conversation)) {
3043 - var $chatBox = getElement(botId, 'chat-box');
1235 +
1236 + function loadChatHistory() {
1237 + var sessionId = getChatSession();
1238 + var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
1239 +
1240 + if (chatPersistenceEnabled && sessionId) {
1241 + $.ajax({
1242 + url: mxchatChat.ajax_url,
1243 + type: 'POST',
1244 + dataType: 'json',
1245 + data: {
1246 + action: 'mxchat_fetch_conversation_history',
1247 + session_id: sessionId
1248 + },
1249 + success: function(response) {
1250 + if (response.success && response.data && Array.isArray(response.data.conversation)) {
1251 +
1252 +
1253 + var $chatBox = $('#chat-box');
3044 1254 var $fragment = $(document.createDocumentFragment());
3045 - let highestMessageId = instance.lastSeenMessageId;
3046 -
3047 - // Update chat mode if provided
1255 + let highestMessageId = lastSeenMessageId;
1256 +
3048 1257 if (response.data.chat_mode) {
3049 - updateChatModeIndicator(response.data.chat_mode, botId);
1258 + updateChatModeIndicator(response.data.chat_mode);
3050 1259 }
3051 -
3052 - // Only process if there are actual messages
3053 - if (response.data.conversation.length > 0) {
3054 - // Restored history must be SILENT to screen readers
3055 - // (plan 67f126): these are DOM additions inside the
3056 - // live region and would otherwise announce as if
3057 - // they just arrived. Lift aria-live for the batch
3058 - // repopulate, restore it after the browser has
3059 - // processed the mutations.
3060 - var mxLiveRegionEl = $chatBox.get(0);
3061 - var mxSavedAriaLive = mxLiveRegionEl ? mxLiveRegionEl.getAttribute('aria-live') : null;
3062 - if (mxLiveRegionEl) {
3063 - mxLiveRegionEl.setAttribute('aria-live', 'off');
1260 +
1261 + $.each(response.data.conversation, function(index, message) {
1262 + // Skip agent messages if persistence is off
1263 + if (!chatPersistenceEnabled && message.role === 'agent') {
1264 + return;
3064 1265 }
3065 -
3066 - // IMPORTANT: Clear existing messages before loading history.
3067 - // Detach the greeting first and put it back below —
3068 - // it is server-rendered and never stored in the
3069 - // transcript, so the old unconditional .empty()
3070 - // deleted it for the rest of the page life (plan
3071 - // a1a79b). Detach rather than rebuild: intro_message
3072 - // is not localized to JS, and this node already
3073 - // carries the per-bot inline colors and any
3074 - // {visitor_name} substitution already applied to it.
3075 - var $mxIntro = $chatBox.find('.mxchat-intro-message').first().detach();
3076 - $chatBox.empty();
3077 -
3078 - $.each(response.data.conversation, function(index, message) {
3079 - // Skip agent messages if persistence is off
3080 - if (!chatPersistenceEnabled && message.role === 'agent') {
3081 - return;
3082 - }
3083 -
3084 - var messageClass, messageBgColor, messageFontColor;
3085 -
3086 - switch (message.role) {
3087 - case 'user':
3088 - messageClass = 'user-message';
3089 - messageBgColor = userMessageBgColor;
3090 - messageFontColor = userMessageFontColor;
3091 - break;
3092 - case 'agent':
3093 - messageClass = 'agent-message';
3094 - messageBgColor = liveAgentMessageBgColor;
3095 - messageFontColor = liveAgentMessageFontColor;
3096 - break;
3097 - default:
3098 - messageClass = 'bot-message';
3099 - messageBgColor = botMessageBgColor;
3100 - messageFontColor = botMessageFontColor;
3101 - break;
3102 - }
3103 -
3104 - var messageElement = $('<div>').addClass(messageClass)
3105 - .css({
3106 - 'background': messageBgColor,
3107 - 'color': messageFontColor
3108 - });
3109 -
3110 - var content = message.content;
3111 - content = content.replace(/\\'/g, "'").replace(/\\"/g, '"');
3112 - content = decodeHTMLEntities(content);
3113 -
3114 - // Skip linkify for messages containing structured HTML
3115 - // (forms, product cards, galleries, etc.) to avoid
3116 - // markdown formatting corrupting HTML attributes
3117 - // (e.g. underscores in name="field_name" becoming <em> tags).
3118 - // One family check instead of a per-card literal list: any
3119 - // element carrying an mxchat- prefixed class is MxChat-generated
3120 - // structured markup and replays raw. The old list drifted every
3121 - // time an add-on minted a new card class — the filtered-search
3122 - // card ("mxchat-filtered-product-card") missed it and replayed
3123 - // through linkify as visible markup.
3124 - if (/<[a-z][^>]*class\s*=\s*["'][^"']*\bmxchat-/i.test(content) ||
3125 - content.includes("<form") ||
3126 - content.includes("<input") ||
3127 - content.includes("<select") ||
3128 - content.includes("<textarea")) {
3129 - messageElement.html(content);
3130 - } else {
3131 - var formattedContent = linkify(content);
3132 - messageElement.html(formattedContent);
3133 - }
3134 -
3135 - $fragment.append(messageElement);
3136 -
3137 - // Track message IDs
1266 +
1267 + var messageClass, messageBgColor, messageFontColor;
1268 +
1269 + switch (message.role) {
1270 + case 'user':
1271 + messageClass = 'user-message';
1272 + messageBgColor = userMessageBgColor;
1273 + messageFontColor = userMessageFontColor;
1274 + break;
1275 + case 'agent':
1276 + messageClass = 'agent-message';
1277 + messageBgColor = liveAgentMessageBgColor;
1278 + messageFontColor = liveAgentMessageFontColor;
1279 + break;
1280 + default:
1281 + messageClass = 'bot-message';
1282 + messageBgColor = botMessageBgColor;
1283 + messageFontColor = botMessageFontColor;
1284 + break;
1285 + }
1286 +
1287 + var messageElement = $('<div>').addClass(messageClass)
1288 + .css({
1289 + 'background': messageBgColor,
1290 + 'color': messageFontColor
1291 + });
1292 +
1293 + var content = message.content;
1294 + content = content.replace(/\\'/g, "'").replace(/\\"/g, '"');
1295 + content = decodeHTMLEntities(content);
1296 +
1297 + if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) {
1298 + messageElement.html(content);
1299 + } else {
1300 + var formattedContent = linkify(
1301 + formatBoldText(
1302 + convertNewlinesToBreaks(formatCodeBlocks(content))
1303 + )
1304 + );
1305 + messageElement.html(formattedContent);
1306 + }
1307 +
1308 + $fragment.append(messageElement);
1309 +
1310 + // In loadChatHistory, change this part:
3138 1311 if (message.id) {
3139 1312 highestMessageId = Math.max(highestMessageId, message.id);
3140 - instance.processedMessageIds.add(message.id);
1313 + processedMessageIds.add(message.id); // Add all message IDs to processed set
3141 1314 }
3142 - });
3143 -
3144 - // Only append messages and scroll if we have content.
3145 - // Greeting goes back FIRST, above the restored
3146 - // history: "Hello — [earlier conversation]" is the
3147 - // natural reading and matches the order a fresh
3148 - // visitor sees (plan a1a79b).
3149 - if ($mxIntro && $mxIntro.length) {
3150 - $chatBox.append($mxIntro);
1315 + });
1316 +
1317 + $chatBox.append($fragment);
1318 + scrollToBottom(true);
1319 +
1320 + if (response.data.conversation.length > 0 && hasQuickQuestions()) {
1321 + collapseQuickQuestions();
1322 + }
1323 +
1324 + // Update lastSeenMessageId after history loads
1325 + lastSeenMessageId = highestMessageId;
1326 +
1327 + // Only update chat mode if persistence is enabled
1328 + if (chatPersistenceEnabled && response.data.conversation.length > 0) {
1329 + var lastMessage = response.data.conversation[response.data.conversation.length - 1];
1330 + if (lastMessage.role === 'agent') {
1331 + updateChatModeIndicator('agent');
3151 1332 }
3152 - $chatBox.append($fragment);
3153 - scrollToBottom(botId, true);
3154 -
3155 - // Re-attach live semantics AFTER the rehydration
3156 - // mutations have been processed with the region off
3157 - // (plan 67f126). Restoring later announces nothing
3158 - // retroactively; new turns announce normally.
3159 - if (mxLiveRegionEl) {
3160 - setTimeout(function() {
3161 - mxLiveRegionEl.setAttribute('aria-live', mxSavedAriaLive || 'polite');
3162 - }, 200);
3163 - }
3164 -
3165 - // Collapse quick questions if we have conversation history
3166 - // BUT skip auto-collapse for embedded bots (they should stay expanded)
3167 - if (hasQuickQuestions(botId) && !isEmbeddedBot(botId)) {
3168 - collapseQuickQuestions(botId);
3169 - }
3170 -
3171 - // Update lastSeenMessageId after history loads
3172 - instance.lastSeenMessageId = highestMessageId;
3173 -
3174 - // Only update chat mode if persistence is enabled and we have messages
3175 - if (chatPersistenceEnabled) {
3176 - var lastMessage = response.data.conversation[response.data.conversation.length - 1];
3177 - if (lastMessage.role === 'agent') {
3178 - updateChatModeIndicator('agent', botId);
3179 - }
3180 - }
3181 -
3182 - // Mark as loaded ONLY after successful load
3183 - instance.chatHistoryLoaded = true;
3184 1333 }
1334 + } else {
1335 + console.warn("No conversation history found.");
3185 1336 }
1337 + },
1338 + error: function(xhr, status, error) {
1339 + //console.error("Error loading chat history:", status, error);
1340 + appendMessage("bot", "Unable to load chat history.");
3186 1341 }
3187 - if (onComplete) onComplete();
3188 - },
3189 - error: function(xhr, status, error) {
3190 - // Error loading chat history - silently continue
3191 - if (onComplete) onComplete();
3192 - }
3193 - });
3194 - } else {
3195 - if (onComplete) onComplete();
1342 + });
1343 + } else {
1344 + console.warn("Chat persistence is disabled or no session ID found. Not loading history.");
1345 + }
3196 1346 }
3197 -}
3198 1347
3199 1348
3200 1349 // ====================================
3201 1350 // FILE UPLOAD FUNCTIONALITY
@@ -3207,42 +1356,40 @@
3207 1356 element.addEventListener(eventType, handler);
3208 1357 }
3209 1358 }
3210 1359
3211 - function showActivePdf(filename, botId) {
3212 - botId = botId || 'default';
3213 - const container = getElementDOM(botId, 'active-pdf-container');
3214 - const nameElement = getElementDOM(botId, 'active-pdf-name');
3215 -
1360 + function showActivePdf(filename) {
1361 + const container = document.getElementById('active-pdf-container');
1362 + const nameElement = document.getElementById('active-pdf-name');
1363 +
3216 1364 if (!container || !nameElement) {
1365 + //console.error('PDF container elements not found');
3217 1366 return;
3218 1367 }
3219 -
1368 +
3220 1369 nameElement.textContent = filename;
3221 1370 container.style.display = 'flex';
3222 1371 }
3223 -
3224 - function showActiveWord(filename, botId) {
3225 - botId = botId || 'default';
3226 - const container = getElementDOM(botId, 'active-word-container');
3227 - const nameElement = getElementDOM(botId, 'active-word-name');
3228 -
1372 +
1373 + function showActiveWord(filename) {
1374 + const container = document.getElementById('active-word-container');
1375 + const nameElement = document.getElementById('active-word-name');
1376 +
3229 1377 if (!container || !nameElement) {
1378 + //console.error('Word document container elements not found');
3230 1379 return;
3231 1380 }
3232 -
1381 +
3233 1382 nameElement.textContent = filename;
3234 1383 container.style.display = 'flex';
3235 1384 }
3236 -
3237 - function removeActivePdf(botId) {
3238 - botId = botId || 'default';
3239 - var instance = MxChatInstances.get(botId);
3240 - const container = getElementDOM(botId, 'active-pdf-container');
3241 - const nameElement = getElementDOM(botId, 'active-pdf-name');
3242 -
3243 - if (!container || !nameElement || !instance.activePdfFile) return;
3244 -
1385 +
1386 + function removeActivePdf() {
1387 + const container = document.getElementById('active-pdf-container');
1388 + const nameElement = document.getElementById('active-pdf-name');
1389 +
1390 + if (!container || !nameElement || !activePdfFile) return;
1391 +
3245 1392 fetch(mxchatChat.ajax_url, {
3246 1393 method: 'POST',
3247 1394 headers: {
3248 1395 'Content-Type': 'application/x-www-form-urlencoded',
@@ -3248,9 +1395,9 @@
3248 1395 'Content-Type': 'application/x-www-form-urlencoded',
3249 1396 },
3250 1397 body: new URLSearchParams({
3251 1398 'action': 'mxchat_remove_pdf',
3252 - 'session_id': getChatSession(botId),
1399 + 'session_id': sessionId,
3253 1400 'nonce': mxchatChat.nonce
3254 1401 })
3255 1402 })
3256 1403 .then(response => response.json())
@@ -3257,25 +1404,23 @@
3257 1404 .then(data => {
3258 1405 if (data.success) {
3259 1406 container.style.display = 'none';
3260 1407 nameElement.textContent = '';
3261 - instance.activePdfFile = null;
3262 - appendMessage('bot', 'PDF removed.', '', [], false, botId);
1408 + activePdfFile = null;
1409 + appendMessage('bot', 'PDF removed.');
3263 1410 }
3264 1411 })
3265 1412 .catch(error => {
3266 - // Error removing PDF - silently continue
1413 + //console.error('Error removing PDF:', error);
3267 1414 });
3268 1415 }
3269 -
3270 - function removeActiveWord(botId) {
3271 - botId = botId || 'default';
3272 - var instance = MxChatInstances.get(botId);
3273 - const container = getElementDOM(botId, 'active-word-container');
3274 - const nameElement = getElementDOM(botId, 'active-word-name');
3275 -
3276 - if (!container || !nameElement || !instance.activeWordFile) return;
3277 -
1416 +
1417 + function removeActiveWord() {
1418 + const container = document.getElementById('active-word-container');
1419 + const nameElement = document.getElementById('active-word-name');
1420 +
1421 + if (!container || !nameElement || !activeWordFile) return;
1422 +
3278 1423 fetch(mxchatChat.ajax_url, {
3279 1424 method: 'POST',
3280 1425 headers: {
3281 1426 'Content-Type': 'application/x-www-form-urlencoded',
@@ -3281,9 +1426,9 @@
3281 1426 'Content-Type': 'application/x-www-form-urlencoded',
3282 1427 },
3283 1428 body: new URLSearchParams({
3284 1429 'action': 'mxchat_remove_word',
3285 - 'session_id': getChatSession(botId),
1430 + 'session_id': sessionId,
3286 1431 'nonce': mxchatChat.nonce
3287 1432 })
3288 1433 })
3289 1434 .then(response => response.json())
@@ -3290,58 +1435,61 @@
3290 1435 .then(data => {
3291 1436 if (data.success) {
3292 1437 container.style.display = 'none';
3293 1438 nameElement.textContent = '';
3294 - instance.activeWordFile = null;
3295 - appendMessage('bot', 'Word document removed.', '', [], false, botId);
1439 + activeWordFile = null;
1440 + appendMessage('bot', 'Word document removed.');
3296 1441 }
3297 1442 })
3298 1443 .catch(error => {
3299 - // Error removing Word document - silently continue
1444 + //console.error('Error removing Word document:', error);
3300 1445 });
3301 1446 }
3302 -
1447 +
3303 1448 // ====================================
3304 1449 // CONSENT & COMPLIANCE (GDPR)
3305 1450 // ====================================
3306 -
3307 - function initializeChatVisibility(botId) {
3308 - botId = botId || 'default';
3309 - const complianzEnabled = mxchatChat.complianz_toggle === 'on' ||
3310 - mxchatChat.complianz_toggle === '1' ||
1451 +
1452 + function initializeChatVisibility() {
1453 + //console.log('Initializing chat visibility');
1454 + const complianzEnabled = mxchatChat.complianz_toggle === 'on' ||
1455 + mxchatChat.complianz_toggle === '1' ||
3311 1456 mxchatChat.complianz_toggle === 1;
3312 -
1457 +
3313 1458 if (complianzEnabled && typeof cmplz_has_consent === "function" && typeof complianz !== 'undefined') {
3314 1459 // Initial check
3315 - checkConsentAndShowChat(botId);
3316 -
1460 + checkConsentAndShowChat();
1461 +
3317 1462 // Listen for consent changes
3318 1463 $(document).on('cmplz_status_change', function(event) {
3319 - checkConsentAndShowChat(botId);
1464 + //console.log('Status change detected');
1465 + checkConsentAndShowChat();
3320 1466 });
3321 1467 } else {
3322 1468 // If Complianz is not enabled, always show
3323 - getElement(botId, 'floating-chatbot-button')
1469 + $('#floating-chatbot-button')
3324 1470 .css('display', 'flex')
3325 1471 .removeClass('hidden no-consent')
3326 1472 .fadeTo(500, 1);
3327 -
1473 +
3328 1474 // Also check pre-chat message when Complianz is not enabled
3329 - checkPreChatDismissal(botId);
1475 + checkPreChatDismissal();
3330 1476 }
3331 1477 }
3332 1478
3333 -
3334 - function checkConsentAndShowChat(botId) {
3335 - botId = botId || 'default';
1479 +
1480 + function checkConsentAndShowChat() {
3336 1481 var consentStatus = cmplz_has_consent('marketing');
3337 1482 var consentType = complianz.consenttype;
3338 -
3339 - let $widget = getElement(botId, 'floating-chatbot-button');
3340 - let $chatbot = getElement(botId, 'floating-chatbot');
3341 - let $preChat = getElement(botId, 'pre-chat-message');
3342 -
1483 +
1484 + //console.log('Checking consent:', {status: consentStatus,type: consentType});
1485 +
1486 + let $widget = $('#floating-chatbot-button');
1487 + let $chatbot = $('#floating-chatbot');
1488 + let $preChat = $('#pre-chat-message');
1489 +
3343 1490 if (consentStatus === true) {
1491 + //console.log('Consent granted - showing widget');
3344 1492 $widget
3345 1493 .removeClass('no-consent')
3346 1494 .css('display', 'flex')
3347 1495 .removeClass('hidden')
@@ -3346,12 +1494,13 @@
3346 1494 .css('display', 'flex')
3347 1495 .removeClass('hidden')
3348 1496 .fadeTo(500, 1);
3349 1497 $chatbot.removeClass('no-consent');
3350 -
1498 +
3351 1499 // Show pre-chat message if not dismissed
3352 - checkPreChatDismissal(botId);
1500 + checkPreChatDismissal();
3353 1501 } else {
1502 + //console.log('No consent - hiding widget');
3354 1503 $widget
3355 1504 .addClass('no-consent')
3356 1505 .fadeTo(500, 0, function() {
3357 1506 $(this)
@@ -3358,9 +1507,9 @@
3358 1507 .css('display', 'none')
3359 1508 .addClass('hidden');
3360 1509 });
3361 1510 $chatbot.addClass('no-consent');
3362 -
1511 +
3363 1512 // Hide pre-chat message when no consent
3364 1513 $preChat.hide();
3365 1514 }
3366 1515 }
@@ -3368,38 +1517,46 @@
3368 1517
3369 1518 // ====================================
3370 1519 // PRE-CHAT MESSAGE HANDLING
3371 1520 // ====================================
3372 -
3373 - function checkPreChatDismissal(botId) {
3374 - botId = botId || 'default';
3375 - try {
3376 - var dismissedAt = localStorage.getItem('mxchat_pre_chat_dismissed_' + botId);
3377 - if (dismissedAt) {
3378 - // Re-show after 24 hours
3379 - var elapsed = Date.now() - parseInt(dismissedAt, 10);
3380 - if (elapsed < 86400000) {
3381 - getElement(botId, 'pre-chat-message').hide();
3382 - return;
1521 +
1522 + function checkPreChatDismissal() {
1523 + $.ajax({
1524 + url: mxchatChat.ajax_url,
1525 + type: 'POST',
1526 + data: {
1527 + action: 'mxchat_check_pre_chat_message_status',
1528 + _ajax_nonce: mxchatChat.nonce
1529 + },
1530 + success: function(response) {
1531 + if (response.success && !response.data.dismissed) {
1532 + $('#pre-chat-message').fadeIn(250);
1533 + } else {
1534 + $('#pre-chat-message').hide();
3383 1535 }
3384 - // Expired — clear and show again
3385 - localStorage.removeItem('mxchat_pre_chat_dismissed_' + botId);
1536 + },
1537 + error: function() {
1538 + //console.error('Failed to check pre-chat message dismissal status.');
3386 1539 }
3387 - getElement(botId, 'pre-chat-message').fadeIn(250);
3388 - } catch (e) {
3389 - // localStorage unavailable — show the message
3390 - getElement(botId, 'pre-chat-message').fadeIn(250);
3391 - }
1540 + });
3392 1541 }
3393 -
3394 - function handlePreChatDismissal(botId) {
3395 - botId = botId || 'default';
3396 - getElement(botId, 'pre-chat-message').fadeOut(200);
3397 - try {
3398 - localStorage.setItem('mxchat_pre_chat_dismissed_' + botId, String(Date.now()));
3399 - } catch (e) {
3400 - // localStorage unavailable — dismissal won't persist
3401 - }
1542 +
1543 + function handlePreChatDismissal() {
1544 + $('#pre-chat-message').fadeOut(200);
1545 + $.ajax({
1546 + url: mxchatChat.ajax_url,
1547 + type: 'POST',
1548 + data: {
1549 + action: 'mxchat_dismiss_pre_chat_message',
1550 + _ajax_nonce: mxchatChat.nonce
1551 + },
1552 + success: function() {
1553 + $('#pre-chat-message').hide();
1554 + },
1555 + error: function() {
1556 + //console.error('Failed to dismiss pre-chat message.');
1557 + }
1558 + });
3402 1559 }
3403 1560
3404 1561
3405 1562 // ====================================
@@ -3425,329 +1582,242 @@
3425 1582 // ====================================
3426 1583
3427 1584 $(document).on('click', '.mxchat-popular-question', function () {
3428 1585 var question = $(this).text();
3429 - var botId = getBotIdFromElement(this);
3430 -
1586 +
3431 1587 // Append the question as if the user typed it
3432 - appendMessage("user", question, '', [], false, botId);
3433 -
1588 + appendMessage("user", question);
1589 +
3434 1590 // Only collapse if there are questions
3435 - if (hasQuickQuestions(botId)) {
3436 - collapseQuickQuestions(botId);
1591 + if (hasQuickQuestions()) {
1592 + collapseQuickQuestions();
3437 1593 }
3438 -
1594 +
3439 1595 // Send the question to the server
3440 - sendMessageToChatbot(question, botId);
1596 + sendMessageToChatbot(question);
3441 1597 });
3442 1598
3443 1599 $(document).on('click', '.questions-toggle-btn', function(e) {
3444 1600 e.preventDefault();
3445 1601 e.stopPropagation();
3446 - var botId = getBotIdFromElement(this);
3447 - expandQuickQuestions(botId);
1602 + expandQuickQuestions();
3448 1603 });
3449 1604
3450 1605 $(document).on('click', '.questions-collapse-btn', function(e) {
3451 1606 e.preventDefault();
3452 1607 e.stopPropagation();
3453 - var botId = getBotIdFromElement(this);
3454 - collapseQuickQuestions(botId);
1608 + collapseQuickQuestions();
3455 1609 });
3456 -
3457 -// Consent-safe YouTube embed (plan 03ba33): the server only ever ships a
3458 -// thumbnail facade — no Google iframe exists until the visitor taps play.
3459 -// Delegated so it also works for embeds restored from chat history.
3460 -$(document).on('click', '.mxchat-youtube-embed .mxchat-youtube-facade', function(e) {
3461 - e.preventDefault();
3462 - var $wrap = $(this).closest('.mxchat-youtube-embed');
3463 - var videoId = String($wrap.data('video-id') || '').replace(/[^A-Za-z0-9_-]/g, '');
3464 - if (!videoId) {
3465 - return;
3466 - }
3467 - var title = $wrap.find('.mxchat-youtube-title').text() || 'YouTube video';
3468 - var $iframe = $('<iframe>', {
3469 - src: 'https://www.youtube-nocookie.com/embed/' + videoId + '?autoplay=1&rel=0',
3470 - title: title,
3471 - allow: 'accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture',
3472 - allowfullscreen: true,
3473 - frameborder: 0
3474 - }).addClass('mxchat-youtube-iframe');
3475 - $wrap.addClass('mxchat-youtube-playing');
3476 - $(this).replaceWith($iframe);
3477 -});
3478 1610
3479 - // Chatbot visibility toggle handlers - use class selector for multi-instance support
3480 - // Handles click + Enter/Space keypresses for keyboard accessibility (WCAG 2.1 SC 2.1.1).
3481 - $(document).on('click keydown', '.floating-chatbot-button', function(e) {
3482 - if (e.type === 'keydown') {
3483 - if (e.key !== 'Enter' && e.key !== ' ' && e.key !== 'Spacebar') return;
3484 - e.preventDefault();
3485 - }
3486 - var botId = getBotIdFromElement(this);
3487 - var $chatbot = getElement(botId, 'floating-chatbot');
3488 - var $badge = getElement(botId, 'chat-notification-badge');
3489 - var $preChat = getElement(botId, 'pre-chat-message');
3490 -
3491 - if ($chatbot.hasClass('hidden')) {
3492 - $chatbot.removeClass('hidden').addClass('visible')
3493 - .attr('aria-modal', 'true').attr('role', 'dialog');
3494 - $(this).addClass('hidden').attr('aria-expanded', 'true');
3495 - $badge.hide(); // Hide notification when opening chat
1611 + // Chatbot visibility toggle handlers
1612 + $(document).on('click', '#floating-chatbot-button', function() {
1613 + var chatbot = $('#floating-chatbot');
1614 + if (chatbot.hasClass('hidden')) {
1615 + chatbot.removeClass('hidden').addClass('visible');
1616 + $(this).addClass('hidden');
1617 + $('#chat-notification-badge').hide(); // Hide notification when opening chat
3496 1618 disableScroll();
3497 - $preChat.fadeOut(250);
3498 -
3499 - // First open per page load: re-fetch behavior settings in case
3500 - // this page's inline values came from a stale full-page cache
3501 - // (plan-32db95). Idempotent — later opens are a no-op.
3502 - mxchatRefreshDynamicSettings();
3503 -
3504 - // Load chat history for returning visitors (persistence)
3505 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
3506 - if (chatPersistenceEnabled) {
3507 - MxChatInstances.ensureSession(botId);
3508 - }
3509 -
3510 - // Deferred email check — only on first widget open
3511 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3512 - var instance = MxChatInstances.get(botId);
3513 - if (emailBlocker && !instance.emailCheckDone) {
3514 - instance.emailCheckDone = true;
3515 - resolveEmailState(botId);
3516 - } else if (!emailBlocker) {
3517 - // No email collection — still route through showChatContainerForBot
3518 - // so the loader is shown while chat history loads
3519 - showChatContainerForBot(botId);
3520 - }
3521 -
3522 - // Move keyboard focus into the message input after the open transition.
3523 - setTimeout(function() {
3524 - var chatInput = getElementDOM(botId, 'chat-input');
3525 - if (chatInput && !chatInput.disabled) {
3526 - try { chatInput.focus({ preventScroll: true }); } catch (err) { chatInput.focus(); }
3527 - }
3528 - }, 300);
1619 + $('#pre-chat-message').fadeOut(250);
3529 1620 } else {
3530 - $chatbot.removeClass('visible').addClass('hidden').removeAttr('aria-modal');
3531 - $(this).removeClass('hidden').attr('aria-expanded', 'false');
1621 + chatbot.removeClass('visible').addClass('hidden');
1622 + $(this).removeClass('hidden');
3532 1623 enableScroll();
3533 - checkPreChatDismissal(botId);
1624 + checkPreChatDismissal();
3534 1625 }
3535 1626 });
3536 -
3537 - // Allow clicking anywhere on the title bar to close the chatbot.
3538 - // Returns keyboard focus to the launcher so keyboard users don't get
3539 - // stranded at <body> (WCAG SC 2.4.3 Focus Order). :focus-visible is
3540 - // heuristic-based so mouse-triggered close won't show a focus ring.
3541 - $(document).on('click', '.chatbot-top-bar', function() {
3542 - var botId = getBotIdFromElement(this);
3543 - getElement(botId, 'floating-chatbot').addClass('hidden').removeClass('visible').removeAttr('aria-modal');
3544 - var $launcher = getElement(botId, 'floating-chatbot-button');
3545 - $launcher.removeClass('hidden').attr('aria-expanded', 'false');
1627 +
1628 + $(document).on('click', '#exit-chat-button', function() {
1629 + $('#floating-chatbot').addClass('hidden').removeClass('visible');
1630 + $('#floating-chatbot-button').removeClass('hidden');
3546 1631 enableScroll();
3547 - try { $launcher.trigger('focus'); } catch (err) { /* no-op */ }
3548 1632 });
3549 -
3550 - // Global Escape-key handler — closes any visible chat widget and
3551 - // returns focus to its launcher. Standard modal-dismissal pattern;
3552 - // pairs with aria-modal="true" set on the widget when it opens.
3553 - $(document).on('keydown', function(e) {
3554 - if (e.key !== 'Escape' && e.key !== 'Esc') return;
3555 - var $visible = $('.floating-chatbot.visible');
3556 - if (!$visible.length) return;
3557 - e.preventDefault();
3558 - $visible.each(function() {
3559 - var botId = getBotIdFromElement(this);
3560 - $(this).addClass('hidden').removeClass('visible').removeAttr('aria-modal');
3561 - var $launcher = getElement(botId, 'floating-chatbot-button');
3562 - $launcher.removeClass('hidden').attr('aria-expanded', 'false');
3563 - try { $launcher.trigger('focus'); } catch (err) { /* no-op */ }
3564 - });
3565 - enableScroll();
3566 - });
3567 -
1633 +
3568 1634 $(document).on('click', '.close-pre-chat-message', function(e) {
3569 1635 e.stopPropagation(); // Prevent triggering the parent .pre-chat-message click
3570 - var botId = getBotIdFromElement(this);
3571 - handlePreChatDismissal(botId);
1636 + $('#pre-chat-message').fadeOut(200, function() {
1637 + $(this).remove();
1638 + });
3572 1639 });
3573 -
3574 -
3575 - // PDF upload button handlers - use class selector
3576 - $(document).on('click', '.pdf-upload-btn', function() {
3577 - var botId = getBotIdFromElement(this);
3578 - var pdfInput = getElementDOM(botId, 'pdf-upload');
3579 - if (pdfInput) pdfInput.click();
1640 +
1641 + // Add to Cart button handler
1642 + $(document).on('click', '.mxchat-add-to-cart-button', function() {
1643 + var productId = $(this).data('product-id');
1644 +
1645 + // Get the button text instead of hardcoded "add to cart"
1646 + var buttonText = $(this).text() || "add to cart";
1647 +
1648 + // Add a special prefix to indicate this is from button
1649 + appendMessage("user", buttonText);
1650 + sendMessageToChatbot("!addtocart"); // Special command to indicate button click
3580 1651 });
3581 1652
3582 - // Word upload button handlers - use class selector
3583 - $(document).on('click', '.word-upload-btn', function() {
3584 - var botId = getBotIdFromElement(this);
3585 - var wordInput = getElementDOM(botId, 'word-upload');
3586 - if (wordInput) wordInput.click();
3587 - });
1653 + // PDF upload button handlers
1654 + if (document.getElementById('pdf-upload-btn')) {
1655 + document.getElementById('pdf-upload-btn').addEventListener('click', function() {
1656 + document.getElementById('pdf-upload').click();
1657 + });
1658 + }
3588 1659
3589 - // PDF file input change handler - delegated, bot-aware (was bound to stale un-suffixed id 'pdf-upload')
3590 - $(document).on('change', '.pdf-upload', async function(e) {
3591 - var botId = getBotIdFromElement(this);
3592 - var instance = MxChatInstances.get(botId);
3593 - const file = this.files[0];
3594 - const sessionId = MxChatInstances.ensureSession(botId);
3595 -
1660 + // Word upload button handlers
1661 + if (document.getElementById('word-upload-btn')) {
1662 + document.getElementById('word-upload-btn').addEventListener('click', function() {
1663 + document.getElementById('word-upload').click();
1664 + });
1665 + }
1666 +
1667 + // PDF file input change handler
1668 + addSafeEventListener('pdf-upload', 'change', async function(e) {
1669 + const file = e.target.files[0];
1670 +
3596 1671 if (!file || file.type !== 'application/pdf') {
3597 1672 alert('Please select a valid PDF file.');
3598 1673 return;
3599 1674 }
3600 -
1675 +
3601 1676 if (!sessionId) {
1677 + //console.error('No session ID found');
3602 1678 alert('Error: No session ID found');
3603 1679 return;
3604 1680 }
3605 -
1681 +
3606 1682 if (!mxchatChat || !mxchatChat.ajax_url || !mxchatChat.nonce) {
1683 + //console.error('mxchatChat not properly configured:', mxchatChat);
3607 1684 alert('Error: Ajax configuration missing');
3608 1685 return;
3609 1686 }
3610 -
1687 +
3611 1688 // Disable buttons and show loading state
3612 - const uploadBtn = getElementDOM(botId, 'pdf-upload-btn');
3613 - const sendBtn = getElementDOM(botId, 'send-button');
3614 - if (!uploadBtn) return;
1689 + const uploadBtn = document.getElementById('pdf-upload-btn');
1690 + const sendBtn = document.getElementById('send-button');
3615 1691 const originalBtnContent = uploadBtn.innerHTML;
3616 -
1692 +
3617 1693 try {
3618 - // Wait for page-cache nonce refresh before reading mxchatChat.nonce. See plan-c5457f.
3619 - await new Promise(function(resolve) { refreshNonceIfNeeded(resolve); });
3620 1694 const formData = new FormData();
3621 1695 formData.append('action', 'mxchat_upload_pdf');
3622 1696 formData.append('pdf_file', file);
3623 1697 formData.append('session_id', sessionId);
3624 1698 formData.append('nonce', mxchatChat.nonce);
3625 -
1699 +
3626 1700 uploadBtn.disabled = true;
3627 - if (sendBtn) sendBtn.disabled = true;
1701 + sendBtn.disabled = true;
3628 1702 uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
3629 1703 <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
3630 1704 </svg>`;
3631 -
1705 +
3632 1706 const response = await fetch(mxchatChat.ajax_url, {
3633 1707 method: 'POST',
3634 1708 body: formData
3635 1709 });
3636 -
1710 +
3637 1711 const data = await response.json();
3638 -
1712 +
3639 1713 if (data.success) {
3640 1714 // Hide popular questions if they exist
3641 - if (hasQuickQuestions(botId)) {
3642 - collapseQuickQuestions(botId);
1715 + const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
1716 + if (hasQuickQuestions()) {
1717 + collapseQuickQuestions();
3643 1718 }
3644 -
1719 +
3645 1720 // Show the active PDF name
3646 - showActivePdf(data.data.filename, botId);
3647 -
3648 - appendMessage('bot', data.data.message, '', [], false, botId);
3649 - scrollToBottom(botId);
3650 - instance.activePdfFile = data.data.filename;
1721 + showActivePdf(data.data.filename);
1722 +
1723 + appendMessage('bot', data.data.message);
1724 + scrollToBottom();
1725 + activePdfFile = data.data.filename;
3651 1726 } else {
1727 + //console.error('Upload failed:', data.data);
3652 1728 alert('Failed to upload PDF. Please try again.');
3653 1729 }
3654 1730 } catch (error) {
1731 + //console.error('Upload error:', error);
3655 1732 alert('Error uploading file. Please try again.');
3656 1733 } finally {
3657 1734 uploadBtn.disabled = false;
3658 - if (sendBtn) sendBtn.disabled = false;
1735 + sendBtn.disabled = false;
3659 1736 uploadBtn.innerHTML = originalBtnContent;
3660 1737 this.value = ''; // Reset file input
3661 1738 }
3662 1739 });
3663 1740
3664 - // Word file input change handler - delegated, bot-aware (was bound to stale un-suffixed id 'word-upload')
3665 - $(document).on('change', '.word-upload', async function(e) {
3666 - var botId = getBotIdFromElement(this);
3667 - var instance = MxChatInstances.get(botId);
3668 - const file = this.files[0];
3669 - const sessionId = MxChatInstances.ensureSession(botId);
3670 -
1741 + // Word file input change handler
1742 + addSafeEventListener('word-upload', 'change', async function(e) {
1743 + const file = e.target.files[0];
1744 +
3671 1745 if (!file || file.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
3672 1746 alert('Please select a valid Word document (.docx).');
3673 1747 return;
3674 1748 }
3675 -
1749 +
3676 1750 if (!sessionId) {
1751 + //console.error('No session ID found');
3677 1752 alert('Error: No session ID found');
3678 1753 return;
3679 1754 }
3680 -
3681 - if (!mxchatChat || !mxchatChat.ajax_url || !mxchatChat.nonce) {
3682 - alert('Error: Ajax configuration missing');
3683 - return;
3684 - }
3685 -
1755 +
3686 1756 // Disable buttons and show loading state
3687 - const uploadBtn = getElementDOM(botId, 'word-upload-btn');
3688 - const sendBtn = getElementDOM(botId, 'send-button');
3689 - if (!uploadBtn) return;
1757 + const uploadBtn = document.getElementById('word-upload-btn');
1758 + const sendBtn = document.getElementById('send-button');
3690 1759 const originalBtnContent = uploadBtn.innerHTML;
3691 -
1760 +
3692 1761 try {
3693 - // Wait for page-cache nonce refresh before reading mxchatChat.nonce. See plan-c5457f.
3694 - await new Promise(function(resolve) { refreshNonceIfNeeded(resolve); });
3695 1762 const formData = new FormData();
3696 1763 formData.append('action', 'mxchat_upload_word');
3697 1764 formData.append('word_file', file);
3698 1765 formData.append('session_id', sessionId);
3699 1766 formData.append('nonce', mxchatChat.nonce);
3700 -
1767 +
3701 1768 uploadBtn.disabled = true;
3702 - if (sendBtn) sendBtn.disabled = true;
1769 + sendBtn.disabled = true;
3703 1770 uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
3704 1771 <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
3705 1772 </svg>`;
3706 -
1773 +
3707 1774 const response = await fetch(mxchatChat.ajax_url, {
3708 1775 method: 'POST',
3709 1776 body: formData
3710 1777 });
3711 -
1778 +
3712 1779 const data = await response.json();
3713 -
1780 +
3714 1781 if (data.success) {
3715 1782 // Hide popular questions if they exist
3716 - if (hasQuickQuestions(botId)) {
3717 - collapseQuickQuestions(botId);
1783 + const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
1784 + if (hasQuickQuestions()) {
1785 + collapseQuickQuestions();
3718 1786 }
3719 -
1787 +
3720 1788 // Show the active Word document name
3721 - showActiveWord(data.data.filename, botId);
3722 -
3723 - appendMessage('bot', data.data.message, '', [], false, botId);
3724 - scrollToBottom(botId);
3725 - instance.activeWordFile = data.data.filename;
1789 + showActiveWord(data.data.filename);
1790 +
1791 + appendMessage('bot', data.data.message);
1792 + scrollToBottom();
1793 + activeWordFile = data.data.filename;
3726 1794 } else {
1795 + //console.error('Upload failed:', data.data);
3727 1796 alert('Failed to upload Word document. Please try again.');
3728 1797 }
3729 1798 } catch (error) {
1799 + //console.error('Upload error:', error);
3730 1800 alert('Error uploading file. Please try again.');
3731 1801 } finally {
3732 1802 uploadBtn.disabled = false;
3733 - if (sendBtn) sendBtn.disabled = false;
1803 + sendBtn.disabled = false;
3734 1804 uploadBtn.innerHTML = originalBtnContent;
3735 1805 this.value = ''; // Reset file input
3736 1806 }
3737 1807 });
3738 1808
3739 - // Remove button click handlers - delegated, bot-aware (were bound to stale un-suffixed ids)
3740 - $(document).on('click', '.remove-pdf-btn', function(e) {
1809 + // Remove button click handlers
1810 + document.getElementById('remove-pdf-btn')?.addEventListener('click', function(e) {
3741 1811 e.preventDefault();
3742 1812 e.stopPropagation();
3743 - removeActivePdf(getBotIdFromElement(this));
1813 + removeActivePdf();
3744 1814 });
3745 -
3746 - $(document).on('click', '.remove-word-btn', function(e) {
1815 +
1816 + document.getElementById('remove-word-btn')?.addEventListener('click', function(e) {
3747 1817 e.preventDefault();
3748 1818 e.stopPropagation();
3749 - removeActiveWord(getBotIdFromElement(this));
1819 + removeActiveWord();
3750 1820 });
3751 1821
3752 1822 // Window resize handlers
3753 1823 $(window).on('resize orientationchange', function() {
@@ -3785,245 +1855,28 @@
3785 1855 });
3786 1856
3787 1857
3788 1858 // ====================================
3789 -// INIT LOADER & CHAT CONTAINER HELPERS
1859 +// IMPROVED EMAIL COLLECTION SETUP
3790 1860 // ====================================
3791 -// These must be outside the email collection block so they're always available
3792 -// (used by persistence loading even when email collection is off)
3793 1861
3794 -function showInitLoader(botId) {
3795 - var loader = getElementDOM(botId, 'mxchat-init-loader');
3796 - if (loader) loader.style.display = 'flex';
3797 -}
1862 +// Email collection form setup and handlers
1863 +const emailForm = document.getElementById('email-collection-form');
1864 +const emailBlocker = document.getElementById('email-blocker');
1865 +const chatbotWrapper = document.getElementById('chat-container');
3798 1866
3799 -function hideInitLoader(botId) {
3800 - var loader = getElementDOM(botId, 'mxchat-init-loader');
3801 - if (loader) loader.style.display = 'none';
3802 -}
3803 -
3804 -function showEmailFormForBot(botId) {
3805 - hideInitLoader(botId);
3806 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3807 - var chatContainer = getElementDOM(botId, 'chat-container');
3808 - if (emailBlocker) emailBlocker.style.display = 'flex';
3809 - if (chatContainer) chatContainer.style.display = 'none';
3810 -}
3811 -
3812 -function showChatContainerForBot(botId) {
3813 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3814 - var chatContainer = getElementDOM(botId, 'chat-container');
3815 - if (emailBlocker) emailBlocker.style.display = 'none';
3816 -
3817 - var instance = MxChatInstances.get(botId);
3818 - var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
3819 -
3820 - // If persistence is on and history hasn't loaded yet, show loader
3821 - // while history loads to prevent flash of empty chat
3822 - if (chatPersistenceEnabled && !instance.chatHistoryLoaded) {
3823 - if (chatContainer) chatContainer.style.display = 'none';
3824 - showInitLoader(botId);
3825 - loadChatHistory(botId, function() {
3826 - hideInitLoader(botId);
3827 - if (chatContainer) chatContainer.style.display = 'flex';
3828 - scrollToBottom(botId, true);
3829 - });
3830 - } else {
3831 - hideInitLoader(botId);
3832 - if (chatContainer) chatContainer.style.display = 'flex';
3833 - if (typeof loadChatHistory === 'function') {
3834 - loadChatHistory(botId);
3835 - }
3836 - }
3837 -}
3838 -
3839 -// ====================================
3840 -// EMAIL COLLECTION SETUP - MULTI-INSTANCE VERSION
3841 -// ====================================
3842 -// Only run email collection setup if it's enabled
3843 -if (mxchatChat && mxchatChat.email_collection_enabled === 'on') {
3844 -
3845 - // Track submitting state per bot
3846 - const emailSubmittingState = {};
3847 -
3848 - // Add CSS animations for email form (once globally)
3849 - if (!document.getElementById('email-error-styles')) {
3850 - const style = document.createElement('style');
3851 - style.id = 'email-error-styles';
3852 - style.textContent = `
3853 - @keyframes fadeInError {
3854 - from { opacity: 0; transform: translateY(-5px); }
3855 - to { opacity: 1; transform: translateY(0); }
3856 - }
3857 - .email-input-shake {
3858 - animation: shake 0.5s ease-in-out;
3859 - }
3860 - @keyframes shake {
3861 - 0%, 100% { transform: translateX(0); }
3862 - 25% { transform: translateX(-5px); }
3863 - 75% { transform: translateX(5px); }
3864 - }
3865 - @keyframes spin {
3866 - from { transform: rotate(0deg); }
3867 - to { transform: rotate(360deg); }
3868 - }
3869 - .email-spinner {
3870 - display: inline-block;
3871 - vertical-align: middle;
3872 - }
3873 - `;
3874 - document.head.appendChild(style);
3875 - }
3876 -
3877 - function isValidEmailAddress(email) {
3878 - const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
3879 - return emailRegex.test(email.trim()) && email.length <= 254;
3880 - }
3881 -
3882 - function isValidNameInput(name) {
3883 - return name && name.trim().length >= 2 && name.trim().length <= 100;
3884 - }
3885 -
3886 - /**
3887 - * Replace {visitor_name} placeholder in intro message with actual visitor name
3888 - * @param {string} botId - The bot instance ID
3889 - * @param {string} visitorName - The visitor's name to insert
3890 - */
3891 - function replaceVisitorNamePlaceholder(botId, visitorName) {
3892 - var chatBox = getElementDOM(botId, 'chat-box');
3893 - if (!chatBox) return;
3894 -
3895 - // Find the greeting by its marker, not by position (plan a1a79b) —
3896 - // after a persistence restore the first .bot-message is a restored
3897 - // reply, and {visitor_name} was being substituted into that instead.
3898 - // Positional fallback for HTML cached before this release only.
3899 - var introMessage = chatBox.querySelector('.mxchat-intro-message') || chatBox.querySelector('.bot-message');
3900 - if (!introMessage) return;
3901 -
3902 - var messageContent = introMessage.querySelector('div[dir="auto"]');
3903 - if (!messageContent) return;
3904 -
3905 - var html = messageContent.innerHTML;
3906 -
3907 - // Replace {visitor_name} placeholder (case-insensitive)
3908 - if (visitorName && visitorName.trim()) {
3909 - // Escape HTML to prevent XSS
3910 - var safeName = $('<div>').text(visitorName.trim()).html();
3911 - html = html.replace(/\{visitor_name\}/gi, safeName);
3912 - } else {
3913 - // Remove placeholder and clean up spacing if no name provided
3914 - html = html.replace(/\{visitor_name\}/gi, '');
3915 - // Clean up any double spaces that might result
3916 - html = html.replace(/\s{2,}/g, ' ').trim();
3917 - }
3918 -
3919 - messageContent.innerHTML = html;
3920 - }
3921 -
3922 - function setEmailSubmissionState(botId, loading) {
3923 - var submitButton = getElementDOM(botId, 'email-submit-button');
3924 - var emailInput = getElementDOM(botId, 'user-email');
3925 - var nameInput = getElementDOM(botId, 'user-name');
3926 -
3927 - if (loading) {
3928 - emailSubmittingState[botId] = true;
3929 - if (submitButton) submitButton.disabled = true;
3930 - if (emailInput) emailInput.disabled = true;
3931 - if (nameInput) nameInput.disabled = true;
3932 -
3933 - if (submitButton && !submitButton.getAttribute('data-original-html')) {
3934 - submitButton.setAttribute('data-original-html', submitButton.innerHTML);
3935 - const originalText = submitButton.textContent;
3936 - submitButton.innerHTML = `
3937 - <svg class="email-spinner" style="width: 16px; height: 16px; margin-right: 8px; animation: spin 1s linear infinite;" viewBox="0 0 24 24">
3938 - <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" fill="none" stroke-dasharray="31.416" stroke-dashoffset="31.416">
3939 - <animate attributeName="stroke-dasharray" dur="2s" values="0 31.416;15.708 15.708;0 31.416" repeatCount="indefinite"/>
3940 - <animate attributeName="stroke-dashoffset" dur="2s" values="0;-15.708;-31.416" repeatCount="indefinite"/>
3941 - </circle>
3942 - </svg>
3943 - ${originalText}
3944 - `;
3945 - submitButton.style.opacity = '0.8';
3946 - }
3947 - } else {
3948 - emailSubmittingState[botId] = false;
3949 - if (submitButton) submitButton.disabled = false;
3950 - if (emailInput) emailInput.disabled = false;
3951 - if (nameInput) nameInput.disabled = false;
3952 -
3953 - if (submitButton) {
3954 - const originalHtml = submitButton.getAttribute('data-original-html');
3955 - if (originalHtml) {
3956 - submitButton.innerHTML = originalHtml;
3957 - }
3958 - submitButton.style.opacity = '1';
3959 - }
3960 - }
3961 - }
3962 -
3963 - function showEmailError(botId, message) {
3964 - clearEmailError(botId);
3965 -
3966 - var emailForm = getElementDOM(botId, 'email-collection-form');
3967 - if (!emailForm) return;
3968 -
3969 - const errorDiv = document.createElement('div');
3970 - errorDiv.className = 'email-error';
3971 - errorDiv.style.cssText = `
3972 - color: #e74c3c;
3973 - font-size: 12px;
3974 - margin-top: 8px;
3975 - padding: 4px 0;
3976 - animation: fadeInError 0.3s ease;
3977 - `;
3978 - errorDiv.textContent = message;
3979 - emailForm.appendChild(errorDiv);
3980 -
3981 - // Add shake animation to inputs
3982 - var emailInput = getElementDOM(botId, 'user-email');
3983 - var nameInput = getElementDOM(botId, 'user-name');
3984 -
3985 - if (emailInput) {
3986 - emailInput.classList.add('email-input-shake');
3987 - setTimeout(() => emailInput.classList.remove('email-input-shake'), 500);
3988 - }
3989 - if (nameInput) {
3990 - nameInput.classList.add('email-input-shake');
3991 - setTimeout(() => nameInput.classList.remove('email-input-shake'), 500);
3992 - }
3993 - }
3994 -
3995 - function clearEmailError(botId) {
3996 - var emailForm = getElementDOM(botId, 'email-collection-form');
3997 - if (emailForm) {
3998 - const existingErrors = emailForm.querySelectorAll('.email-error');
3999 - existingErrors.forEach(error => error.remove());
4000 - }
4001 - }
4002 -
4003 - // Resolve email state using server-side data when available, AJAX fallback otherwise
4004 - function resolveEmailState(botId) {
4005 - if (mxchatChat.skip_email_check && mxchatChat.initial_email_state) {
4006 - if (mxchatChat.initial_email_state.show_email_form) {
4007 - showEmailFormForBot(botId);
4008 - } else {
4009 - showChatContainerForBot(botId);
4010 - }
4011 - } else {
4012 - checkSessionAndEmailForBot(botId);
4013 - }
4014 - }
4015 -
4016 - function checkSessionAndEmailForBot(botId) {
4017 - const sessionId = MxChatInstances.ensureSession(botId);
4018 -
4019 - // Hide both panels while we check — show loader instead
4020 - var emailBlocker = getElementDOM(botId, 'email-blocker');
4021 - var chatContainer = getElementDOM(botId, 'chat-container');
4022 - if (emailBlocker) emailBlocker.style.display = 'none';
4023 - if (chatContainer) chatContainer.style.display = 'none';
4024 - showInitLoader(botId);
4025 -
1867 +if (emailForm && emailBlocker && chatbotWrapper) {
1868 + // Add loading state management
1869 + let isSubmitting = false;
1870 +
1871 + // Check if email exists for the current session
1872 + function checkSessionAndEmail() {
1873 + const sessionId = getChatSession();
1874 +
1875 + // Add timeout to prevent hanging
1876 + const controller = new AbortController();
1877 + const timeoutId = setTimeout(() => controller.abort(), 10000); // 10 second timeout
1878 +
4026 1879 fetch(mxchatChat.ajax_url, {
4027 1880 method: 'POST',
4028 1881 headers: {
4029 1882 'Content-Type': 'application/x-www-form-urlencoded',
@@ -4031,11 +1884,13 @@
4031 1884 body: new URLSearchParams({
4032 1885 action: 'mxchat_check_email_provided',
4033 1886 session_id: sessionId,
4034 1887 nonce: mxchatChat.nonce,
4035 - })
1888 + }),
1889 + signal: controller.signal
4036 1890 })
4037 1891 .then((response) => {
1892 + clearTimeout(timeoutId);
4038 1893 if (!response.ok) {
4039 1894 throw new Error(`HTTP error! status: ${response.status}`);
4040 1895 }
4041 1896 return response.json();
@@ -4042,93 +1897,157 @@
4042 1897 })
4043 1898 .then((data) => {
4044 1899 if (data.success) {
4045 1900 if (data.data.logged_in || data.data.email) {
4046 - showChatContainerForBot(botId);
1901 + showChatContainer();
4047 1902 } else {
4048 - showEmailFormForBot(botId);
1903 + showEmailForm();
4049 1904 }
4050 1905 } else {
4051 - showEmailFormForBot(botId);
1906 + // On error, default to showing email form
1907 + showEmailForm();
4052 1908 }
4053 1909 })
4054 1910 .catch((error) => {
4055 - showEmailFormForBot(botId);
1911 + clearTimeout(timeoutId);
1912 + console.warn('Email check failed, defaulting to email form:', error);
1913 + showEmailForm();
4056 1914 });
4057 1915 }
4058 1916
4059 - // Event delegation for email form submission
4060 - $(document).on('submit', '.email-collection-form', function(e) {
4061 - e.preventDefault();
4062 - e.stopPropagation();
1917 + // Optimized UI transition functions
1918 + function showEmailForm() {
1919 + emailBlocker.style.display = 'flex';
1920 + chatbotWrapper.style.display = 'none';
1921 + }
4063 1922
4064 - var botId = getBotIdFromElement(this);
1923 + function showChatContainer() {
1924 + // Show chat immediately without delay
1925 + emailBlocker.style.display = 'none';
1926 + chatbotWrapper.style.display = 'flex';
1927 +
1928 + // Load chat history only after showing chat container
1929 + if (typeof loadChatHistory === 'function') {
1930 + loadChatHistory();
1931 + }
1932 + }
4065 1933
4066 - // Prevent double submission
4067 - if (emailSubmittingState[botId]) {
4068 - return false;
1934 + // Enhanced email validation
1935 + function isValidEmail(email) {
1936 + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
1937 + return emailRegex.test(email.trim()) && email.length <= 254; // RFC 5321 limit
1938 + }
1939 +
1940 + // Show loading state with spinner
1941 + function setSubmissionState(loading) {
1942 + const submitButton = document.getElementById('email-submit-button');
1943 + const emailInput = document.getElementById('user-email');
1944 +
1945 + if (loading) {
1946 + isSubmitting = true;
1947 + submitButton.disabled = true;
1948 + emailInput.disabled = true;
1949 +
1950 + // Store original content and add spinner
1951 + if (!submitButton.getAttribute('data-original-html')) {
1952 + submitButton.setAttribute('data-original-html', submitButton.innerHTML);
1953 + }
1954 +
1955 + // Add loading spinner while keeping original text
1956 + const originalText = submitButton.textContent;
1957 + submitButton.innerHTML = `
1958 + <svg class="email-spinner" style="width: 16px; height: 16px; margin-right: 8px; animation: spin 1s linear infinite;" viewBox="0 0 24 24">
1959 + <circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" fill="none" stroke-dasharray="31.416" stroke-dashoffset="31.416">
1960 + <animate attributeName="stroke-dasharray" dur="2s" values="0 31.416;15.708 15.708;0 31.416" repeatCount="indefinite"/>
1961 + <animate attributeName="stroke-dashoffset" dur="2s" values="0;-15.708;-31.416" repeatCount="indefinite"/>
1962 + </circle>
1963 + </svg>
1964 + ${originalText}
1965 + `;
1966 +
1967 + submitButton.style.opacity = '0.8';
1968 + } else {
1969 + isSubmitting = false;
1970 + submitButton.disabled = false;
1971 + emailInput.disabled = false;
1972 +
1973 + // Restore original content
1974 + const originalHtml = submitButton.getAttribute('data-original-html');
1975 + if (originalHtml) {
1976 + submitButton.innerHTML = originalHtml;
1977 + }
1978 +
1979 + submitButton.style.opacity = '1';
4069 1980 }
1981 + }
4070 1982
4071 - var emailInput = getElementDOM(botId, 'user-email');
4072 - var nameInput = getElementDOM(botId, 'user-name');
4073 - var consentInput = getElementDOM(botId, 'user-consent');
4074 - var userEmail = emailInput ? emailInput.value.trim() : '';
4075 - var userName = nameInput ? nameInput.value.trim() : '';
4076 - var sessionId = MxChatInstances.ensureSession(botId);
1983 + // Add CSS for spinner animation if not already present
1984 + if (!document.getElementById('email-spinner-styles')) {
1985 + const style = document.createElement('style');
1986 + style.id = 'email-spinner-styles';
1987 + style.textContent = `
1988 + @keyframes spin {
1989 + from { transform: rotate(0deg); }
1990 + to { transform: rotate(360deg); }
1991 + }
1992 + .email-spinner {
1993 + display: inline-block;
1994 + vertical-align: middle;
1995 + }
1996 + `;
1997 + document.head.appendChild(style);
1998 + }
4077 1999
4078 - // Validate email
4079 - if (!userEmail) {
4080 - showEmailError(botId, 'Please enter your email address.');
4081 - return false;
2000 + // Handle email form submission with improved error handling
2001 + emailForm.addEventListener('submit', function (event) {
2002 + event.preventDefault();
2003 +
2004 + // Prevent double submission
2005 + if (isSubmitting) {
2006 + return;
4082 2007 }
4083 2008
4084 - if (!isValidEmailAddress(userEmail)) {
4085 - showEmailError(botId, 'Please enter a valid email address.');
4086 - return false;
4087 - }
2009 + const userEmail = document.getElementById('user-email').value.trim();
2010 + const sessionId = getChatSession();
4088 2011
4089 - // Validate name if field exists and has content
4090 - if (nameInput && userName && !isValidNameInput(userName)) {
4091 - showEmailError(botId, 'Please enter a valid name (2-100 characters).');
4092 - return false;
2012 + // Validate email before submission
2013 + if (!userEmail) {
2014 + showEmailError('Please enter your email address.');
2015 + return;
4093 2016 }
4094 2017
4095 - // Consent checkbox (b062c4): backstop behind the native required
4096 - // attribute; the server enforces this independently either way.
4097 - if (consentInput && consentInput.required && !consentInput.checked) {
4098 - showEmailError(botId, 'Please tick the consent box to continue.');
4099 - return false;
2018 + if (!isValidEmail(userEmail)) {
2019 + showEmailError('Please enter a valid email address.');
2020 + return;
4100 2021 }
4101 2022
4102 - clearEmailError(botId);
4103 - setEmailSubmissionState(botId, true);
2023 + // Clear any existing errors
2024 + clearEmailError();
2025 + setSubmissionState(true);
4104 2026
4105 - // Prepare form data
4106 - const formData = new URLSearchParams({
4107 - action: 'mxchat_handle_save_email_and_response',
4108 - email: userEmail,
4109 - session_id: sessionId,
4110 - nonce: mxchatChat.nonce,
4111 - });
2027 + // Add timeout for submission
2028 + const controller = new AbortController();
2029 + const timeoutId = setTimeout(() => {
2030 + controller.abort();
2031 + setSubmissionState(false);
2032 + showEmailError('Request timed out. Please try again.');
2033 + }, 15000); // 15 second timeout
4112 2034
4113 - if (userName) {
4114 - formData.append('name', userName);
4115 - }
4116 -
4117 - // Ticked/unticked both travel when the checkbox is rendered, so an
4118 - // optional-consent "no" is recorded as a decision, not an absence.
4119 - if (consentInput) {
4120 - formData.append('consent', consentInput.checked ? '1' : '0');
4121 - }
4122 -
4123 2035 fetch(mxchatChat.ajax_url, {
4124 2036 method: 'POST',
4125 2037 headers: {
4126 2038 'Content-Type': 'application/x-www-form-urlencoded',
4127 2039 },
4128 - body: formData
2040 + body: new URLSearchParams({
2041 + action: 'mxchat_handle_save_email_and_response',
2042 + email: userEmail,
2043 + session_id: sessionId,
2044 + nonce: mxchatChat.nonce,
2045 + }),
2046 + signal: controller.signal
4129 2047 })
4130 2048 .then((response) => {
2049 + clearTimeout(timeoutId);
4131 2050 if (!response.ok) {
4132 2051 throw new Error(`HTTP error! status: ${response.status}`);
4133 2052 }
4134 2053 return response.json();
@@ -4133,166 +2052,185 @@
4133 2052 }
4134 2053 return response.json();
4135 2054 })
4136 2055 .then((data) => {
4137 - setEmailSubmissionState(botId, false);
4138 -
2056 + setSubmissionState(false);
2057 +
4139 2058 if (data.success) {
4140 - showChatContainerForBot(botId);
2059 + // Show chat immediately
2060 + showChatContainer();
4141 2061
4142 - // Replace {visitor_name} placeholder in intro message with actual name
4143 - if (userName) {
4144 - replaceVisitorNamePlaceholder(botId, userName);
4145 - } else {
4146 - // Remove placeholder if no name provided
4147 - replaceVisitorNamePlaceholder(botId, '');
4148 - }
4149 -
2062 + // Handle bot response if provided
4150 2063 if (data.message && typeof appendMessage === 'function') {
4151 2064 setTimeout(() => {
4152 - appendMessage('bot', data.message, '', [], false, botId);
2065 + appendMessage('bot', data.message);
4153 2066 if (typeof scrollToBottom === 'function') {
4154 - scrollToBottom(botId);
2067 + scrollToBottom();
4155 2068 }
4156 2069 }, 100);
4157 2070 }
4158 2071 } else {
4159 - showEmailError(botId, data.message || 'Failed to save email. Please try again.');
2072 + showEmailError(data.message || 'Failed to save email. Please try again.');
4160 2073 }
4161 2074 })
4162 2075 .catch((error) => {
4163 - setEmailSubmissionState(botId, false);
4164 - showEmailError(botId, 'An error occurred. Please try again.');
2076 + clearTimeout(timeoutId);
2077 + setSubmissionState(false);
2078 +
2079 + if (error.name === 'AbortError') {
2080 + showEmailError('Request timed out. Please try again.');
2081 + } else {
2082 + console.error('Email submission error:', error);
2083 + showEmailError('An error occurred. Please try again.');
2084 + }
4165 2085 });
4166 -
4167 - return false;
4168 2086 });
4169 2087
4170 - // Real-time email validation using event delegation
4171 - $(document).on('input', '.mxchat-email-input', function() {
4172 - var botId = getBotIdFromElement(this);
4173 - var $input = $(this);
2088 + // Real-time email validation
2089 + const emailInput = document.getElementById('user-email');
2090 + if (emailInput) {
2091 + let validationTimeout;
2092 +
2093 + emailInput.addEventListener('input', function() {
2094 + // Clear previous validation timeout
2095 + if (validationTimeout) {
2096 + clearTimeout(validationTimeout);
2097 + }
2098 +
2099 + // Debounce validation
2100 + validationTimeout = setTimeout(() => {
2101 + const email = this.value.trim();
2102 + clearEmailError();
2103 +
2104 + if (email && !isValidEmail(email)) {
2105 + showEmailError('Please enter a valid email address.');
2106 + }
2107 + }, 500);
2108 + });
4174 2109
4175 - // Clear previous timeout
4176 - clearTimeout($input.data('validationTimeout'));
4177 -
4178 - // Debounce validation
4179 - var timeout = setTimeout(() => {
4180 - var email = this.value.trim();
4181 - clearEmailError(botId);
4182 -
4183 - if (email && !isValidEmailAddress(email)) {
4184 - showEmailError(botId, 'Please enter a valid email address.');
2110 + // Handle Enter key
2111 + emailInput.addEventListener('keypress', function(e) {
2112 + if (e.key === 'Enter' && !isSubmitting) {
2113 + emailForm.dispatchEvent(new Event('submit'));
4185 2114 }
4186 - }, 500);
2115 + });
2116 + }
4187 2117
4188 - $input.data('validationTimeout', timeout);
4189 - });
4190 -
4191 - // Handle Enter key in email input
4192 - $(document).on('keypress', '.mxchat-email-input', function(e) {
4193 - if (e.key === 'Enter') {
4194 - e.preventDefault();
4195 - var botId = getBotIdFromElement(this);
4196 - if (!emailSubmittingState[botId]) {
4197 - $(this).closest('.email-collection-form').submit();
4198 - }
2118 + // Error display functions
2119 + function showEmailError(message) {
2120 + clearEmailError();
2121 +
2122 + const errorDiv = document.createElement('div');
2123 + errorDiv.className = 'email-error';
2124 + errorDiv.style.cssText = `
2125 + color: #e74c3c;
2126 + font-size: 12px;
2127 + margin-top: 8px;
2128 + padding: 4px 0;
2129 + animation: fadeInError 0.3s ease;
2130 + `;
2131 + errorDiv.textContent = message;
2132 +
2133 + // Add CSS animation if not already present
2134 + if (!document.getElementById('email-error-styles')) {
2135 + const style = document.createElement('style');
2136 + style.id = 'email-error-styles';
2137 + style.textContent = `
2138 + @keyframes fadeInError {
2139 + from { opacity: 0; transform: translateY(-5px); }
2140 + to { opacity: 1; transform: translateY(0); }
2141 + }
2142 + .email-input-shake {
2143 + animation: shake 0.5s ease-in-out;
2144 + }
2145 + @keyframes shake {
2146 + 0%, 100% { transform: translateX(0); }
2147 + 25% { transform: translateX(-5px); }
2148 + 75% { transform: translateX(5px); }
2149 + }
2150 + `;
2151 + document.head.appendChild(style);
4199 2152 }
4200 - });
4201 -
4202 - // Handle Enter key in name input
4203 - $(document).on('keypress', '.mxchat-name-input', function(e) {
4204 - if (e.key === 'Enter') {
4205 - e.preventDefault();
4206 - var botId = getBotIdFromElement(this);
4207 - if (!emailSubmittingState[botId]) {
4208 - $(this).closest('.email-collection-form').submit();
4209 - }
2153 +
2154 + emailForm.appendChild(errorDiv);
2155 +
2156 + // Add shake animation to input
2157 + if (emailInput) {
2158 + emailInput.classList.add('email-input-shake');
2159 + setTimeout(() => {
2160 + emailInput.classList.remove('email-input-shake');
2161 + }, 500);
4210 2162 }
4211 - });
2163 + }
4212 2164
4213 - // Initialize email check for all bot instances
4214 - // For floating bots: defer until widget is opened (zero passive AJAX)
4215 - // For embedded bots: check immediately since the form is visible
4216 - $('.mxchat-chatbot-wrapper').each(function() {
4217 - var botId = $(this).data('bot-id') || 'default';
4218 - var emailBlocker = getElementDOM(botId, 'email-blocker');
2165 + function clearEmailError() {
2166 + const existingErrors = emailForm.querySelectorAll('.email-error');
2167 + existingErrors.forEach(error => error.remove());
2168 + }
4219 2169
4220 - if (emailBlocker) {
4221 - if (isEmbeddedBot(botId)) {
4222 - // Embedded bots are always visible — check now
4223 - resolveEmailState(botId);
4224 - }
4225 - // Floating bots: handled in the widget open handler
4226 - } else if (isEmbeddedBot(botId)) {
4227 - // Embedded bot, no email collection — load history with loader
4228 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
4229 - if (chatPersistenceEnabled) {
4230 - MxChatInstances.ensureSession(botId);
4231 - showChatContainerForBot(botId);
4232 - }
4233 - }
2170 + // Initialize email check with delay to prevent race conditions
2171 + setTimeout(checkSessionAndEmail, 100);
2172 +
2173 +} else if (mxchatChat.email_collection_enabled) {
2174 + console.error('Essential elements for email handling are missing:', {
2175 + emailForm: !!emailForm,
2176 + emailBlocker: !!emailBlocker,
2177 + chatbotWrapper: !!chatbotWrapper
4234 2178 });
4235 2179 }
4236 2180
4237 - // Open chatbot when pre-chat message is clicked - use class selector for multi-instance
4238 - $(document).on('click', '.pre-chat-message', function() {
4239 - var botId = getBotIdFromElement(this);
4240 - var $chatbot = getElement(botId, 'floating-chatbot');
4241 - if ($chatbot.hasClass('hidden')) {
4242 - $chatbot.removeClass('hidden').addClass('visible');
4243 - getElement(botId, 'floating-chatbot-button').addClass('hidden');
4244 - handlePreChatDismissal(botId);
2181 +
2182 + // Open chatbot when pre-chat message is clicked
2183 + $(document).on('click', '#pre-chat-message', function() {
2184 + var chatbot = $('#floating-chatbot');
2185 + if (chatbot.hasClass('hidden')) {
2186 + chatbot.removeClass('hidden').addClass('visible');
2187 + $('#floating-chatbot-button').addClass('hidden');
2188 + $('#pre-chat-message').fadeOut(250); // Hide pre-chat message
4245 2189 disableScroll(); // Disable scroll when chatbot opens
4246 -
4247 - // Load chat history for returning visitors (persistence)
4248 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
4249 - if (chatPersistenceEnabled) {
4250 - MxChatInstances.ensureSession(botId);
4251 - }
4252 -
4253 - // Deferred email check — only on first widget open
4254 - var emailBlocker = getElementDOM(botId, 'email-blocker');
4255 - var instance = MxChatInstances.get(botId);
4256 - if (emailBlocker && !instance.emailCheckDone) {
4257 - instance.emailCheckDone = true;
4258 - resolveEmailState(botId);
4259 - } else if (!emailBlocker) {
4260 - showChatContainerForBot(botId);
4261 - }
4262 2190 }
4263 2191 });
4264 2192
4265 - // Legacy duplicate close handler removed — handled by single event delegation above
2193 + var closeButton = document.querySelector('.close-pre-chat-message');
2194 + if (closeButton) {
2195 + closeButton.addEventListener('click', function() {
2196 + $('#pre-chat-message').fadeOut(200); // Hide the message
4266 2197
2198 + // Send an AJAX request to set the transient flag for 24 hours
2199 + $.ajax({
2200 + url: mxchatChat.ajax_url,
2201 + type: 'POST',
2202 + data: {
2203 + action: 'mxchat_dismiss_pre_chat_message',
2204 + _ajax_nonce: mxchatChat.nonce
2205 + },
2206 + success: function() {
2207 + //console.log('Pre-chat message dismissed for 24 hours.');
4267 2208
4268 -function hasQuickQuestions(botId) {
4269 - botId = botId || 'default';
4270 - var questionsContainer = getElementDOM(botId, 'mxchat-popular-questions');
4271 - if (!questionsContainer) return false;
4272 - const questionButtons = questionsContainer.querySelectorAll('.mxchat-popular-question');
2209 + // Ensure the message is hidden after dismissal
2210 + $('#pre-chat-message').hide();
2211 + },
2212 + error: function() {
2213 + ////console.error('Failed to dismiss pre-chat message.');
2214 + }
2215 + });
2216 + });
2217 + }
2218 +
2219 +
2220 +function hasQuickQuestions() {
2221 + const questionButtons = document.querySelectorAll('#mxchat-popular-questions .mxchat-popular-question');
4273 2222 return questionButtons.length > 0;
4274 2223 }
4275 2224
4276 -/**
4277 - * Check if a bot is embedded (not floating)
4278 - * Embedded bots don't have a .floating-chatbot wrapper
4279 - */
4280 -function isEmbeddedBot(botId) {
4281 - botId = botId || 'default';
4282 - var floatingWrapper = document.getElementById('floating-chatbot-' + botId);
4283 - return !floatingWrapper;
4284 -}
4285 -
4286 -function collapseQuickQuestions(botId) {
4287 - botId = botId || 'default';
4288 - const questionsContainer = getElementDOM(botId, 'mxchat-popular-questions');
4289 - if (questionsContainer && hasQuickQuestions(botId)) {
2225 +function collapseQuickQuestions() {
2226 + const questionsContainer = document.getElementById('mxchat-popular-questions');
2227 + if (questionsContainer && hasQuickQuestions()) {
4290 2228 questionsContainer.classList.add('collapsed');
4291 2229 questionsContainer.classList.add('has-been-collapsed');
4292 2230 try {
4293 - sessionStorage.setItem('mxchat_questions_collapsed_' + botId, 'true');
4294 - sessionStorage.setItem('mxchat_questions_has_been_collapsed_' + botId, 'true');
2231 + sessionStorage.setItem('mxchat_questions_collapsed', 'true');
2232 + sessionStorage.setItem('mxchat_questions_has_been_collapsed', 'true');
4295 2233 } catch (e) {
4296 2234 // Ignore if sessionStorage is not available
4297 2235 }
4298 2236 }
@@ -4297,15 +2235,14 @@
4297 2235 }
4298 2236 }
4299 2237 }
4300 2238
4301 -function expandQuickQuestions(botId) {
4302 - botId = botId || 'default';
4303 - const questionsContainer = getElementDOM(botId, 'mxchat-popular-questions');
4304 - if (questionsContainer && hasQuickQuestions(botId)) {
2239 +function expandQuickQuestions() {
2240 + const questionsContainer = document.getElementById('mxchat-popular-questions');
2241 + if (questionsContainer && hasQuickQuestions()) {
4305 2242 questionsContainer.classList.remove('collapsed');
4306 2243 try {
4307 - sessionStorage.setItem('mxchat_questions_collapsed_' + botId, 'false');
2244 + sessionStorage.setItem('mxchat_questions_collapsed', 'false');
4308 2245 } catch (e) {
4309 2246 // Ignore if sessionStorage is not available
4310 2247 }
4311 2248 }
@@ -4310,24 +2247,18 @@
4310 2247 }
4311 2248 }
4312 2249 }
4313 2250
4314 -function checkQuickQuestionsState(botId) {
4315 - botId = botId || 'default';
4316 - if (!hasQuickQuestions(botId)) {
2251 +function checkQuickQuestionsState() {
2252 + if (!hasQuickQuestions()) {
4317 2253 return; // Don't do anything if no questions exist
4318 2254 }
4319 -
4320 - // Skip restoring collapsed state for embedded bots - they should always start expanded
4321 - if (isEmbeddedBot(botId)) {
4322 - return;
4323 - }
4324 -
2255 +
4325 2256 try {
4326 - const isCollapsed = sessionStorage.getItem('mxchat_questions_collapsed_' + botId);
4327 - const hasBeenCollapsed = sessionStorage.getItem('mxchat_questions_has_been_collapsed_' + botId);
4328 -
4329 - const questionsContainer = getElementDOM(botId, 'mxchat-popular-questions');
2257 + const isCollapsed = sessionStorage.getItem('mxchat_questions_collapsed');
2258 + const hasBeenCollapsed = sessionStorage.getItem('mxchat_questions_has_been_collapsed');
2259 +
2260 + const questionsContainer = document.getElementById('mxchat-popular-questions');
4330 2261 if (questionsContainer) {
4331 2262 if (hasBeenCollapsed === 'true') {
4332 2263 questionsContainer.classList.add('has-been-collapsed');
4333 2264 }
@@ -4338,100 +2269,39 @@
4338 2269 } catch (e) {
4339 2270 // Ignore if sessionStorage is not available
4340 2271 }
4341 2272 }
2273 +
2274 +
2275 +
2276 +
2277 +// ====================================
2278 +// MAIN INITIALIZATION
2279 +// ====================================
4342 2280
4343 -// Global delegation for dynamically added links as fallback
4344 -// Use class selector for multi-instance support
4345 -$(document).on('click', '.chat-box a[href]:not([data-tracked])', function(e) {
4346 - const $link = $(this);
4347 - const messageDiv = $link.closest('.bot-message, .agent-message');
2281 +if ($('#floating-chatbot').hasClass('hidden')) {
2282 + $('#floating-chatbot-button').removeClass('hidden');
2283 +}
2284 +// Initialize when document is ready
2285 +setFullHeight();
2286 +initializeChatVisibility();
2287 +loadChatHistory();
4348 2288
4349 - // Only process bot/agent message links
4350 - if (messageDiv.length > 0) {
4351 - const originalHref = $link.attr('href');
2289 +// Make functions globally available for add-ons
2290 +window.hasQuickQuestions = hasQuickQuestions;
2291 +window.collapseQuickQuestions = collapseQuickQuestions;
2292 +window.appendMessage = appendMessage;
2293 +window.appendThinkingMessage = appendThinkingMessage;
2294 +window.scrollToBottom = scrollToBottom;
2295 +window.scrollElementToTop = scrollElementToTop;
2296 +window.replaceLastMessage = replaceLastMessage;
2297 +window.callMxChat = callMxChat;
2298 +window.callMxChatStream = callMxChatStream;
2299 +window.shouldUseStreaming = shouldUseStreaming;
2300 +window.getChatSession = getChatSession;
2301 +window.getPageContext = getPageContext;
2302 +window.updateStreamingMessage = updateStreamingMessage;
4352 2303
4353 - if (originalHref && (originalHref.startsWith('http://') || originalHref.startsWith('https://'))) {
4354 - e.preventDefault();
4355 - e.stopPropagation();
4356 -
4357 - // Mark as tracked
4358 - $link.attr('data-tracked', 'true');
4359 -
4360 - // Get bot ID from the chat box context
4361 - var botId = getBotIdFromElement(this);
4362 -
4363 - // Get message context from the message div
4364 - const messageText = messageDiv.text().substring(0, 200);
4365 -
4366 - $.ajax({
4367 - url: mxchatChat.ajax_url,
4368 - type: 'POST',
4369 - data: {
4370 - action: 'mxchat_track_url_click',
4371 - session_id: getChatSession(botId),
4372 - url: originalHref,
4373 - message_context: messageText,
4374 - nonce: mxchatChat.nonce
4375 - },
4376 - complete: function() {
4377 - if ($link.attr('target') === '_blank' || linkTarget === '_blank') {
4378 - window.open(originalHref, '_blank');
4379 - } else {
4380 - window.location.href = originalHref;
4381 - }
4382 - }
4383 - });
4384 -
4385 - return false;
4386 - }
4387 - }
4388 -});
4389 -
4390 - // ====================================
4391 - // MAIN INITIALIZATION
4392 - // ====================================
4393 -
4394 - // Initialize all chatbot instances on the page
4395 - initializeAllInstances();
4396 -
4397 - // Legacy initialization for single bot compatibility
4398 - $('.floating-chatbot.hidden').each(function() {
4399 - var botId = getBotIdFromElement(this);
4400 - getElement(botId, 'floating-chatbot-button').removeClass('hidden');
4401 - });
4402 -
4403 - // Initialize when document is ready
4404 - setFullHeight();
4405 -
4406 - // Note: trackOriginatingPage() and loadChatHistory() are now deferred
4407 - // until the user's first interaction via MxChatInstances.ensureSession()
4408 -
4409 - // Initialize chat visibility for all instances
4410 - $('.mxchat-chatbot-wrapper').each(function() {
4411 - var botId = $(this).data('bot-id') || 'default';
4412 - initializeChatVisibility(botId);
4413 - });
4414 -
4415 - // Make functions globally available for add-ons
4416 - window.hasQuickQuestions = hasQuickQuestions;
4417 - window.collapseQuickQuestions = collapseQuickQuestions;
4418 - window.appendMessage = appendMessage;
4419 - window.appendThinkingMessage = appendThinkingMessage;
4420 - window.scrollToBottom = scrollToBottom;
4421 - window.scrollElementToTop = scrollElementToTop;
4422 - window.replaceLastMessage = replaceLastMessage;
4423 - window.callMxChat = callMxChat;
4424 - window.callMxChatStream = callMxChatStream;
4425 - window.shouldUseStreaming = shouldUseStreaming;
4426 - window.getChatSession = getChatSession;
4427 - window.getPageContext = getPageContext;
4428 - window.updateStreamingMessage = updateStreamingMessage;
4429 - window.MxChatInstances = MxChatInstances;
4430 - window.getElement = getElement;
4431 - window.getElementDOM = getElementDOM;
4432 - window.getBotIdFromElement = getBotIdFromElement;
4433 -
4434 2304 }); // End of jQuery ready
4435 2305
4436 2306
4437 2307 // ====================================
@@ -4460,310 +2330,9 @@
4460 2330 }
4461 2331 }
4462 2332 });
4463 2333
4464 -// ============================================================================
4465 -// SATISFACTION RATING (v3.2.6)
4466 -// ============================================================================
4467 -// Per-session 👍/👎 prompt that appears in the chat-box after 60s of user
4468 -// inactivity following a bot reply. One prompt per session, deduped via
4469 -// localStorage. Runs ONLY when the satisfaction_rating_enabled option is on —
4470 -// the option (default off) is authoritative.
4471 -jQuery(function($) {
4472 - if (typeof mxchatChat === 'undefined') return;
4473 - // wp_localize_script stringifies scalars: a PHP boolean false arrives as
4474 - // '' and true as '1', so this must be an explicit-enable allowlist — the
4475 - // old "disabled when exactly false/'off'" check let '' through and the
4476 - // bubble rendered on sites with the option off/unset (plan-4bba64). PHP
4477 - // now emits 'on'/'off' strings; true/'1'/1 keep cached pre-fix HTML
4478 - // (boolean-true localizations) working.
4479 - // NOTE (plan-32db95): this gate reads the INLINE value at DOM ready and is
4480 - // deliberately NOT re-evaluated after the widget's dynamic-settings refresh
4481 - // merges fresh values over mxchatChat (that merge fires on first widget
4482 - // open, after this module has already decided). Re-evaluating would mean
4483 - // restructuring the whole module to late-bind its listeners — not worth it
4484 - // for a prompt that is at worst stale for one page load on a cached page.
4485 - var sre = mxchatChat.satisfaction_rating_enabled;
4486 - if (sre !== 'on' && sre !== true && sre !== '1' && sre !== 1) return;
4487 2334
4488 - // wp_localize_script stringifies ints, so accept both number and numeric string.
4489 - var idleRaw = mxchatChat.satisfaction_rating_idle_seconds;
4490 - var idleSeconds = (typeof idleRaw === 'number') ? idleRaw : parseInt(idleRaw, 10);
4491 - if (!isFinite(idleSeconds)) idleSeconds = 60;
4492 - if (idleSeconds < 5) idleSeconds = 5;
4493 - if (idleSeconds > 600) idleSeconds = 600;
4494 - var IDLE_MS = idleSeconds * 1000;
4495 - var MIN_BOT_REPLIES = 2;
4496 - var ratingState = {};
4497 2335
4498 - function getState(botId) {
4499 - if (!ratingState[botId]) {
4500 - ratingState[botId] = { idleTimer: null, botReplies: 0, promptShown: false, dismissed: false };
4501 - }
4502 - return ratingState[botId];
4503 - }
4504 2336
4505 - function getSessionId(botId) {
4506 - if (typeof MxChatInstances !== 'undefined' && MxChatInstances.getChatSession) {
4507 - return MxChatInstances.getChatSession(botId);
4508 - }
4509 - return null;
4510 - }
4511 2337
4512 - function isAlreadyRated(sessionId) {
4513 - if (!sessionId) return false;
4514 - try { return localStorage.getItem('mxchat_rated:' + sessionId) === '1'; } catch (e) { return false; }
4515 - }
4516 -
4517 - function markRated(sessionId) {
4518 - if (!sessionId) return;
4519 - try { localStorage.setItem('mxchat_rated:' + sessionId, '1'); } catch (e) {}
4520 - }
4521 -
4522 - function esc(s) {
4523 - return String(s == null ? '' : s)
4524 - .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
4525 - .replace(/"/g, '&quot;').replace(/'/g, '&#039;');
4526 - }
4527 -
4528 - // Mirror shouldSkipInlineColors so rating bubbles defer to AI-theme CSS.
4529 - function ratingSkipInlineColors(botId) {
4530 - if (mxchatChat.skip_inline_colors) return true;
4531 - var botAssignments = mxchatChat.bot_theme_assignments || {};
4532 - return botAssignments.hasOwnProperty(botId);
4533 - }
4534 -
4535 - function botBubbleStyleAttr(botId) {
4536 - if (ratingSkipInlineColors(botId)) return '';
4537 - var bg = mxchatChat.bot_message_bg_color;
4538 - var fg = mxchatChat.bot_message_font_color;
4539 - if (!bg && !fg) return '';
4540 - return ' style="background-color: ' + esc(bg || '') + '; color: ' + esc(fg || '') + ';"';
4541 - }
4542 -
4543 - // Reads the rating bubble's actual computed fg+bg (whatever paints it —
4544 - // the inline color pickers OR the mxchat-theme AI customizer's injected CSS)
4545 - // and paints the filled "Send" pill so it fills with the bot font color and
4546 - // labels in the bubble bg. Mirrors mxchatSyncMenuColors(~:1512) for the read.
4547 - // We paint the submit button DIRECTLY (inline longhand) rather than relying
4548 - // on the CSS rule's var()s: Chromium resolves an INHERITED custom property
4549 - // unreliably inside a descendant's `background`, so a bubble-level var would
4550 - // silently fall back to the literal (white-block bug all over again). Inline
4551 - // longhand always wins. Same transparent-guard as the menu so we never paint
4552 - // a see-through value — in that case the CSS literal fallbacks keep it legible.
4553 - function syncRatingBubbleColors(botId) {
4554 - var $chatBox = getChatBoxByBotId(botId);
4555 - if (!$chatBox || !$chatBox.length) return;
4556 - var bubbleEl = $chatBox.find('.mxchat-rating-bot-bubble').last()[0];
4557 - if (!bubbleEl) return;
4558 - var cs = window.getComputedStyle(bubbleEl);
4559 - var fg = cs.color;
4560 - var bg = cs.backgroundColor;
4561 - var hasFg = fg && fg !== 'rgba(0, 0, 0, 0)' && fg !== 'transparent';
4562 - var hasBg = bg && bg !== 'rgba(0, 0, 0, 0)' && bg !== 'transparent';
4563 - // Expose on the bubble too, for any inheriting styles / future use.
4564 - if (hasFg) bubbleEl.style.setProperty('--mxchat-bot-fg', fg);
4565 - if (hasBg) bubbleEl.style.setProperty('--mxchat-bot-bg', bg);
4566 - // Paint the Send pill directly — the part that actually fixes the bug.
4567 - var submitEl = bubbleEl.querySelector('.mxchat-rating-submit');
4568 - if (submitEl) {
4569 - if (hasFg) submitEl.style.backgroundColor = fg; // fill = bot font color
4570 - if (hasBg) submitEl.style.color = bg; // label = bubble background
4571 - }
4572 - }
4573 -
4574 - function copy(key) {
4575 - var c = mxchatChat.satisfaction_rating_copy || {};
4576 - var d = {
4577 - question: 'Was this helpful?',
4578 - helpful: 'Helpful',
4579 - not_helpful: 'Not helpful',
4580 - dismiss: 'Dismiss',
4581 - thanks: 'Thanks! Anything we should improve? (optional)',
4582 - placeholder: 'Tell us what could be better…',
4583 - send: 'Send',
4584 - skip: 'Skip',
4585 - saved: 'Thanks for the feedback.'
4586 - };
4587 - return c[key] || d[key];
4588 - }
4589 -
4590 - function thumbUpSvg() {
4591 - return '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor" aria-hidden="true"><path d="M7.493 18.75c-.425 0-.82-.236-.975-.632A7.48 7.48 0 0 1 6 15.375c0-1.75.599-3.358 1.602-4.634.151-.192.373-.309.6-.397.473-.183.89-.514 1.212-.924a9.042 9.042 0 0 1 2.861-2.4c.723-.384 1.35-.956 1.653-1.715a4.498 4.498 0 0 0 .322-1.672V2.75A.75.75 0 0 1 15 2a2.25 2.25 0 0 1 2.25 2.25c0 1.152-.26 2.243-.723 3.218-.266.558.107 1.282.725 1.282h3.126c1.026 0 1.945.694 2.054 1.715.045.422.068.85.068 1.285a11.95 11.95 0 0 1-2.649 7.521c-.388.482-.987.729-1.605.729H14.23c-.483 0-.964-.078-1.423-.23l-3.114-1.04a4.501 4.501 0 0 0-1.423-.23h-.777Z"/><path d="M2.331 10.977a11.969 11.969 0 0 0-.831 4.398 12 12 0 0 0 .52 3.507c.26.85 1.084 1.368 1.973 1.368H4.9c.445 0 .72-.498.523-.898a8.963 8.963 0 0 1-.924-3.977c0-1.708.476-3.305 1.302-4.666.245-.403-.028-.959-.5-.959H4.25c-.832 0-1.612.453-1.918 1.227Z"/></svg>';
4592 - }
4593 - function thumbDownSvg() {
4594 - return '<svg viewBox="0 0 24 24" width="18" height="18" fill="currentColor" aria-hidden="true"><path d="M15.73 5.25h1.035A7.465 7.465 0 0 1 18 9.375a7.465 7.465 0 0 1-1.235 4.125h-.148c-.806 0-1.534.446-2.031 1.08a9.04 9.04 0 0 1-2.861 2.4c-.723.384-1.35.956-1.653 1.715a4.498 4.498 0 0 0-.322 1.672V21a.75.75 0 0 1-.75.75 2.25 2.25 0 0 1-2.25-2.25c0-1.152.26-2.243.723-3.218.266-.558-.107-1.282-.725-1.282H3.622c-1.026 0-1.945-.694-2.054-1.715A12.137 12.137 0 0 1 1.5 12c0-2.848.992-5.464 2.649-7.521C4.537 3.997 5.136 3.75 5.754 3.75h4.541c.483 0 .964.078 1.423.23l3.114 1.04c.46.152.94.23 1.423.23Z"/><path d="M21.669 13.023c.536-1.362.831-2.845.831-4.398 0-1.22-.182-2.398-.52-3.507-.26-.85-1.084-1.368-1.973-1.368H19.1c-.445 0-.72.498-.523.898.591 1.2.924 2.55.924 3.977a8.958 8.958 0 0 1-1.302 4.666c-.245.403.028.959.5.959h1.053c.832 0 1.612-.453 1.918-1.227Z"/></svg>';
4595 - }
4596 -
4597 - function buildPromptHtml(botId) {
4598 - var styleAttr = botBubbleStyleAttr(botId);
4599 - return ''
4600 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4601 - + '<div class="mxchat-rating-prompt" data-bot-id="' + esc(botId) + '" role="group" aria-label="' + esc(copy('question')) + '">'
4602 - + '<div class="mxchat-rating-question">' + esc(copy('question')) + '</div>'
4603 - + '<div class="mxchat-rating-actions">'
4604 - + '<span class="mxchat-rating-buttons">'
4605 - + '<button type="button" class="mxchat-rating-btn" data-rating="1" aria-label="' + esc(copy('helpful')) + '">' + thumbUpSvg() + '</button>'
4606 - + '<button type="button" class="mxchat-rating-btn" data-rating="-1" aria-label="' + esc(copy('not_helpful')) + '">' + thumbDownSvg() + '</button>'
4607 - + '</span>'
4608 - + '<button type="button" class="mxchat-rating-dismiss" aria-label="' + esc(copy('dismiss')) + '">×</button>'
4609 - + '</div>'
4610 - + '</div>'
4611 - + '</div>';
4612 - }
4613 -
4614 - function buildFeedbackHtml(botId, rating) {
4615 - var styleAttr = botBubbleStyleAttr(botId);
4616 - return ''
4617 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4618 - + '<div class="mxchat-rating-feedback" data-bot-id="' + esc(botId) + '" data-rating="' + esc(String(rating)) + '">'
4619 - + '<div class="mxchat-rating-feedback-label">' + esc(copy('thanks')) + '</div>'
4620 - + '<textarea class="mxchat-rating-feedback-input" maxlength="500" placeholder="' + esc(copy('placeholder')) + '" rows="2"></textarea>'
4621 - + '<div class="mxchat-rating-feedback-actions">'
4622 - + '<button type="button" class="mxchat-rating-skip">' + esc(copy('skip')) + '</button>'
4623 - + '<button type="button" class="mxchat-rating-submit">' + esc(copy('send')) + '</button>'
4624 - + '</div>'
4625 - + '</div>'
4626 - + '</div>';
4627 - }
4628 -
4629 - function buildSavedHtml(botId) {
4630 - var styleAttr = botBubbleStyleAttr(botId);
4631 - return ''
4632 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4633 - + '<div class="mxchat-rating-saved">' + esc(copy('saved')) + '</div>'
4634 - + '</div>';
4635 - }
4636 -
4637 - function getChatBoxByBotId(botId) {
4638 - var $byId = $('#chat-box-' + botId);
4639 - if ($byId.length) return $byId.first();
4640 - return $('.chat-box').first();
4641 - }
4642 -
4643 - function scrollChatBoxToBottom($chatBox) {
4644 - if (!$chatBox || !$chatBox.length) return;
4645 - $chatBox.scrollTop($chatBox[0].scrollHeight);
4646 - }
4647 -
4648 - function showPrompt(botId) {
4649 - var s = getState(botId);
4650 - if (s.promptShown || s.dismissed) return;
4651 - var sessionId = getSessionId(botId);
4652 - if (!sessionId) return;
4653 - if (isAlreadyRated(sessionId)) { s.promptShown = true; return; }
4654 - var $chatBox = getChatBoxByBotId(botId);
4655 - if (!$chatBox.length) return;
4656 - if ($chatBox.find('.mxchat-rating-prompt').length) { s.promptShown = true; return; }
4657 - $chatBox.append(buildPromptHtml(botId));
4658 - syncRatingBubbleColors(botId);
4659 - s.promptShown = true;
4660 - scrollChatBoxToBottom($chatBox);
4661 - }
4662 -
4663 - function submitRating(botId, rating, feedback) {
4664 - var sessionId = getSessionId(botId);
4665 - if (!sessionId) return;
4666 - $.post(mxchatChat.ajax_url, {
4667 - action: 'mxchat_save_rating',
4668 - session_id: sessionId,
4669 - bot_id: botId,
4670 - rating: rating,
4671 - feedback: feedback || ''
4672 - });
4673 - markRated(sessionId);
4674 - }
4675 -
4676 - function onBotReply(botId) {
4677 - var s = getState(botId);
4678 - s.botReplies += 1;
4679 - if (s.promptShown || s.dismissed) return;
4680 - var sessionId = getSessionId(botId);
4681 - if (sessionId && isAlreadyRated(sessionId)) { s.promptShown = true; return; }
4682 - if (s.botReplies < MIN_BOT_REPLIES) return;
4683 - if (s.idleTimer) clearTimeout(s.idleTimer);
4684 - s.idleTimer = setTimeout(function() { showPrompt(botId); }, IDLE_MS);
4685 - }
4686 -
4687 - function onUserMessage(botId) {
4688 - var s = getState(botId);
4689 - if (s.idleTimer) { clearTimeout(s.idleTimer); s.idleTimer = null; }
4690 - }
4691 -
4692 - function botIdFromChatBox(el) {
4693 - var id = el && el.id ? el.id : '';
4694 - return id.indexOf('chat-box-') === 0 ? id.substring('chat-box-'.length) : 'default';
4695 - }
4696 -
4697 - function setupObserver(chatBox) {
4698 - var botId = botIdFromChatBox(chatBox);
4699 - try {
4700 - var observer = new MutationObserver(function(mutations) {
4701 - mutations.forEach(function(m) {
4702 - for (var i = 0; i < m.addedNodes.length; i++) {
4703 - var node = m.addedNodes[i];
4704 - if (!node || node.nodeType !== 1) continue;
4705 - var $n = $(node);
4706 - if ($n.hasClass('mxchat-rating-bot-bubble') || $n.hasClass('mxchat-rating-prompt') || $n.hasClass('mxchat-rating-feedback') || $n.hasClass('mxchat-rating-saved')) continue;
4707 - if ($n.hasClass('bot-message')) onBotReply(botId); // count at insert time — streaming providers append with .temporary-message first, then remove later (childList observer can't see attr changes)
4708 - else if ($n.hasClass('user-message')) onUserMessage(botId);
4709 - }
4710 - });
4711 - });
4712 - observer.observe(chatBox, { childList: true });
4713 - } catch (e) { /* noop */ }
4714 - }
4715 -
4716 - $('.chat-box').each(function() { setupObserver(this); });
4717 -
4718 - $(document).on('click', '.mxchat-rating-btn', function(e) {
4719 - e.preventDefault();
4720 - var $btn = $(this);
4721 - var $prompt = $btn.closest('.mxchat-rating-prompt');
4722 - var $wrap = $btn.closest('.mxchat-rating-bot-bubble');
4723 - var botId = $prompt.data('bot-id') || 'default';
4724 - var rating = parseInt($btn.attr('data-rating'), 10);
4725 - if (rating !== 1 && rating !== -1) return;
4726 - submitRating(botId, rating, '');
4727 - ($wrap.length ? $wrap : $prompt).replaceWith(buildFeedbackHtml(botId, rating));
4728 - syncRatingBubbleColors(botId);
4729 - scrollChatBoxToBottom(getChatBoxByBotId(botId));
4730 - });
4731 -
4732 - $(document).on('click', '.mxchat-rating-dismiss', function(e) {
4733 - e.preventDefault();
4734 - var $prompt = $(this).closest('.mxchat-rating-prompt');
4735 - var $wrap = $(this).closest('.mxchat-rating-bot-bubble');
4736 - var botId = $prompt.data('bot-id') || 'default';
4737 - var s = getState(botId);
4738 - s.dismissed = true;
4739 - markRated(getSessionId(botId));
4740 - ($wrap.length ? $wrap : $prompt).remove();
4741 - });
4742 -
4743 - function closeFeedback($fb) {
4744 - var botId = $fb.data('bot-id') || 'default';
4745 - var $wrap = $fb.closest('.mxchat-rating-bot-bubble');
4746 - ($wrap.length ? $wrap : $fb).replaceWith(buildSavedHtml(botId));
4747 - syncRatingBubbleColors(botId);
4748 - scrollChatBoxToBottom(getChatBoxByBotId(botId));
4749 - }
4750 -
4751 - $(document).on('click', '.mxchat-rating-skip', function(e) {
4752 - e.preventDefault();
4753 - closeFeedback($(this).closest('.mxchat-rating-feedback'));
4754 - });
4755 -
4756 - $(document).on('click', '.mxchat-rating-submit', function(e) {
4757 - e.preventDefault();
4758 - var $fb = $(this).closest('.mxchat-rating-feedback');
4759 - var botId = $fb.data('bot-id') || 'default';
4760 - var rating = parseInt($fb.attr('data-rating'), 10);
4761 - if (rating !== 1 && rating !== -1) { closeFeedback($fb); return; }
4762 - var text = String($fb.find('.mxchat-rating-feedback-input').val() || '').trim();
4763 - if (text !== '') {
4764 - submitRating(botId, rating, text);
4765 - }
4766 - closeFeedback($fb);
4767 - });
4768 -});
4769 2338