PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.0.5
MxChat – AI Chatbot & Content Generation for WordPress v3.0.5
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 +287 -1568 3.2.93.0.5 View file →
@@ -1,166 +1,6 @@
1 1 jQuery(document).ready(function($) {
2 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 -
163 3 // ====================================
164 4 // MULTI-INSTANCE MANAGEMENT SYSTEM
165 5 // ====================================
166 6
@@ -170,15 +10,11 @@
170 10
171 11 // Initialize an instance for a bot
172 12 init: function(botId) {
173 13 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 14 this.instances[botId] = {
179 15 botId: botId,
180 - sessionId: null,
16 + sessionId: this.getChatSession(botId),
181 17 lastSeenMessageId: '',
182 18 notificationCheckInterval: null,
183 19 pollingInterval: null,
184 20 processedMessageIds: new Set(),
@@ -184,11 +20,9 @@
184 20 processedMessageIds: new Set(),
185 21 activePdfFile: null,
186 22 activeWordFile: null,
187 23 chatHistoryLoaded: false,
188 - isStreaming: false,
189 - // Fresh context timestamp - only used when persistence is OFF
190 - sessionStartTimestamp: chatPersistenceEnabled ? 0 : Date.now()
24 + isStreaming: false
191 25 };
192 26 }
193 27 return this.instances[botId];
194 28 },
@@ -203,77 +37,23 @@
203 37 return Object.keys(this.instances);
204 38 },
205 39
206 40 // 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 41 getChatSession: function(botId) {
210 42 var cookieName = 'mxchat_session_id_' + botId;
211 - var storageKey = 'mxchat_session_id_' + botId;
212 43 var sessionId = getCookie(cookieName);
213 44
214 - // Fallback to localStorage if cookie is missing (e.g. cleared by browser/consent)
215 45 if (!sessionId) {
216 - try { sessionId = localStorage.getItem(storageKey); } catch (e) {}
46 + sessionId = generateSessionId();
47 + this.setChatSession(botId, sessionId);
217 48 }
218 49
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;
50 + return sessionId;
238 51 },
239 52
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 53 setChatSession: function(botId, sessionId) {
272 54 var cookieName = 'mxchat_session_id_' + botId;
273 - var storageKey = 'mxchat_session_id_' + botId;
274 55 document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
275 - try { localStorage.setItem(storageKey, sessionId); } catch (e) {}
276 56 if (this.instances[botId]) {
277 57 this.instances[botId].sessionId = sessionId;
278 58 }
279 59 },
@@ -278,10 +58,8 @@
278 58 }
279 59 },
280 60
281 61 resetChatSession: function(botId) {
282 - // Clear old session from localStorage before setting new one
283 - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {}
284 62 var newSessionId = generateSessionId();
285 63 this.setChatSession(botId, newSessionId);
286 64 var $chatBox = getElement(botId, 'chat-box');
287 65 if ($chatBox.length) {
@@ -290,20 +68,8 @@
290 68 if (this.instances[botId]) {
291 69 this.instances[botId].chatHistoryLoaded = false;
292 70 this.instances[botId].processedMessageIds = new Set();
293 71 }
294 - },
295 -
296 - // Silent reset — new session ID without clearing the chat UI
297 - // Used when IP changes mid-conversation so the user doesn't see messages vanish
298 - silentResetSession: function(botId) {
299 - try { localStorage.removeItem('mxchat_session_id_' + botId); } catch (e) {}
300 - var newSessionId = generateSessionId();
301 - this.setChatSession(botId, newSessionId);
302 - if (this.instances[botId]) {
303 - this.instances[botId].sessionId = newSessionId;
304 - }
305 - return newSessionId;
306 72 }
307 73 };
308 74
309 75 // ====================================
@@ -603,106 +369,13 @@
603 369 sendButton.disabled = false;
604 370 sendButton.style.opacity = '1';
605 371 sendButton.style.pointerEvents = 'auto';
606 372 }
607 - // Every completion path re-enables input, so this is the single restore
608 - // point for the streaming Stop affordance (no-op when not in stop mode).
609 - mxchatRestoreSendButton(botId);
610 373 }
611 374
612 -// --- Streaming Stop control -------------------------------------------------
613 -// One live stream handle per bot instance, so Stop on one widget never aborts
614 -// another bot on the same page.
615 -var mxchatActiveStreams = {};
616 -// Original send-button markup, captured once per bot the first time the Stop
617 -// state is shown (never captured while already in stop mode, so a rapid
618 -// stop-then-resend can't save the stop glyph as the "original").
619 -var mxchatSendMarkup = {};
620 -
621 -function mxchatShowStopButton(botId) {
622 - var btn = getElementDOM(botId, 'send-button');
623 - if (!btn) return;
624 - if (!btn.classList.contains('mxchat-stop-mode')) {
625 - mxchatSendMarkup[botId] = {
626 - html: btn.innerHTML,
627 - label: btn.getAttribute('aria-label')
628 - };
629 - }
630 -
631 - // Mirror the send icon's rendered size + color so the stop glyph looks
632 - // native, including custom send images/colors and theme overrides.
633 - var child = btn.querySelector('svg, img');
634 - var size = 25;
635 - var color = '';
636 - if (child) {
637 - var rect = child.getBoundingClientRect();
638 - if (rect.width) {
639 - size = Math.round(Math.min(rect.width, rect.height));
640 - }
641 - var cs = window.getComputedStyle(child);
642 - color = (child.tagName.toLowerCase() === 'svg' ? cs.fill : cs.color) || '';
643 - }
644 - var stopLabel = (typeof mxchatChat !== 'undefined' && mxchatChat.stop_button_label) || 'Stop response';
645 - 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>';
646 - // An add-on's DIRECT send-button handler (e.g. mxchat-vision's rebind) can
647 - // start a stream synchronously while the originating click is still
648 - // bubbling up to our delegated handler. Without this guard, that handler
649 - // reads the just-added stop-mode class as a user Stop press and aborts the
650 - // brand-new stream — the user's message renders but no reply ever fires
651 - // (plan-4bba64 silent message loss). The flag only spans the current event
652 - // dispatch: cleared on the next macrotask, long before a real Stop click.
653 - btn.__mxchatStopJustShown = true;
654 - setTimeout(function () { btn.__mxchatStopJustShown = false; }, 0);
655 - btn.classList.add('mxchat-stop-mode');
656 - btn.setAttribute('aria-label', stopLabel);
657 - btn.setAttribute('title', stopLabel);
658 - // disableChatInput() ran when the turn was sent; the Stop control itself
659 - // must stay clickable while the textarea remains disabled.
660 - btn.disabled = false;
661 - btn.style.opacity = '1';
662 - btn.style.pointerEvents = 'auto';
663 -}
664 -
665 -function mxchatRestoreSendButton(botId) {
666 - var btn = getElementDOM(botId, 'send-button');
667 - var saved = mxchatSendMarkup[botId];
668 - if (!btn || !btn.classList.contains('mxchat-stop-mode') || !saved) return;
669 - btn.innerHTML = saved.html;
670 - btn.classList.remove('mxchat-stop-mode');
671 - btn.removeAttribute('title');
672 - if (saved.label) {
673 - btn.setAttribute('aria-label', saved.label);
674 - }
675 -}
676 -
677 -function mxchatStopStreaming(botId) {
678 - var entry = mxchatActiveStreams[botId];
679 - if (!entry || !entry.controller) return;
680 - entry.aborted = true;
681 - try { entry.controller.abort(); } catch (e) {}
682 -}
683 -
684 -// Returns true when a stream rejection came from an intentional Stop click:
685 -// keep the partial text as the turn's answer — no error UI, no fallback resend.
686 -function mxchatHandleStreamAbort(botId, accumulatedContent, callback) {
687 - var entry = mxchatActiveStreams[botId];
688 - if (!entry || !entry.aborted) return false;
689 - delete mxchatActiveStreams[botId];
690 - if (!accumulatedContent) {
691 - // Stopped before the first chunk: drop the thinking bubble, no orphan message.
692 - getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
693 - }
694 - enableChatInput(botId); // also restores the send icon
695 - if (callback) {
696 - callback(accumulatedContent || '');
697 - }
698 - return true;
699 -}
700 -
701 375 // Update your existing sendMessage function
702 376 function sendMessage(botId) {
703 377 botId = botId || 'default';
704 - MxChatInstances.ensureSession(botId);
705 378 var $chatInput = getElement(botId, 'chat-input');
706 379 var message = $chatInput.val();
707 380
708 381 // ADD PROMPT HOOK HERE
@@ -710,14 +383,10 @@
710 383 message = customMxChatFilter(message, "prompt");
711 384 }
712 385
713 386 if (message) {
714 - // Don't disable input in live agent mode - let users chat freely
715 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
716 - var isAgentMode = modeIndicator && modeIndicator.textContent === 'Live Agent';
717 - if (!isAgentMode) {
718 - disableChatInput(botId);
719 - }
387 + // Disable input while waiting for response
388 + disableChatInput(botId);
720 389
721 390 appendMessage("user", message, '', [], false, botId);
722 391 $chatInput.val('');
723 392 $chatInput.css('height', 'auto');
@@ -727,9 +396,9 @@
727 396 }
728 397 appendThinkingMessage(botId);
729 398 scrollToBottom(botId);
730 399
731 - const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
400 + const currentModel = mxchatChat.model || 'gpt-4o';
732 401
733 402 // Check if streaming is enabled AND supported for this model
734 403 if (shouldUseStreaming(currentModel)) {
735 404 callMxChatStream(message, function(response) {
@@ -745,9 +414,8 @@
745 414
746 415 // Update your existing sendMessageToChatbot function
747 416 function sendMessageToChatbot(message, botId) {
748 417 botId = botId || 'default';
749 - MxChatInstances.ensureSession(botId);
750 418
751 419 // ADD PROMPT HOOK HERE
752 420 if (typeof customMxChatFilter === 'function') {
753 421 message = customMxChatFilter(message, "prompt");
@@ -752,14 +420,10 @@
752 420 if (typeof customMxChatFilter === 'function') {
753 421 message = customMxChatFilter(message, "prompt");
754 422 }
755 423
756 - // Don't disable input in live agent mode - let users chat freely
757 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
758 - var isAgentMode = modeIndicator && modeIndicator.textContent === 'Live Agent';
759 - if (!isAgentMode) {
760 - disableChatInput(botId);
761 - }
424 + // Disable input while waiting for response
425 + disableChatInput(botId);
762 426
763 427 var sessionId = getChatSession(botId);
764 428
765 429 if (hasQuickQuestions(botId)) {
@@ -767,9 +431,9 @@
767 431 }
768 432 appendThinkingMessage(botId);
769 433 scrollToBottom(botId);
770 434
771 - const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
435 + const currentModel = mxchatChat.model || 'gpt-4o';
772 436
773 437 // Check if streaming is enabled AND supported for this model
774 438 if (shouldUseStreaming(currentModel)) {
775 439 callMxChatStream(message, function(response) {
@@ -838,15 +502,8 @@
838 502
839 503 function callMxChat(message, callback, botId) {
840 504 botId = botId || getMxChatBotId();
841 505
842 - // Streaming fallbacks land here: drop any leftover stream handle and
843 - // return the button to its send state (no-op for plain non-stream turns).
844 - if (mxchatActiveStreams[botId]) {
845 - delete mxchatActiveStreams[botId];
846 - }
847 - mxchatRestoreSendButton(botId);
848 -
849 506 // Store the message in case we need to retry after session reset
850 507 getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', message);
851 508
852 509 // Get page context if contextual awareness is enabled
@@ -851,44 +508,24 @@
851 508
852 509 // Get page context if contextual awareness is enabled
853 510 const pageContext = getPageContext();
854 511
855 - // Get instance for session start timestamp (used when persistence is OFF)
856 - var instance = MxChatInstances.get(botId);
857 -
858 - // Guarantee a non-null session_id before the AJAX leaves. ensureSession() is idempotent
859 - // and returns the guaranteed-present session id from the in-memory instance even when
860 - // cookie/localStorage writes are silently blocked by the browser.
861 - var sessionId = MxChatInstances.ensureSession(botId);
862 - if (!sessionId || sessionId === 'null' || sessionId === 'undefined') {
863 - // Last-resort generation to ensure we never POST a null marker.
864 - sessionId = generateSessionId();
865 - MxChatInstances.setChatSession(botId, sessionId);
866 - }
867 -
868 - // Wait for the page-cache nonce refresh to complete before firing the
869 - // chat-send AJAX. On cached pages the inline mxchatChat.nonce is stale
870 - // until refreshNonceIfNeeded() returns; constructing ajaxData inside the
871 - // callback guarantees we read the fresh value. See plan-c5457f.
872 - refreshNonceIfNeeded(function() {
873 512 // Prepare AJAX data
874 513 const ajaxData = {
875 514 action: 'mxchat_handle_chat_request',
876 515 message: message,
877 - session_id: sessionId,
516 + session_id: getChatSession(botId),
878 517 nonce: mxchatChat.nonce,
879 518 current_page_url: window.location.href,
880 519 current_page_title: document.title,
881 - bot_id: botId,
882 - // Pass session start timestamp so AI context matches what user sees
883 - session_start_timestamp: instance.sessionStartTimestamp || 0
520 + bot_id: botId
884 521 };
885 -
522 +
886 523 // Add page context if available
887 524 if (pageContext) {
888 525 ajaxData.page_context = JSON.stringify(pageContext);
889 526 }
890 -
527 +
891 528 // CHECK FOR VISION FLAGS AND ADD THEM
892 529 if (window.mxchatVisionProcessed) {
893 530 ajaxData.vision_processed = true;
894 531 ajaxData.original_user_message = window.mxchatOriginalMessage || message;
@@ -897,9 +534,9 @@
897 534 window.mxchatVisionProcessed = false;
898 535 window.mxchatOriginalMessage = null;
899 536 window.mxchatVisionImagesCount = 0;
900 537 }
901 -
538 +
902 539 $.ajax({
903 540 url: mxchatChat.ajax_url,
904 541 type: 'POST',
905 542 dataType: 'json',
@@ -937,16 +574,23 @@
937 574 errorMessage = "An error occurred. Please try again or contact support.";
938 575 }
939 576
940 577 // Handle session reset action (IP changed, session expired, etc.)
941 - // Silent reset — keep chat UI intact, just get a new session and retry
942 578 if (response.data && response.data.action === 'reset_session') {
943 - MxChatInstances.silentResetSession(botId);
944 - // Re-send the original message with the new session (user message is already displayed)
579 + // Clear the old session and generate a new one
580 + resetChatSession(botId);
581 + // Remove the temporary loading message
582 + getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
583 + // Re-send the original message with the new session
945 584 var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message');
946 585 if (originalMessage) {
947 586 getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null);
948 - var currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
587 + // Re-add the user message and thinking indicator
588 + appendMessage("user", originalMessage, '', [], false, botId);
589 + appendThinkingMessage(botId);
590 + scrollToBottom(botId);
591 + // Determine whether to use streaming
592 + const currentModel = mxchatChat.model || 'gpt-4o';
949 593 if (shouldUseStreaming(currentModel)) {
950 594 callMxChatStream(originalMessage, function(response) {
951 595 getElement(botId, 'chat-box').find('.bot-message.temporary-message').removeClass('temporary-message');
952 596 }, botId);
@@ -1003,11 +647,9 @@
1003 647 }
1004 648
1005 649 // Check for live agent response
1006 650 if (response.success && response.data && response.data.status === 'waiting_for_agent') {
1007 - removeThinkingDots(botId);
1008 651 updateChatModeIndicator('agent', botId);
1009 - enableChatInput(botId);
1010 652 return;
1011 653 }
1012 654
1013 655 // Handle the message and show notification if chat is hidden
@@ -1040,13 +682,9 @@
1040 682 $badge.show();
1041 683 }
1042 684 }
1043 685 } else {
1044 - var emptyMsg = "I received an empty response. Please try again or contact support if this persists.";
1045 - if (response.vectorstore_error) {
1046 - emptyMsg = "I received an empty response. Debug info: " + response.vectorstore_error;
1047 - }
1048 - replaceLastMessage("bot", emptyMsg, '', [], botId);
686 + replaceLastMessage("bot", "I received an empty response. Please try again or contact support if this persists.", '', [], botId);
1049 687 }
1050 688
1051 689 if (response.message_id) {
1052 690 var instance = MxChatInstances.get(botId);
@@ -1088,9 +726,8 @@
1088 726
1089 727 replaceLastMessage("bot", errorMessage, '', [], botId);
1090 728 }
1091 729 });
1092 - }); // refreshNonceIfNeeded
1093 730 }
1094 731
1095 732 function callMxChatStream(message, callback, botId) {
1096 733 botId = botId || getMxChatBotId();
@@ -1097,9 +734,9 @@
1097 734
1098 735 // Store the message in case we need to retry after session reset
1099 736 getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', message);
1100 737
1101 - const currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
738 + const currentModel = mxchatChat.model || 'gpt-4o';
1102 739 if (!isStreamingSupported(currentModel)) {
1103 740 callMxChat(message, callback, botId);
1104 741 return;
1105 742 }
@@ -1106,35 +743,17 @@
1106 743
1107 744 // Get page context if contextual awareness is enabled
1108 745 const pageContext = getPageContext();
1109 746
1110 - // Get instance for session start timestamp (used when persistence is OFF)
1111 - var instance = MxChatInstances.get(botId);
1112 -
1113 - // Guarantee a non-null session_id before the fetch. FormData.append() stringifies any
1114 - // non-string value via String(), so passing `null` would POST the literal string "null"
1115 - // and land in the transcripts table as a ghost session. ensureSession() always returns
1116 - // a real string even when cookies/localStorage are blocked.
1117 - var streamSessionId = MxChatInstances.ensureSession(botId);
1118 - if (!streamSessionId || streamSessionId === 'null' || streamSessionId === 'undefined') {
1119 - streamSessionId = generateSessionId();
1120 - MxChatInstances.setChatSession(botId, streamSessionId);
1121 - }
1122 -
1123 - // Wait for the page-cache nonce refresh before constructing formData (which
1124 - // captures mxchatChat.nonce by value). Mirrors callMxChat's wrapping. See plan-c5457f.
1125 - refreshNonceIfNeeded(function() {
1126 747 const formData = new FormData();
1127 748 formData.append('action', 'mxchat_stream_chat');
1128 749 formData.append('message', message);
1129 - formData.append('session_id', streamSessionId);
750 + formData.append('session_id', getChatSession(botId));
1130 751 formData.append('nonce', mxchatChat.nonce);
1131 752 formData.append('current_page_url', window.location.href);
1132 753 formData.append('current_page_title', document.title);
1133 754 formData.append('bot_id', botId);
1134 - // Pass session start timestamp so AI context matches what user sees
1135 - formData.append('session_start_timestamp', instance.sessionStartTimestamp || 0);
1136 -
755 +
1137 756 // Add page context if available
1138 757 if (pageContext) {
1139 758 formData.append('page_context', JSON.stringify(pageContext));
1140 759 }
@@ -1153,20 +772,12 @@
1153 772 let accumulatedContent = '';
1154 773 let testingDataReceived = false;
1155 774 let streamingStarted = false;
1156 775
1157 - // Abortable stream: a fresh controller per turn, keyed by bot instance.
1158 - // The Stop control (send button swapped in place) aborts both the read
1159 - // loop and the underlying request.
1160 - var streamControl = { controller: new AbortController(), aborted: false };
1161 - mxchatActiveStreams[botId] = streamControl;
1162 - mxchatShowStopButton(botId);
1163 -
1164 776 fetch(mxchatChat.ajax_url, {
1165 777 method: 'POST',
1166 778 body: formData,
1167 - credentials: 'same-origin',
1168 - signal: streamControl.controller.signal
779 + credentials: 'same-origin'
1169 780 })
1170 781 .then(response => {
1171 782 // Store the response for potential fallback handling
1172 783 const responseClone = response.clone();
@@ -1235,16 +846,8 @@
1235 846
1236 847 // Re-enable chat input when stream ends with content
1237 848 enableChatInput(botId);
1238 849
1239 - // Scroll the user's last message to the top now that the
1240 - // bot's full reply has rendered (gives max reading room).
1241 - var $chatBoxDone = getElement(botId, 'chat-box');
1242 - var $lastUserMsgDone = $chatBoxDone.find('.user-message').last();
1243 - if ($lastUserMsgDone.length) {
1244 - scrollElementToTop($lastUserMsgDone, botId);
1245 - }
1246 -
1247 850 if (callback) {
1248 851 callback(accumulatedContent);
1249 852 }
1250 853 return;
@@ -1267,16 +870,8 @@
1267 870
1268 871 // Re-enable chat input after streaming completes
1269 872 enableChatInput(botId);
1270 873
1271 - // Scroll the user's last message to the top now
1272 - // that the bot's full reply has rendered.
1273 - var $chatBoxStreamDone = getElement(botId, 'chat-box');
1274 - var $lastUserMsgStreamDone = $chatBoxStreamDone.find('.user-message').last();
1275 - if ($lastUserMsgStreamDone.length) {
1276 - scrollElementToTop($lastUserMsgStreamDone, botId);
1277 - }
1278 -
1279 874 if (callback) {
1280 875 callback(accumulatedContent);
1281 876 }
1282 877 return;
@@ -1333,9 +928,8 @@
1333 928 }
1334 929
1335 930 processStream();
1336 931 }).catch(streamError => {
1337 - if (mxchatHandleStreamAbort(botId, accumulatedContent, callback)) return;
1338 932 getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1339 933 callMxChat(message, callback, botId);
1340 934 });
1341 935 }
@@ -1342,9 +936,8 @@
1342 936
1343 937 processStream();
1344 938 })
1345 939 .catch(error => {
1346 - if (mxchatHandleStreamAbort(botId, accumulatedContent, callback)) return;
1347 940 // Check if we have server error data with chat mode
1348 941 if (error && error.isServerError && error.data) {
1349 942 // Check for chat mode in error data
1350 943 if (error.data.chat_mode) {
@@ -1357,9 +950,8 @@
1357 950 getElement(botId, 'chat-box').find('.bot-message.temporary-message').remove();
1358 951 callMxChat(message, callback, botId);
1359 952 }
1360 953 });
1361 - }); // refreshNonceIfNeeded
1362 954 }
1363 955
1364 956 // Helper function to handle non-streaming responses
1365 957 function handleNonStreamResponse(data, callback, botId) {
@@ -1398,16 +990,21 @@
1398 990 errorMessage = "An error occurred. Please try again or contact support.";
1399 991 }
1400 992
1401 993 // Handle session reset action (IP changed, session expired, etc.)
1402 - // Silent reset — keep chat UI intact, just get a new session and retry
1403 994 if (data.data && data.data.action === 'reset_session') {
1404 - MxChatInstances.silentResetSession(botId);
1405 - // Re-send the original message with the new session (user message is already displayed)
995 + // Clear the old session and generate a new one
996 + resetChatSession(botId);
997 + // Re-send the original message with the new session
1406 998 var originalMessage = getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message');
1407 999 if (originalMessage) {
1408 1000 getElement(botId, 'mxchat-chatbot-wrapper').find('.mxchat-input-holder textarea').data('pending-message', null);
1409 - var currentModel = mxchatChat.model || 'gpt-5.1-chat-latest';
1001 + // Re-add the user message and thinking indicator
1002 + appendMessage("user", originalMessage, '', [], false, botId);
1003 + appendThinkingMessage(botId);
1004 + scrollToBottom(botId);
1005 + // Determine whether to use streaming
1006 + const currentModel = mxchatChat.model || 'gpt-4o';
1410 1007 if (shouldUseStreaming(currentModel)) {
1411 1008 callMxChatStream(originalMessage, callback, botId);
1412 1009 } else {
1413 1010 callMxChat(originalMessage, callback, botId);
@@ -1429,22 +1026,8 @@
1429 1026 }
1430 1027 return; // Exit early for errors
1431 1028 }
1432 1029
1433 - // Check for live agent response
1434 - if (data.success && data.data && data.data.status === 'waiting_for_agent') {
1435 - removeThinkingDots(botId);
1436 - // Also remove any leftover bot-message that lost its temporary-message class
1437 - var $chatBox = getElement(botId, 'chat-box');
1438 - $chatBox.find('.bot-message .thinking-dots').closest('.bot-message').remove();
1439 - updateChatModeIndicator('agent', botId);
1440 - enableChatInput(botId);
1441 - if (callback) {
1442 - callback('');
1443 - }
1444 - return;
1445 - }
1446 -
1447 1030 // Handle different response formats
1448 1031 if (data.text || data.html || data.message) {
1449 1032
1450 1033 // Apply response hooks
@@ -1489,15 +1072,19 @@
1489 1072 }
1490 1073
1491 1074 // Enhanced updateChatModeIndicator function for immediate DOM updates
1492 1075 function updateChatModeIndicator(mode, botId) {
1076 + console.log('[MxChat] updateChatModeIndicator called with mode:', mode, 'botId:', botId);
1493 1077 botId = botId || 'default';
1494 1078 const indicator = getElementDOM(botId, 'chat-mode-indicator');
1079 + console.log('[MxChat] chat-mode-indicator element found:', !!indicator);
1495 1080 if (indicator) {
1496 1081 const oldText = indicator.textContent;
1082 + console.log('[MxChat] Current indicator text:', oldText, '-> changing to mode:', mode);
1497 1083
1498 1084 if (mode === 'agent') {
1499 1085 indicator.textContent = 'Live Agent';
1086 + console.log('[MxChat] Mode is agent, calling startPolling...');
1500 1087 startPolling(botId);
1501 1088 } else {
1502 1089 // Everything else is AI mode
1503 1090 const customAiText = indicator.getAttribute('data-ai-text') || 'AI Agent';
@@ -1568,21 +1155,9 @@
1568 1155 // Update the event handlers to use the correct function names (using event delegation)
1569 1156 // Use class-based selectors for multi-instance support
1570 1157 $(document).on('click', '.send-button', function() {
1571 1158 var botId = getBotIdFromElement(this);
1572 - // While a response is streaming the button is a Stop control.
1573 - if (this.classList.contains('mxchat-stop-mode')) {
1574 - // Same click that just started this stream (an add-on's direct handler
1575 - // ran before this delegated one) — not a Stop press. See
1576 - // mxchatShowStopButton for the full story (plan-4bba64).
1577 - if (this.__mxchatStopJustShown) return;
1578 - mxchatStopStreaming(botId);
1579 - return;
1580 - }
1581 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
1582 - if (!(modeIndicator && modeIndicator.textContent === 'Live Agent')) {
1583 - disableChatInput(botId);
1584 - }
1159 + disableChatInput(botId);
1585 1160 sendMessage(botId);
1586 1161 });
1587 1162
1588 1163 // Override enter key handler (using event delegation)
@@ -1589,295 +1164,14 @@
1589 1164 $(document).on('keypress', '.chat-input', function(e) {
1590 1165 if (e.which == 13 && !e.shiftKey) {
1591 1166 e.preventDefault();
1592 1167 var botId = getBotIdFromElement(this);
1593 - var modeIndicator = getElementDOM(botId, 'chat-mode-indicator');
1594 - if (!(modeIndicator && modeIndicator.textContent === 'Live Agent')) {
1595 - disableChatInput(botId);
1596 - }
1168 + disableChatInput(botId);
1597 1169 sendMessage(botId);
1598 1170 }
1599 1171 });
1600 1172
1601 -// Builds the list of overflow-menu items for a given bot.
1602 -// Adding a future item is one push to this array — do NOT hardcode "only download."
1603 -function mxchatGetHeaderMenuItems(botId) {
1604 - var items = [];
1605 - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {};
1606 -
1607 - // The `print_button_*` keys still gate this item for back-compat with
1608 - // existing user options. The action is now a transcript download, not print.
1609 - if (settings.print_button_enabled === 'on') {
1610 - items.push({
1611 - id: 'download-transcript',
1612 - label: settings.print_button_label || 'Download Transcript',
1613 - 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>',
1614 - action: function() {
1615 - mxchatDownloadTranscript(botId);
1616 - }
1617 - });
1618 - }
1619 -
1620 - return items;
1621 -}
1622 -
1623 -// Builds a clean markdown transcript of the current conversation and triggers
1624 -// a file download. Used by the "Download Transcript" menu item.
1625 -function mxchatDownloadTranscript(botId) {
1626 - var $chatBox = getElement(botId, 'chat-box');
1627 - if (!$chatBox || !$chatBox.length) return;
1628 -
1629 - var settings = (typeof mxchatChat !== 'undefined') ? mxchatChat : {};
1630 - var headerTitle = settings.print_header_title || 'Chat transcript';
1631 - var now = new Date();
1632 - var stamp = now.toLocaleString();
1633 -
1634 - var lines = [];
1635 - lines.push('# ' + headerTitle);
1636 - lines.push('');
1637 - lines.push('Exported: ' + stamp);
1638 - lines.push('');
1639 - lines.push('---');
1640 - lines.push('');
1641 -
1642 - $chatBox.find('.user-message, .bot-message, .agent-message').each(function() {
1643 - var $msg = $(this);
1644 - // Skip thinking placeholders and any in-flight temporary messages.
1645 - if ($msg.find('.thinking-dots').length) return;
1646 - if ($msg.hasClass('temporary-message')) return;
1647 -
1648 - var sender;
1649 - if ($msg.hasClass('user-message')) sender = 'User';
1650 - else if ($msg.hasClass('agent-message')) sender = 'Live Agent';
1651 - else sender = 'AI Agent';
1652 -
1653 - // Strip interactive UI from the cloned message so we get the conversation text.
1654 - var $clone = $msg.clone();
1655 - $clone.find('.copy-button, .message-toolbar, .mxchat-copy, button, script, style').remove();
1656 - var text = $clone.text().replace(/ /g, ' ').replace(/[ \t]+\n/g, '\n').replace(/\n{3,}/g, '\n\n').trim();
1657 - if (!text) return;
1658 -
1659 - lines.push('**' + sender + '**');
1660 - lines.push('');
1661 - lines.push(text);
1662 - lines.push('');
1663 - });
1664 -
1665 - var content = lines.join('\n');
1666 - var iso = now.toISOString().replace(/[:.]/g, '-').slice(0, 19);
1667 - var fname = 'mxchat-transcript-' + iso + '.md';
1668 - var blob = new Blob([content], { type: 'text/markdown;charset=utf-8' });
1669 - var url = URL.createObjectURL(blob);
1670 - var a = document.createElement('a');
1671 - a.href = url;
1672 - a.download = fname;
1673 - a.style.display = 'none';
1674 - document.body.appendChild(a);
1675 - a.click();
1676 - setTimeout(function() {
1677 - if (a.parentNode) a.parentNode.removeChild(a);
1678 - URL.revokeObjectURL(url);
1679 - }, 100);
1680 -}
1681 -
1682 -// Reads the bot bubble's actual computed bg+fg and writes them as CSS vars
1683 -// on the menu wrap, so the dropdown matches whatever paints the bubble —
1684 -// saved options, AI theme CSS, or the mxchat-theme add-on.
1685 -function mxchatSyncMenuColors(botId, $wrap) {
1686 - if (!$wrap || !$wrap.length) return;
1687 - var $bot = $wrap.closest('.mxchat-chatbot-wrapper').find('.bot-message').not('.temporary-message').first();
1688 - if (!$bot.length) return;
1689 - var cs = window.getComputedStyle($bot[0]);
1690 - if (cs.backgroundColor && cs.backgroundColor !== 'rgba(0, 0, 0, 0)' && cs.backgroundColor !== 'transparent') {
1691 - $wrap[0].style.setProperty('--mxchat-menu-bg', cs.backgroundColor);
1692 - }
1693 - // Bot text color usually lives on a child div, not .bot-message itself.
1694 - var $textChild = $bot.find('[style*="color"]').first();
1695 - var fg = ($textChild.length ? window.getComputedStyle($textChild[0]).color : cs.color);
1696 - if (fg) $wrap[0].style.setProperty('--mxchat-menu-fg', fg);
1697 -}
1698 -
1699 -// Renders (or re-renders) the item list for one menu wrap. Split out of
1700 -// mxchatInitHeaderMenu so the dynamic-settings merge (plan-32db95) can
1701 -// rebuild items + trigger visibility WITHOUT re-binding the one-time
1702 -// open/close/keyboard wiring. closeMenu is passed in by the init closure;
1703 -// a rebuild before init (never happens, but harmless) just skips it.
1704 -function mxchatRenderHeaderMenuItems(botId, $wrap, closeMenuFn) {
1705 - var $trigger = $wrap.find('.mxchat-menu-trigger');
1706 - var $menu = $wrap.find('.mxchat-header-menu');
1707 - var items = mxchatGetHeaderMenuItems(botId);
1708 -
1709 - $menu.empty();
1710 -
1711 - if (!items.length) {
1712 - $trigger.hide();
1713 - $menu.hide();
1714 - return;
1715 - }
1716 -
1717 - // Clear any inline display:none a previous zero-item render left behind —
1718 - // open/close visibility is governed by the hidden prop + is-open class.
1719 - $trigger.css('display', '');
1720 - $menu.css('display', '');
1721 -
1722 - items.forEach(function(item, idx) {
1723 - var $btn = $('<button>', {
1724 - type: 'button',
1725 - 'class': 'mxchat-menu-item',
1726 - 'role': 'menuitem',
1727 - 'tabindex': '-1',
1728 - 'data-menu-id': item.id,
1729 - html: '<span class="mxchat-menu-item-icon">' + item.icon + '</span>' +
1730 - '<span class="mxchat-menu-item-label"></span>'
1731 - });
1732 - $btn.find('.mxchat-menu-item-label').text(item.label);
1733 - $btn.on('click', function(e) {
1734 - e.preventDefault();
1735 - e.stopPropagation();
1736 - if (closeMenuFn) closeMenuFn();
1737 - try { item.action(); } catch (err) { /* no-op */ }
1738 - });
1739 - $menu.append($btn);
1740 - });
1741 -}
1742 -
1743 -// Re-render every menu on the page after a dynamic-settings merge
1744 -// (multi-bot: each wrap re-reads its items). An OPEN menu is left alone —
1745 -// swapping items under the user mid-interaction yanks focus — and the
1746 -// rebuild runs when it closes instead (closeMenu checks the pending flag).
1747 -function mxchatRebuildHeaderMenus() {
1748 - $('.mxchat-header-menu-wrap').each(function() {
1749 - var $wrap = $(this);
1750 - var botId = $wrap.data('bot-id');
1751 - if (!botId) return;
1752 - if (!$wrap.data('mxchatMenuReady')) {
1753 - mxchatInitHeaderMenu(botId);
1754 - return;
1755 - }
1756 - if ($wrap.find('.mxchat-header-menu').hasClass('is-open')) {
1757 - $wrap.data('mxchatMenuRebuildPending', true);
1758 - return;
1759 - }
1760 - mxchatRenderHeaderMenuItems(botId, $wrap, $wrap.data('mxchatMenuClose'));
1761 - });
1762 -}
1763 -
1764 -// One-time per-widget init: renders menu items, wires open/close,
1765 -// outside-click, Escape, and arrow-key navigation. If no items, hides the
1766 -// trigger. Wiring happens even when there are zero items at init, so a
1767 -// later dynamic-settings rebuild that adds items has a working trigger.
1768 -function mxchatInitHeaderMenu(botId) {
1769 - var $wrap = $('.mxchat-header-menu-wrap[data-bot-id="' + botId + '"]').first();
1770 - if (!$wrap.length || $wrap.data('mxchatMenuReady')) return;
1771 -
1772 - var $trigger = $wrap.find('.mxchat-menu-trigger');
1773 - var $menu = $wrap.find('.mxchat-header-menu');
1774 -
1775 - // Initial color sync — covers normal page load.
1776 - mxchatSyncMenuColors(botId, $wrap);
1777 -
1778 - function openMenu() {
1779 - // Re-sync each open in case the active theme changed since init.
1780 - mxchatSyncMenuColors(botId, $wrap);
1781 - $menu.prop('hidden', false).attr('aria-hidden', 'false').addClass('is-open');
1782 - $trigger.attr('aria-expanded', 'true');
1783 - // Focus the first item for keyboard users
1784 - setTimeout(function() {
1785 - $menu.find('.mxchat-menu-item').first().attr('tabindex', '0').trigger('focus');
1786 - }, 0);
1787 - }
1788 - function closeMenu(returnFocus) {
1789 - $menu.prop('hidden', true).attr('aria-hidden', 'true').removeClass('is-open');
1790 - $trigger.attr('aria-expanded', 'false');
1791 - $menu.find('.mxchat-menu-item').attr('tabindex', '-1');
1792 - if (returnFocus) $trigger.trigger('focus');
1793 - // A dynamic-settings rebuild that arrived while the menu was open
1794 - // was deferred (mxchatRebuildHeaderMenus) — run it now.
1795 - if ($wrap.data('mxchatMenuRebuildPending')) {
1796 - $wrap.removeData('mxchatMenuRebuildPending');
1797 - mxchatRenderHeaderMenuItems(botId, $wrap, closeMenu);
1798 - }
1799 - }
1800 -
1801 - // Toggle on trigger click — stop propagation so the .chatbot-top-bar
1802 - // click-to-collapse handler does not fire.
1803 - $trigger.on('click', function(e) {
1804 - e.preventDefault();
1805 - e.stopPropagation();
1806 - if ($menu.hasClass('is-open')) closeMenu();
1807 - else openMenu();
1808 - });
1809 -
1810 - // Don't let clicks inside the menu bubble to the top-bar collapse handler.
1811 - $menu.on('click', function(e) {
1812 - e.stopPropagation();
1813 - });
1814 -
1815 - // Outside click closes the menu.
1816 - $(document).on('click.mxchatMenu-' + botId, function(e) {
1817 - if (!$menu.hasClass('is-open')) return;
1818 - if ($wrap.has(e.target).length || $wrap.is(e.target)) return;
1819 - closeMenu();
1820 - });
1821 -
1822 - // Keyboard: Escape closes and returns focus; arrow keys move focus; Enter activates.
1823 - $menu.on('keydown', '.mxchat-menu-item', function(e) {
1824 - var $items = $menu.find('.mxchat-menu-item');
1825 - var idx = $items.index(this);
1826 - if (e.key === 'Escape') {
1827 - e.preventDefault();
1828 - closeMenu(true);
1829 - } else if (e.key === 'ArrowDown') {
1830 - e.preventDefault();
1831 - var $next = $items.eq((idx + 1) % $items.length);
1832 - $items.attr('tabindex', '-1');
1833 - $next.attr('tabindex', '0').trigger('focus');
1834 - } else if (e.key === 'ArrowUp') {
1835 - e.preventDefault();
1836 - var $prev = $items.eq((idx - 1 + $items.length) % $items.length);
1837 - $items.attr('tabindex', '-1');
1838 - $prev.attr('tabindex', '0').trigger('focus');
1839 - } else if (e.key === 'Enter' || e.key === ' ') {
1840 - e.preventDefault();
1841 - $(this).trigger('click');
1842 - }
1843 - });
1844 - $trigger.on('keydown', function(e) {
1845 - if (e.key === 'Escape' && $menu.hasClass('is-open')) {
1846 - e.preventDefault();
1847 - closeMenu(true);
1848 - } else if ((e.key === 'ArrowDown' || e.key === 'Enter' || e.key === ' ') && !$menu.hasClass('is-open')) {
1849 - e.preventDefault();
1850 - openMenu();
1851 - }
1852 - });
1853 -
1854 - // Expose closeMenu for out-of-closure re-renders (mxchatRebuildHeaderMenus),
1855 - // then do the initial item render.
1856 - $wrap.data('mxchatMenuClose', closeMenu);
1857 - mxchatRenderHeaderMenuItems(botId, $wrap, closeMenu);
1858 -
1859 - $wrap.data('mxchatMenuReady', true);
1860 -}
1861 -
1862 -// Initialize header menus for every rendered widget on DOM ready.
1863 -$(function() {
1864 - $('.mxchat-header-menu-wrap').each(function() {
1865 - var botId = $(this).data('bot-id');
1866 - if (botId) mxchatInitHeaderMenu(botId);
1867 - });
1868 -
1869 - // Embedded (non-floating) widgets are open from the moment the page
1870 - // renders — refresh dynamic settings at init (plan-32db95). Floating
1871 - // widgets refresh on first launcher open instead.
1872 - var hasEmbeddedWidget = $('.mxchat-chatbot-wrapper').filter(function() {
1873 - return !$(this).closest('.floating-chatbot').length;
1874 - }).length > 0;
1875 - if (hasEmbeddedWidget) {
1876 - mxchatRefreshDynamicSettings();
1877 - }
1878 -});
1879 -
1173 +
1880 1174 function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false, botId = 'default') {
1881 1175 try {
1882 1176 // Determine styles based on sender type
1883 1177 let messageClass, bgColor, fontColor;
@@ -1915,12 +1209,17 @@
1915 1209 'margin-bottom': '1em'
1916 1210 });
1917 1211 }
1918 1212
1919 - // Process the message content - always run linkify to convert markdown
1920 - // links and format text. linkify() handles existing HTML safely via
1921 - // negative lookaheads that skip URLs already inside <a> tags.
1922 - let fullMessage = linkify(messageText);
1213 + // Process the message content based on sender
1214 + let fullMessage;
1215 + if (sender === "user") {
1216 + // For user messages, apply linkify after sanitization
1217 + fullMessage = linkify(messageText);
1218 + } else {
1219 + // For bot/agent messages, preserve HTML
1220 + fullMessage = messageText;
1221 + }
1923 1222
1924 1223 // Add images if provided
1925 1224 if (images && images.length > 0) {
1926 1225 fullMessage += '<div class="image-gallery" dir="auto">';
@@ -1969,12 +1268,8 @@
1969 1268 if (lastUserMessage.length) {
1970 1269 scrollElementToTop(lastUserMessage, botId);
1971 1270 }
1972 1271 }
1973 -
1974 - if ((sender === "bot" || sender === "agent") && !isTemporary) {
1975 - if (typeof mxchatInitHeaderMenu === 'function') mxchatInitHeaderMenu(botId);
1976 - }
1977 1272 });
1978 1273
1979 1274 if (messageText.id) {
1980 1275 var instance = MxChatInstances.get(botId);
@@ -2059,12 +1354,26 @@
2059 1354 bgColor = botMessageBgColor;
2060 1355 fontColor = botMessageFontColor;
2061 1356 }
2062 1357
2063 - // Always run linkify to convert markdown links and format text.
2064 - // linkify() already handles existing HTML (its URL patterns use negative lookaheads
2065 - // to avoid double-processing URLs that are already inside <a> tags).
2066 - var fullMessage = linkify(responseText);
1358 + // FIXED: Only linkify if response doesn't already contain HTML links or tags
1359 + // This prevents double-processing of URLs that are already formatted as HTML
1360 + var fullMessage;
1361 + if (sender === "user") {
1362 + // Always linkify user messages (they're plain text)
1363 + fullMessage = linkify(responseText);
1364 + } else {
1365 + // For bot/agent messages, check if HTML already exists
1366 + if (responseText.includes('<a href=') || responseText.includes('</a>') ||
1367 + responseText.includes('<img') || responseText.includes('<div') ||
1368 + responseText.includes('<p>') || responseText.includes('<br>')) {
1369 + // Response already has HTML, don't process it
1370 + fullMessage = responseText;
1371 + } else {
1372 + // Plain text response, apply linkify
1373 + fullMessage = linkify(responseText);
1374 + }
1375 + }
2067 1376
2068 1377 if (responseHtml) {
2069 1378 // Only add line breaks if there's actual text content before the HTML
2070 1379 if (fullMessage && fullMessage.trim()) {
@@ -2121,12 +1430,8 @@
2121 1430 }
2122 1431
2123 1432 // Re-enable chat input after response is displayed
2124 1433 enableChatInput(botId);
2125 -
2126 - if (sender === "bot" || sender === "agent") {
2127 - if (typeof mxchatInitHeaderMenu === 'function') mxchatInitHeaderMenu(botId);
2128 - }
2129 1434 } else {
2130 1435 appendMessage(sender, responseText, responseHtml, images, false, botId);
2131 1436 // Re-enable chat input after response is displayed
2132 1437 enableChatInput(botId);
@@ -2135,15 +1440,8 @@
2135 1440
2136 1441
2137 1442 function appendThinkingMessage(botId) {
2138 1443 botId = botId || 'default';
2139 -
2140 - // Don't show thinking dots in live agent mode - message is just forwarded to a human
2141 - var indicator = getElementDOM(botId, 'chat-mode-indicator');
2142 - if (indicator && indicator.textContent === 'Live Agent') {
2143 - return;
2144 - }
2145 -
2146 1444 var $chatBox = getElement(botId, 'chat-box');
2147 1445
2148 1446 // Remove any existing thinking dots in this bot's chat first
2149 1447 $chatBox.find('.thinking-dots').remove();
@@ -2165,9 +1463,9 @@
2165 1463 '</div>' +
2166 1464 '</div>';
2167 1465
2168 1466 // Append the thinking dots to this bot's chat container - skip inline colors if AI theme is active
2169 - var messageStyle = skipColors ? '' : ' style="background-color: ' + botMessageBgColor + '; color: ' + botMessageFontColor + ';"';
1467 + var messageStyle = skipColors ? '' : ' style="background-color: ' + botMessageBgColor + ';"';
2170 1468 $chatBox.append('<div class="bot-message temporary-message"' + messageStyle + '>' + thinkingHtml + '</div>');
2171 1469 scrollToBottom(botId);
2172 1470 }
2173 1471
@@ -2173,11 +1471,9 @@
2173 1471
2174 1472 function removeThinkingDots(botId) {
2175 1473 botId = botId || 'default';
2176 1474 var $chatBox = getElement(botId, 'chat-box');
2177 - // Remove by temporary-message class first, then fall back to any bot-message containing thinking dots
2178 1475 $chatBox.find('.thinking-dots').closest('.temporary-message').remove();
2179 - $chatBox.find('.bot-message .thinking-dots').closest('.bot-message').remove();
2180 1476 }
2181 1477
2182 1478 // ====================================
2183 1479 // TEXT FORMATTING & PROCESSING
@@ -2211,12 +1507,9 @@
2211 1507 processedText = formatTextStyling(processedText);
2212 1508
2213 1509 // Process code blocks BEFORE processing links
2214 1510 processedText = formatCodeBlocks(processedText);
2215 -
2216 - // Process markdown tables BEFORE converting newlines to paragraphs
2217 - processedText = formatMarkdownTables(processedText);
2218 -
1511 +
2219 1512 // NOW convert to paragraphs
2220 1513 processedText = convertNewlinesToBreaks(processedText);
2221 1514
2222 1515 // IMPORTANT: Handle citation-style brackets FIRST [URL]
@@ -2229,63 +1522,37 @@
2229 1522 // Return as a proper link without the brackets
2230 1523 return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
2231 1524 });
2232 1525
2233 - // Process markdown links: [text](url) and [](url)
2234 - // Uses balanced parenthesis matching to handle URLs containing parens
2235 - // (e.g. PDF filenames with dates like (2025-08-28).pdf)
2236 - processedText = (function(input) {
2237 - var result = '';
2238 - var i = 0;
2239 - while (i < input.length) {
2240 - // Look for [ at current position
2241 - if (input[i] === '[') {
2242 - // Find closing ]
2243 - var closeBracket = input.indexOf(']', i + 1);
2244 - if (closeBracket === -1 || closeBracket + 1 >= input.length || input[closeBracket + 1] !== '(') {
2245 - result += input[i];
2246 - i++;
2247 - continue;
2248 - }
2249 - var linkText = input.substring(i + 1, closeBracket);
2250 - // Check if URL starts with http
2251 - var urlStart = closeBracket + 2;
2252 - if (!input.substring(urlStart).match(/^https?:\/\//)) {
2253 - result += input[i];
2254 - i++;
2255 - continue;
2256 - }
2257 - // Find balanced closing paren
2258 - var depth = 1;
2259 - var j = urlStart;
2260 - while (j < input.length && depth > 0) {
2261 - if (input[j] === '(') depth++;
2262 - else if (input[j] === ')') depth--;
2263 - if (depth > 0) j++;
2264 - }
2265 - if (depth !== 0) {
2266 - result += input[i];
2267 - i++;
2268 - continue;
2269 - }
2270 - var url = input.substring(urlStart, j);
2271 - var cleanUrl = url.replace(/[\].,;!?]+$/, '');
2272 - var encodedUrl = safeEncodeUrl(cleanUrl);
2273 - if (!linkText || !linkText.trim()) {
2274 - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + cleanUrl + '</a>';
2275 - } else {
2276 - var safeText = sanitizeUserInput(linkText);
2277 - result += '<a href="' + encodedUrl + '" target="' + linkTarget + '">' + safeText + '</a>';
2278 - }
2279 - i = j + 1; // Skip past the closing )
2280 - } else {
2281 - result += input[i];
2282 - i++;
2283 - }
1526 + // Process proper markdown links with text: [text](url)
1527 + // This MUST have non-empty text in the first brackets
1528 + const markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s)]+)\)/g;
1529 + processedText = processedText.replace(markdownLinkPattern, (match, text, url) => {
1530 + // Make sure we have actual text (not just whitespace)
1531 + if (!text || !text.trim()) {
1532 + // If no text, treat the URL as the text
1533 + let cleanUrl = url.replace(/[.,;!?]+$/, '');
1534 + const safeUrl = safeEncodeUrl(cleanUrl);
1535 + return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
2284 1536 }
2285 - return result;
2286 - })(processedText);
1537 +
1538 + // Clean the URL
1539 + let cleanUrl = url.replace(/[\].,;!?]+$/, '');
1540 + const safeUrl = safeEncodeUrl(cleanUrl);
1541 + const safeText = sanitizeUserInput(text);
1542 + return `<a href="${safeUrl}" target="${linkTarget}">${safeText}</a>`;
1543 + });
2287 1544
1545 + // Handle empty markdown links: [](url)
1546 + // This is a specific case where there's no text
1547 + const emptyMarkdownPattern = /\[\]\((https?:\/\/[^\s)]+)\)/g;
1548 + processedText = processedText.replace(emptyMarkdownPattern, (match, url) => {
1549 + let cleanUrl = url.replace(/[.,;!?]+$/, '');
1550 + const safeUrl = safeEncodeUrl(cleanUrl);
1551 + // Use the URL itself as the link text
1552 + return `<a href="${safeUrl}" target="${linkTarget}">${cleanUrl}</a>`;
1553 + });
1554 +
2288 1555 // Process phone numbers: [text](tel:number)
2289 1556 const phonePattern = /\[([^\]]+)\]\((tel:[\d+\-\s()]+)\)/g;
2290 1557 processedText = processedText.replace(phonePattern, (match, text, phone) => {
2291 1558 const safePhone = safeEncodeUrl(phone);
@@ -2437,78 +1704,9 @@
2437 1704 });
2438 1705
2439 1706 return text;
2440 1707 }
2441 -
2442 - function formatMarkdownTables(text) {
2443 - var lines = text.split('\n');
2444 - var result = [];
2445 - var i = 0;
2446 -
2447 - while (i < lines.length) {
2448 - // Check for a table: current line has pipes AND next line is a separator row
2449 - if (i + 1 < lines.length &&
2450 - lines[i].indexOf('|') !== -1 &&
2451 - /^\s*\|?[\s\-:]+(\|[\s\-:]+)+\|?\s*$/.test(lines[i + 1])) {
2452 -
2453 - var tableLines = [];
2454 - var headerLine = lines[i];
2455 - var separatorLine = lines[i + 1];
2456 - tableLines.push(headerLine);
2457 - tableLines.push(separatorLine);
2458 -
2459 - // Collect remaining table rows
2460 - var j = i + 2;
2461 - while (j < lines.length && lines[j].indexOf('|') !== -1 && lines[j].trim() !== '') {
2462 - tableLines.push(lines[j]);
2463 - j++;
2464 - }
2465 -
2466 - // Parse alignment from separator row
2467 - var sepCells = separatorLine.split('|').filter(function(c) { return c.trim() !== ''; });
2468 - var alignments = sepCells.map(function(cell) {
2469 - var trimmed = cell.trim();
2470 - if (trimmed.charAt(0) === ':' && trimmed.charAt(trimmed.length - 1) === ':') return 'center';
2471 - if (trimmed.charAt(trimmed.length - 1) === ':') return 'right';
2472 - return 'left';
2473 - });
2474 -
2475 - // Build HTML table
2476 - var html = '<div class="mxchat-table-wrapper"><table class="mxchat-table">';
2477 -
2478 - // Header row
2479 - var headerCells = tableLines[0].split('|').filter(function(c) { return c.trim() !== ''; });
2480 - html += '<thead><tr>';
2481 - headerCells.forEach(function(cell, idx) {
2482 - var align = alignments[idx] || 'left';
2483 - html += '<th style="text-align:' + align + '">' + cell.trim() + '</th>';
2484 - });
2485 - html += '</tr></thead>';
2486 -
2487 - // Body rows
2488 - html += '<tbody>';
2489 - for (var r = 2; r < tableLines.length; r++) {
2490 - var rowCells = tableLines[r].split('|').filter(function(c) { return c.trim() !== ''; });
2491 - html += '<tr>';
2492 - rowCells.forEach(function(cell, idx) {
2493 - var align = alignments[idx] || 'left';
2494 - html += '<td style="text-align:' + align + '">' + cell.trim() + '</td>';
2495 - });
2496 - html += '</tr>';
2497 - }
2498 - html += '</tbody></table></div>';
2499 -
2500 - result.push(html);
2501 - i = j;
2502 - } else {
2503 - result.push(lines[i]);
2504 - i++;
2505 - }
2506 - }
2507 -
2508 - return result.join('\n');
2509 - }
2510 -
1708 +
2511 1709 function sanitizeUserInput(text) {
2512 1710 const div = document.createElement('div');
2513 1711 div.textContent = text;
2514 1712 return div.innerHTML;
@@ -2579,14 +1777,13 @@
2579 1777 requestAnimationFrame(smoothScroll);
2580 1778 }
2581 1779 }
2582 1780
2583 - function scrollElementToTop(element, botId, topOffset) {
1781 + function scrollElementToTop(element, botId) {
2584 1782 botId = botId || 'default';
2585 - topOffset = (typeof topOffset === 'number') ? topOffset : 2;
2586 1783 var chatBox = getElement(botId, 'chat-box');
2587 1784 var elementTop = element.position().top + chatBox.scrollTop();
2588 - chatBox.animate({ scrollTop: Math.max(0, elementTop - topOffset) }, 500);
1785 + chatBox.animate({ scrollTop: elementTop }, 500);
2589 1786 }
2590 1787
2591 1788 function showChatWidget(botId) {
2592 1789 botId = botId || 'default';
@@ -2730,12 +1927,15 @@
2730 1927 // LIVE AGENT FUNCTIONALITY
2731 1928 // ====================================
2732 1929
2733 1930 function startPolling(botId) {
1931 + console.log('[MxChat] startPolling called for botId:', botId);
2734 1932 botId = botId || 'default';
2735 1933 var instance = MxChatInstances.get(botId);
2736 1934 // Clear any existing interval first
2737 1935 stopPolling(botId);
1936 + // Start new polling interval
1937 + console.log('[MxChat] Starting polling interval (5s) for botId:', botId);
2738 1938 instance.pollingInterval = setInterval(function() {
2739 1939 checkForAgentMessages(botId);
2740 1940 }, 5000);
2741 1941 }
@@ -2740,17 +1940,20 @@
2740 1940 }, 5000);
2741 1941 }
2742 1942
2743 1943 function stopPolling(botId) {
1944 + console.log('[MxChat] stopPolling called for botId:', botId);
2744 1945 botId = botId || 'default';
2745 1946 var instance = MxChatInstances.get(botId);
2746 1947 if (instance.pollingInterval) {
2747 1948 clearInterval(instance.pollingInterval);
2748 1949 instance.pollingInterval = null;
1950 + console.log('[MxChat] Polling stopped for botId:', botId);
2749 1951 }
2750 1952 }
2751 1953
2752 1954 function checkForAgentMessages(botId) {
1955 + console.log('[MxChat] checkForAgentMessages called for botId:', botId);
2753 1956 botId = botId || 'default';
2754 1957 var instance = MxChatInstances.get(botId);
2755 1958 const sessionId = getChatSession(botId);
2756 1959 $.ajax({
@@ -2776,12 +1979,8 @@
2776 1979 instance.processedMessageIds.add(message.id);
2777 1980 }
2778 1981 });
2779 1982
2780 - if (hasNewMessage) {
2781 - enableChatInput(botId);
2782 - }
2783 -
2784 1983 var $floatingChatbot = getElement(botId, 'floating-chatbot');
2785 1984 if (hasNewMessage && $floatingChatbot.hasClass('hidden')) {
2786 1985 showNotification(botId);
2787 1986 }
@@ -2787,13 +1986,8 @@
2787 1986 }
2788 1987
2789 1988 scrollToBottom(botId, true);
2790 1989 }
2791 -
2792 - // Handle chat mode transitions (e.g. agent ended chat via !endchat)
2793 - if (response.success && response.data?.chat_mode) {
2794 - updateChatModeIndicator(response.data.chat_mode, botId);
2795 - }
2796 1990 },
2797 1991 error: function (xhr, status, error) {
2798 1992 // Polling error - silently continue
2799 1993 }
@@ -2803,29 +1997,20 @@
2803 1997 // ====================================
2804 1998 // CHAT HISTORY & PERSISTENCE
2805 1999 // ====================================
2806 2000
2807 -function loadChatHistory(botId, onComplete) {
2001 +function loadChatHistory(botId) {
2808 2002 botId = botId || 'default';
2809 2003 var instance = MxChatInstances.get(botId);
2810 2004
2811 2005 // Prevent duplicate loading
2812 2006 if (instance.chatHistoryLoaded) {
2813 - if (onComplete) onComplete();
2814 2007 return;
2815 2008 }
2816 2009
2817 - // Use getChatSession which returns null if no session exists (does NOT create one)
2818 2010 var sessionId = getChatSession(botId);
2819 2011 var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
2820 2012
2821 - // No session yet — nothing to load. History will load after first message via ensureSession.
2822 - if (!sessionId) {
2823 - instance.chatHistoryLoaded = true;
2824 - if (onComplete) onComplete();
2825 - return;
2826 - }
2827 -
2828 2013 if (chatPersistenceEnabled && sessionId) {
2829 2014 $.ajax({
2830 2015 url: mxchatChat.ajax_url,
2831 2016 type: 'POST',
@@ -2836,12 +2021,11 @@
2836 2021 },
2837 2022 success: function(response) {
2838 2023 // Handle session reset (IP changed while user was away)
2839 2024 if (response.success === false && response.data && response.data.action === 'reset_session') {
2840 - // Silent reset — new session but don't clear UI
2841 - MxChatInstances.silentResetSession(botId);
2025 + // Silently reset session - user will start fresh
2026 + resetChatSession(botId);
2842 2027 instance.chatHistoryLoaded = true; // Prevent retry loop
2843 - if (onComplete) onComplete();
2844 2028 return;
2845 2029 }
2846 2030
2847 2031 // Check if the response indicates success
@@ -2897,19 +2081,9 @@
2897 2081 var content = message.content;
2898 2082 content = content.replace(/\\'/g, "'").replace(/\\"/g, '"');
2899 2083 content = decodeHTMLEntities(content);
2900 2084
2901 - // Skip linkify for messages containing structured HTML
2902 - // (forms, product cards, galleries, etc.) to avoid
2903 - // markdown formatting corrupting HTML attributes
2904 - // (e.g. underscores in name="field_name" becoming <em> tags)
2905 - if (content.includes("mxchat-product-card") ||
2906 - content.includes("mxchat-image-gallery") ||
2907 - content.includes("mxchat-featured-products") ||
2908 - content.includes("<form") ||
2909 - content.includes("<input") ||
2910 - content.includes("<select") ||
2911 - content.includes("<textarea")) {
2085 + if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) {
2912 2086 messageElement.html(content);
2913 2087 } else {
2914 2088 var formattedContent = linkify(content);
2915 2089 messageElement.html(formattedContent);
@@ -2949,17 +2123,13 @@
2949 2123 instance.chatHistoryLoaded = true;
2950 2124 }
2951 2125 }
2952 2126 }
2953 - if (onComplete) onComplete();
2954 2127 },
2955 2128 error: function(xhr, status, error) {
2956 2129 // Error loading chat history - silently continue
2957 - if (onComplete) onComplete();
2958 2130 }
2959 2131 });
2960 - } else {
2961 - if (onComplete) onComplete();
2962 2132 }
2963 2133 }
2964 2134
2965 2135
@@ -3023,10 +2193,10 @@
3023 2193 .then(data => {
3024 2194 if (data.success) {
3025 2195 container.style.display = 'none';
3026 2196 nameElement.textContent = '';
3027 - instance.activePdfFile = null;
3028 - appendMessage('bot', 'PDF removed.', '', [], false, botId);
2197 + activePdfFile = null;
2198 + appendMessage('bot', 'PDF removed.');
3029 2199 }
3030 2200 })
3031 2201 .catch(error => {
3032 2202 // Error removing PDF - silently continue
@@ -3032,16 +2202,14 @@
3032 2202 // Error removing PDF - silently continue
3033 2203 });
3034 2204 }
3035 2205
3036 - function removeActiveWord(botId) {
3037 - botId = botId || 'default';
3038 - var instance = MxChatInstances.get(botId);
3039 - const container = getElementDOM(botId, 'active-word-container');
3040 - const nameElement = getElementDOM(botId, 'active-word-name');
3041 -
3042 - if (!container || !nameElement || !instance.activeWordFile) return;
3043 -
2206 + function removeActiveWord() {
2207 + const container = document.getElementById('active-word-container');
2208 + const nameElement = document.getElementById('active-word-name');
2209 +
2210 + if (!container || !nameElement || !activeWordFile) return;
2211 +
3044 2212 fetch(mxchatChat.ajax_url, {
3045 2213 method: 'POST',
3046 2214 headers: {
3047 2215 'Content-Type': 'application/x-www-form-urlencoded',
@@ -3047,9 +2215,9 @@
3047 2215 'Content-Type': 'application/x-www-form-urlencoded',
3048 2216 },
3049 2217 body: new URLSearchParams({
3050 2218 'action': 'mxchat_remove_word',
3051 - 'session_id': getChatSession(botId),
2219 + 'session_id': sessionId,
3052 2220 'nonce': mxchatChat.nonce
3053 2221 })
3054 2222 })
3055 2223 .then(response => response.json())
@@ -3056,10 +2224,10 @@
3056 2224 .then(data => {
3057 2225 if (data.success) {
3058 2226 container.style.display = 'none';
3059 2227 nameElement.textContent = '';
3060 - instance.activeWordFile = null;
3061 - appendMessage('bot', 'Word document removed.', '', [], false, botId);
2228 + activeWordFile = null;
2229 + appendMessage('bot', 'Word document removed.');
3062 2230 }
3063 2231 })
3064 2232 .catch(error => {
3065 2233 // Error removing Word document - silently continue
@@ -3137,35 +2305,45 @@
3137 2305 // ====================================
3138 2306
3139 2307 function checkPreChatDismissal(botId) {
3140 2308 botId = botId || 'default';
3141 - try {
3142 - var dismissedAt = localStorage.getItem('mxchat_pre_chat_dismissed_' + botId);
3143 - if (dismissedAt) {
3144 - // Re-show after 24 hours
3145 - var elapsed = Date.now() - parseInt(dismissedAt, 10);
3146 - if (elapsed < 86400000) {
2309 + $.ajax({
2310 + url: mxchatChat.ajax_url,
2311 + type: 'POST',
2312 + data: {
2313 + action: 'mxchat_check_pre_chat_message_status',
2314 + _ajax_nonce: mxchatChat.nonce
2315 + },
2316 + success: function(response) {
2317 + if (response.success && !response.data.dismissed) {
2318 + getElement(botId, 'pre-chat-message').fadeIn(250);
2319 + } else {
3147 2320 getElement(botId, 'pre-chat-message').hide();
3148 - return;
3149 2321 }
3150 - // Expired — clear and show again
3151 - localStorage.removeItem('mxchat_pre_chat_dismissed_' + botId);
2322 + },
2323 + error: function() {
2324 + // Error checking pre-chat dismissal - silently continue
3152 2325 }
3153 - getElement(botId, 'pre-chat-message').fadeIn(250);
3154 - } catch (e) {
3155 - // localStorage unavailable — show the message
3156 - getElement(botId, 'pre-chat-message').fadeIn(250);
3157 - }
2326 + });
3158 2327 }
3159 2328
3160 2329 function handlePreChatDismissal(botId) {
3161 2330 botId = botId || 'default';
3162 2331 getElement(botId, 'pre-chat-message').fadeOut(200);
3163 - try {
3164 - localStorage.setItem('mxchat_pre_chat_dismissed_' + botId, String(Date.now()));
3165 - } catch (e) {
3166 - // localStorage unavailable — dismissal won't persist
3167 - }
2332 + $.ajax({
2333 + url: mxchatChat.ajax_url,
2334 + type: 'POST',
2335 + data: {
2336 + action: 'mxchat_dismiss_pre_chat_message',
2337 + _ajax_nonce: mxchatChat.nonce
2338 + },
2339 + success: function() {
2340 + $('#pre-chat-message').hide();
2341 + },
2342 + error: function() {
2343 + // Error dismissing pre-chat message - silently continue
2344 + }
2345 + });
3168 2346 }
3169 2347
3170 2348
3171 2349 // ====================================
@@ -3220,14 +2398,9 @@
3220 2398 collapseQuickQuestions(botId);
3221 2399 });
3222 2400
3223 2401 // Chatbot visibility toggle handlers - use class selector for multi-instance support
3224 - // Handles click + Enter/Space keypresses for keyboard accessibility (WCAG 2.1 SC 2.1.1).
3225 - $(document).on('click keydown', '.floating-chatbot-button', function(e) {
3226 - if (e.type === 'keydown') {
3227 - if (e.key !== 'Enter' && e.key !== ' ' && e.key !== 'Spacebar') return;
3228 - e.preventDefault();
3229 - }
2402 + $(document).on('click', '.floating-chatbot-button', function() {
3230 2403 var botId = getBotIdFromElement(this);
3231 2404 var $chatbot = getElement(botId, 'floating-chatbot');
3232 2405 var $badge = getElement(botId, 'chat-notification-badge');
3233 2406 var $preChat = getElement(botId, 'pre-chat-message');
@@ -3232,88 +2405,35 @@
3232 2405 var $badge = getElement(botId, 'chat-notification-badge');
3233 2406 var $preChat = getElement(botId, 'pre-chat-message');
3234 2407
3235 2408 if ($chatbot.hasClass('hidden')) {
3236 - $chatbot.removeClass('hidden').addClass('visible')
3237 - .attr('aria-modal', 'true').attr('role', 'dialog');
3238 - $(this).addClass('hidden').attr('aria-expanded', 'true');
2409 + $chatbot.removeClass('hidden').addClass('visible');
2410 + $(this).addClass('hidden');
3239 2411 $badge.hide(); // Hide notification when opening chat
3240 2412 disableScroll();
3241 2413 $preChat.fadeOut(250);
3242 -
3243 - // First open per page load: re-fetch behavior settings in case
3244 - // this page's inline values came from a stale full-page cache
3245 - // (plan-32db95). Idempotent — later opens are a no-op.
3246 - mxchatRefreshDynamicSettings();
3247 -
3248 - // Load chat history for returning visitors (persistence)
3249 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
3250 - if (chatPersistenceEnabled) {
3251 - MxChatInstances.ensureSession(botId);
3252 - }
3253 -
3254 - // Deferred email check — only on first widget open
3255 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3256 - var instance = MxChatInstances.get(botId);
3257 - if (emailBlocker && !instance.emailCheckDone) {
3258 - instance.emailCheckDone = true;
3259 - resolveEmailState(botId);
3260 - } else if (!emailBlocker) {
3261 - // No email collection — still route through showChatContainerForBot
3262 - // so the loader is shown while chat history loads
3263 - showChatContainerForBot(botId);
3264 - }
3265 -
3266 - // Move keyboard focus into the message input after the open transition.
3267 - setTimeout(function() {
3268 - var chatInput = getElementDOM(botId, 'chat-input');
3269 - if (chatInput && !chatInput.disabled) {
3270 - try { chatInput.focus({ preventScroll: true }); } catch (err) { chatInput.focus(); }
3271 - }
3272 - }, 300);
3273 2414 } else {
3274 - $chatbot.removeClass('visible').addClass('hidden').removeAttr('aria-modal');
3275 - $(this).removeClass('hidden').attr('aria-expanded', 'false');
2415 + $chatbot.removeClass('visible').addClass('hidden');
2416 + $(this).removeClass('hidden');
3276 2417 enableScroll();
3277 2418 checkPreChatDismissal(botId);
3278 2419 }
3279 2420 });
3280 2421
3281 - // Allow clicking anywhere on the title bar to close the chatbot.
3282 - // Returns keyboard focus to the launcher so keyboard users don't get
3283 - // stranded at <body> (WCAG SC 2.4.3 Focus Order). :focus-visible is
3284 - // heuristic-based so mouse-triggered close won't show a focus ring.
2422 + // Allow clicking anywhere on the title bar to close the chatbot
3285 2423 $(document).on('click', '.chatbot-top-bar', function() {
3286 2424 var botId = getBotIdFromElement(this);
3287 - getElement(botId, 'floating-chatbot').addClass('hidden').removeClass('visible').removeAttr('aria-modal');
3288 - var $launcher = getElement(botId, 'floating-chatbot-button');
3289 - $launcher.removeClass('hidden').attr('aria-expanded', 'false');
2425 + getElement(botId, 'floating-chatbot').addClass('hidden').removeClass('visible');
2426 + getElement(botId, 'floating-chatbot-button').removeClass('hidden');
3290 2427 enableScroll();
3291 - try { $launcher.trigger('focus'); } catch (err) { /* no-op */ }
3292 2428 });
3293 2429
3294 - // Global Escape-key handler — closes any visible chat widget and
3295 - // returns focus to its launcher. Standard modal-dismissal pattern;
3296 - // pairs with aria-modal="true" set on the widget when it opens.
3297 - $(document).on('keydown', function(e) {
3298 - if (e.key !== 'Escape' && e.key !== 'Esc') return;
3299 - var $visible = $('.floating-chatbot.visible');
3300 - if (!$visible.length) return;
3301 - e.preventDefault();
3302 - $visible.each(function() {
3303 - var botId = getBotIdFromElement(this);
3304 - $(this).addClass('hidden').removeClass('visible').removeAttr('aria-modal');
3305 - var $launcher = getElement(botId, 'floating-chatbot-button');
3306 - $launcher.removeClass('hidden').attr('aria-expanded', 'false');
3307 - try { $launcher.trigger('focus'); } catch (err) { /* no-op */ }
3308 - });
3309 - enableScroll();
3310 - });
3311 -
3312 2430 $(document).on('click', '.close-pre-chat-message', function(e) {
3313 2431 e.stopPropagation(); // Prevent triggering the parent .pre-chat-message click
3314 2432 var botId = getBotIdFromElement(this);
3315 - handlePreChatDismissal(botId);
2433 + getElement(botId, 'pre-chat-message').fadeOut(200, function() {
2434 + $(this).remove();
2435 + });
3316 2436 });
3317 2437
3318 2438
3319 2439 // PDF upload button handlers - use class selector
@@ -3329,20 +2449,17 @@
3329 2449 var wordInput = getElementDOM(botId, 'word-upload');
3330 2450 if (wordInput) wordInput.click();
3331 2451 });
3332 2452
3333 - // PDF file input change handler - delegated, bot-aware (was bound to stale un-suffixed id 'pdf-upload')
3334 - $(document).on('change', '.pdf-upload', async function(e) {
3335 - var botId = getBotIdFromElement(this);
3336 - var instance = MxChatInstances.get(botId);
3337 - const file = this.files[0];
3338 - const sessionId = MxChatInstances.ensureSession(botId);
3339 -
2453 + // PDF file input change handler
2454 + addSafeEventListener('pdf-upload', 'change', async function(e) {
2455 + const file = e.target.files[0];
2456 +
3340 2457 if (!file || file.type !== 'application/pdf') {
3341 2458 alert('Please select a valid PDF file.');
3342 2459 return;
3343 2460 }
3344 -
2461 +
3345 2462 if (!sessionId) {
3346 2463 alert('Error: No session ID found');
3347 2464 return;
3348 2465 }
@@ -3350,49 +2467,47 @@
3350 2467 if (!mxchatChat || !mxchatChat.ajax_url || !mxchatChat.nonce) {
3351 2468 alert('Error: Ajax configuration missing');
3352 2469 return;
3353 2470 }
3354 -
2471 +
3355 2472 // Disable buttons and show loading state
3356 - const uploadBtn = getElementDOM(botId, 'pdf-upload-btn');
3357 - const sendBtn = getElementDOM(botId, 'send-button');
3358 - if (!uploadBtn) return;
2473 + const uploadBtn = document.getElementById('pdf-upload-btn');
2474 + const sendBtn = document.getElementById('send-button');
3359 2475 const originalBtnContent = uploadBtn.innerHTML;
3360 -
2476 +
3361 2477 try {
3362 - // Wait for page-cache nonce refresh before reading mxchatChat.nonce. See plan-c5457f.
3363 - await new Promise(function(resolve) { refreshNonceIfNeeded(resolve); });
3364 2478 const formData = new FormData();
3365 2479 formData.append('action', 'mxchat_upload_pdf');
3366 2480 formData.append('pdf_file', file);
3367 2481 formData.append('session_id', sessionId);
3368 2482 formData.append('nonce', mxchatChat.nonce);
3369 -
2483 +
3370 2484 uploadBtn.disabled = true;
3371 - if (sendBtn) sendBtn.disabled = true;
2485 + sendBtn.disabled = true;
3372 2486 uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
3373 2487 <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
3374 2488 </svg>`;
3375 -
2489 +
3376 2490 const response = await fetch(mxchatChat.ajax_url, {
3377 2491 method: 'POST',
3378 2492 body: formData
3379 2493 });
3380 -
2494 +
3381 2495 const data = await response.json();
3382 -
2496 +
3383 2497 if (data.success) {
3384 2498 // Hide popular questions if they exist
3385 - if (hasQuickQuestions(botId)) {
3386 - collapseQuickQuestions(botId);
2499 + const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
2500 + if (hasQuickQuestions()) {
2501 + collapseQuickQuestions();
3387 2502 }
3388 -
2503 +
3389 2504 // Show the active PDF name
3390 - showActivePdf(data.data.filename, botId);
3391 -
3392 - appendMessage('bot', data.data.message, '', [], false, botId);
3393 - scrollToBottom(botId);
3394 - instance.activePdfFile = data.data.filename;
2505 + showActivePdf(data.data.filename);
2506 +
2507 + appendMessage('bot', data.data.message);
2508 + scrollToBottom();
2509 + activePdfFile = data.data.filename;
3395 2510 } else {
3396 2511 alert('Failed to upload PDF. Please try again.');
3397 2512 }
3398 2513 } catch (error) {
@@ -3398,76 +2513,66 @@
3398 2513 } catch (error) {
3399 2514 alert('Error uploading file. Please try again.');
3400 2515 } finally {
3401 2516 uploadBtn.disabled = false;
3402 - if (sendBtn) sendBtn.disabled = false;
2517 + sendBtn.disabled = false;
3403 2518 uploadBtn.innerHTML = originalBtnContent;
3404 2519 this.value = ''; // Reset file input
3405 2520 }
3406 2521 });
3407 2522
3408 - // Word file input change handler - delegated, bot-aware (was bound to stale un-suffixed id 'word-upload')
3409 - $(document).on('change', '.word-upload', async function(e) {
3410 - var botId = getBotIdFromElement(this);
3411 - var instance = MxChatInstances.get(botId);
3412 - const file = this.files[0];
3413 - const sessionId = MxChatInstances.ensureSession(botId);
3414 -
2523 + // Word file input change handler
2524 + addSafeEventListener('word-upload', 'change', async function(e) {
2525 + const file = e.target.files[0];
2526 +
3415 2527 if (!file || file.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
3416 2528 alert('Please select a valid Word document (.docx).');
3417 2529 return;
3418 2530 }
3419 -
2531 +
3420 2532 if (!sessionId) {
3421 2533 alert('Error: No session ID found');
3422 2534 return;
3423 2535 }
3424 2536
3425 - if (!mxchatChat || !mxchatChat.ajax_url || !mxchatChat.nonce) {
3426 - alert('Error: Ajax configuration missing');
3427 - return;
3428 - }
3429 -
3430 2537 // Disable buttons and show loading state
3431 - const uploadBtn = getElementDOM(botId, 'word-upload-btn');
3432 - const sendBtn = getElementDOM(botId, 'send-button');
3433 - if (!uploadBtn) return;
2538 + const uploadBtn = document.getElementById('word-upload-btn');
2539 + const sendBtn = document.getElementById('send-button');
3434 2540 const originalBtnContent = uploadBtn.innerHTML;
3435 -
2541 +
3436 2542 try {
3437 - // Wait for page-cache nonce refresh before reading mxchatChat.nonce. See plan-c5457f.
3438 - await new Promise(function(resolve) { refreshNonceIfNeeded(resolve); });
3439 2543 const formData = new FormData();
3440 2544 formData.append('action', 'mxchat_upload_word');
3441 2545 formData.append('word_file', file);
3442 2546 formData.append('session_id', sessionId);
3443 2547 formData.append('nonce', mxchatChat.nonce);
3444 -
2548 +
3445 2549 uploadBtn.disabled = true;
3446 - if (sendBtn) sendBtn.disabled = true;
2550 + sendBtn.disabled = true;
3447 2551 uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
3448 2552 <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
3449 2553 </svg>`;
3450 -
2554 +
3451 2555 const response = await fetch(mxchatChat.ajax_url, {
3452 2556 method: 'POST',
3453 2557 body: formData
3454 2558 });
3455 -
2559 +
3456 2560 const data = await response.json();
3457 -
2561 +
3458 2562 if (data.success) {
3459 2563 // Hide popular questions if they exist
3460 - if (hasQuickQuestions(botId)) {
3461 - collapseQuickQuestions(botId);
2564 + const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
2565 + if (hasQuickQuestions()) {
2566 + collapseQuickQuestions();
3462 2567 }
3463 -
2568 +
3464 2569 // Show the active Word document name
3465 - showActiveWord(data.data.filename, botId);
3466 -
3467 - appendMessage('bot', data.data.message, '', [], false, botId);
3468 - scrollToBottom(botId);
3469 - instance.activeWordFile = data.data.filename;
2570 + showActiveWord(data.data.filename);
2571 +
2572 + appendMessage('bot', data.data.message);
2573 + scrollToBottom();
2574 + activeWordFile = data.data.filename;
3470 2575 } else {
3471 2576 alert('Failed to upload Word document. Please try again.');
3472 2577 }
3473 2578 } catch (error) {
@@ -3473,25 +2578,25 @@
3473 2578 } catch (error) {
3474 2579 alert('Error uploading file. Please try again.');
3475 2580 } finally {
3476 2581 uploadBtn.disabled = false;
3477 - if (sendBtn) sendBtn.disabled = false;
2582 + sendBtn.disabled = false;
3478 2583 uploadBtn.innerHTML = originalBtnContent;
3479 2584 this.value = ''; // Reset file input
3480 2585 }
3481 2586 });
3482 2587
3483 - // Remove button click handlers - delegated, bot-aware (were bound to stale un-suffixed ids)
3484 - $(document).on('click', '.remove-pdf-btn', function(e) {
2588 + // Remove button click handlers
2589 + document.getElementById('remove-pdf-btn')?.addEventListener('click', function(e) {
3485 2590 e.preventDefault();
3486 2591 e.stopPropagation();
3487 - removeActivePdf(getBotIdFromElement(this));
2592 + removeActivePdf();
3488 2593 });
3489 -
3490 - $(document).on('click', '.remove-word-btn', function(e) {
2594 +
2595 + document.getElementById('remove-word-btn')?.addEventListener('click', function(e) {
3491 2596 e.preventDefault();
3492 2597 e.stopPropagation();
3493 - removeActiveWord(getBotIdFromElement(this));
2598 + removeActiveWord();
3494 2599 });
3495 2600
3496 2601 // Window resize handlers
3497 2602 $(window).on('resize orientationchange', function() {
@@ -3529,59 +2634,8 @@
3529 2634 });
3530 2635
3531 2636
3532 2637 // ====================================
3533 -// INIT LOADER & CHAT CONTAINER HELPERS
3534 -// ====================================
3535 -// These must be outside the email collection block so they're always available
3536 -// (used by persistence loading even when email collection is off)
3537 -
3538 -function showInitLoader(botId) {
3539 - var loader = getElementDOM(botId, 'mxchat-init-loader');
3540 - if (loader) loader.style.display = 'flex';
3541 -}
3542 -
3543 -function hideInitLoader(botId) {
3544 - var loader = getElementDOM(botId, 'mxchat-init-loader');
3545 - if (loader) loader.style.display = 'none';
3546 -}
3547 -
3548 -function showEmailFormForBot(botId) {
3549 - hideInitLoader(botId);
3550 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3551 - var chatContainer = getElementDOM(botId, 'chat-container');
3552 - if (emailBlocker) emailBlocker.style.display = 'flex';
3553 - if (chatContainer) chatContainer.style.display = 'none';
3554 -}
3555 -
3556 -function showChatContainerForBot(botId) {
3557 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3558 - var chatContainer = getElementDOM(botId, 'chat-container');
3559 - if (emailBlocker) emailBlocker.style.display = 'none';
3560 -
3561 - var instance = MxChatInstances.get(botId);
3562 - var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
3563 -
3564 - // If persistence is on and history hasn't loaded yet, show loader
3565 - // while history loads to prevent flash of empty chat
3566 - if (chatPersistenceEnabled && !instance.chatHistoryLoaded) {
3567 - if (chatContainer) chatContainer.style.display = 'none';
3568 - showInitLoader(botId);
3569 - loadChatHistory(botId, function() {
3570 - hideInitLoader(botId);
3571 - if (chatContainer) chatContainer.style.display = 'flex';
3572 - scrollToBottom(botId, true);
3573 - });
3574 - } else {
3575 - hideInitLoader(botId);
3576 - if (chatContainer) chatContainer.style.display = 'flex';
3577 - if (typeof loadChatHistory === 'function') {
3578 - loadChatHistory(botId);
3579 - }
3580 - }
3581 -}
3582 -
3583 -// ====================================
3584 2638 // EMAIL COLLECTION SETUP - MULTI-INSTANCE VERSION
3585 2639 // ====================================
3586 2640 // Only run email collection setup if it's enabled
3587 2641 if (mxchatChat && mxchatChat.email_collection_enabled === 'on') {
@@ -3617,8 +2671,28 @@
3617 2671 `;
3618 2672 document.head.appendChild(style);
3619 2673 }
3620 2674
2675 + // Helper functions for email collection (multi-instance aware)
2676 + function showEmailFormForBot(botId) {
2677 + var emailBlocker = getElementDOM(botId, 'email-blocker');
2678 + var chatContainer = getElementDOM(botId, 'chat-container');
2679 + if (emailBlocker) emailBlocker.style.display = 'flex';
2680 + if (chatContainer) chatContainer.style.display = 'none';
2681 + }
2682 +
2683 + function showChatContainerForBot(botId) {
2684 + var emailBlocker = getElementDOM(botId, 'email-blocker');
2685 + var chatContainer = getElementDOM(botId, 'chat-container');
2686 + if (emailBlocker) emailBlocker.style.display = 'none';
2687 + if (chatContainer) chatContainer.style.display = 'flex';
2688 +
2689 + // Load chat history for this bot
2690 + if (typeof loadChatHistory === 'function') {
2691 + loadChatHistory(botId);
2692 + }
2693 + }
2694 +
3621 2695 function isValidEmailAddress(email) {
3622 2696 const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
3623 2697 return emailRegex.test(email.trim()) && email.length <= 254;
3624 2698 }
@@ -3626,41 +2700,8 @@
3626 2700 function isValidNameInput(name) {
3627 2701 return name && name.trim().length >= 2 && name.trim().length <= 100;
3628 2702 }
3629 2703
3630 - /**
3631 - * Replace {visitor_name} placeholder in intro message with actual visitor name
3632 - * @param {string} botId - The bot instance ID
3633 - * @param {string} visitorName - The visitor's name to insert
3634 - */
3635 - function replaceVisitorNamePlaceholder(botId, visitorName) {
3636 - var chatBox = getElementDOM(botId, 'chat-box');
3637 - if (!chatBox) return;
3638 -
3639 - // Find the first bot message (intro message)
3640 - var introMessage = chatBox.querySelector('.bot-message');
3641 - if (!introMessage) return;
3642 -
3643 - var messageContent = introMessage.querySelector('div[dir="auto"]');
3644 - if (!messageContent) return;
3645 -
3646 - var html = messageContent.innerHTML;
3647 -
3648 - // Replace {visitor_name} placeholder (case-insensitive)
3649 - if (visitorName && visitorName.trim()) {
3650 - // Escape HTML to prevent XSS
3651 - var safeName = $('<div>').text(visitorName.trim()).html();
3652 - html = html.replace(/\{visitor_name\}/gi, safeName);
3653 - } else {
3654 - // Remove placeholder and clean up spacing if no name provided
3655 - html = html.replace(/\{visitor_name\}/gi, '');
3656 - // Clean up any double spaces that might result
3657 - html = html.replace(/\s{2,}/g, ' ').trim();
3658 - }
3659 -
3660 - messageContent.innerHTML = html;
3661 - }
3662 -
3663 2704 function setEmailSubmissionState(botId, loading) {
3664 2705 var submitButton = getElementDOM(botId, 'email-submit-button');
3665 2706 var emailInput = getElementDOM(botId, 'user-email');
3666 2707 var nameInput = getElementDOM(botId, 'user-name');
@@ -3740,31 +2781,11 @@
3740 2781 existingErrors.forEach(error => error.remove());
3741 2782 }
3742 2783 }
3743 2784
3744 - // Resolve email state using server-side data when available, AJAX fallback otherwise
3745 - function resolveEmailState(botId) {
3746 - if (mxchatChat.skip_email_check && mxchatChat.initial_email_state) {
3747 - if (mxchatChat.initial_email_state.show_email_form) {
3748 - showEmailFormForBot(botId);
3749 - } else {
3750 - showChatContainerForBot(botId);
3751 - }
3752 - } else {
3753 - checkSessionAndEmailForBot(botId);
3754 - }
3755 - }
3756 -
3757 2785 function checkSessionAndEmailForBot(botId) {
3758 - const sessionId = MxChatInstances.ensureSession(botId);
2786 + const sessionId = getChatSession(botId);
3759 2787
3760 - // Hide both panels while we check — show loader instead
3761 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3762 - var chatContainer = getElementDOM(botId, 'chat-container');
3763 - if (emailBlocker) emailBlocker.style.display = 'none';
3764 - if (chatContainer) chatContainer.style.display = 'none';
3765 - showInitLoader(botId);
3766 -
3767 2788 fetch(mxchatChat.ajax_url, {
3768 2789 method: 'POST',
3769 2790 headers: {
3770 2791 'Content-Type': 'application/x-www-form-urlencoded',
@@ -3812,9 +2833,9 @@
3812 2833 var emailInput = getElementDOM(botId, 'user-email');
3813 2834 var nameInput = getElementDOM(botId, 'user-name');
3814 2835 var userEmail = emailInput ? emailInput.value.trim() : '';
3815 2836 var userName = nameInput ? nameInput.value.trim() : '';
3816 - var sessionId = MxChatInstances.ensureSession(botId);
2837 + var sessionId = getChatSession(botId);
3817 2838
3818 2839 // Validate email
3819 2840 if (!userEmail) {
3820 2841 showEmailError(botId, 'Please enter your email address.');
@@ -3865,16 +2886,8 @@
3865 2886
3866 2887 if (data.success) {
3867 2888 showChatContainerForBot(botId);
3868 2889
3869 - // Replace {visitor_name} placeholder in intro message with actual name
3870 - if (userName) {
3871 - replaceVisitorNamePlaceholder(botId, userName);
3872 - } else {
3873 - // Remove placeholder if no name provided
3874 - replaceVisitorNamePlaceholder(botId, '');
3875 - }
3876 -
3877 2890 if (data.message && typeof appendMessage === 'function') {
3878 2891 setTimeout(() => {
3879 2892 appendMessage('bot', data.message, '', [], false, botId);
3880 2893 if (typeof scrollToBottom === 'function') {
@@ -3937,27 +2950,25 @@
3937 2950 }
3938 2951 });
3939 2952
3940 2953 // Initialize email check for all bot instances
3941 - // For floating bots: defer until widget is opened (zero passive AJAX)
3942 - // For embedded bots: check immediately since the form is visible
3943 2954 $('.mxchat-chatbot-wrapper').each(function() {
3944 2955 var botId = $(this).data('bot-id') || 'default';
3945 2956 var emailBlocker = getElementDOM(botId, 'email-blocker');
3946 2957
2958 + // Only check if email blocker exists for this bot
3947 2959 if (emailBlocker) {
3948 - if (isEmbeddedBot(botId)) {
3949 - // Embedded bots are always visible — check now
3950 - resolveEmailState(botId);
2960 + if (mxchatChat.skip_email_check && mxchatChat.initial_email_state) {
2961 + if (mxchatChat.initial_email_state.show_email_form) {
2962 + showEmailFormForBot(botId);
2963 + } else {
2964 + showChatContainerForBot(botId);
2965 + }
2966 + } else {
2967 + setTimeout(function() {
2968 + checkSessionAndEmailForBot(botId);
2969 + }, 100);
3951 2970 }
3952 - // Floating bots: handled in the widget open handler
3953 - } else if (isEmbeddedBot(botId)) {
3954 - // Embedded bot, no email collection — load history with loader
3955 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
3956 - if (chatPersistenceEnabled) {
3957 - MxChatInstances.ensureSession(botId);
3958 - showChatContainerForBot(botId);
3959 - }
3960 2971 }
3961 2972 });
3962 2973 }
3963 2974
@@ -3967,32 +2978,39 @@
3967 2978 var $chatbot = getElement(botId, 'floating-chatbot');
3968 2979 if ($chatbot.hasClass('hidden')) {
3969 2980 $chatbot.removeClass('hidden').addClass('visible');
3970 2981 getElement(botId, 'floating-chatbot-button').addClass('hidden');
3971 - handlePreChatDismissal(botId);
2982 + $(this).fadeOut(250); // Hide pre-chat message
3972 2983 disableScroll(); // Disable scroll when chatbot opens
2984 + }
2985 + });
3973 2986
3974 - // Load chat history for returning visitors (persistence)
3975 - var chatPersistenceEnabled = typeof mxchatChat !== 'undefined' && mxchatChat.chat_persistence_toggle === 'on';
3976 - if (chatPersistenceEnabled) {
3977 - MxChatInstances.ensureSession(botId);
3978 - }
2987 + // Dismiss pre-chat message via close button - handled by event delegation above at line ~2376
2988 + // This is a fallback for legacy support
2989 + $(document).on('click', '.close-pre-chat-message', function() {
2990 + var botId = getBotIdFromElement(this);
2991 + var $preChat = getElement(botId, 'pre-chat-message');
2992 + $preChat.fadeOut(200); // Hide the message
3979 2993
3980 - // Deferred email check — only on first widget open
3981 - var emailBlocker = getElementDOM(botId, 'email-blocker');
3982 - var instance = MxChatInstances.get(botId);
3983 - if (emailBlocker && !instance.emailCheckDone) {
3984 - instance.emailCheckDone = true;
3985 - resolveEmailState(botId);
3986 - } else if (!emailBlocker) {
3987 - showChatContainerForBot(botId);
2994 + // Send an AJAX request to set the transient flag for 24 hours
2995 + $.ajax({
2996 + url: mxchatChat.ajax_url,
2997 + type: 'POST',
2998 + data: {
2999 + action: 'mxchat_dismiss_pre_chat_message',
3000 + _ajax_nonce: mxchatChat.nonce
3001 + },
3002 + success: function() {
3003 + // Ensure the message is hidden after dismissal
3004 + $preChat.hide();
3005 + },
3006 + error: function() {
3007 + // Error dismissing pre-chat message - silently continue
3988 3008 }
3989 - }
3009 + });
3990 3010 });
3991 3011
3992 - // Legacy duplicate close handler removed — handled by single event delegation above
3993 3012
3994 -
3995 3013 function hasQuickQuestions(botId) {
3996 3014 botId = botId || 'default';
3997 3015 var questionsContainer = getElementDOM(botId, 'mxchat-popular-questions');
3998 3016 if (!questionsContainer) return false;
@@ -4128,11 +3146,18 @@
4128 3146 });
4129 3147
4130 3148 // Initialize when document is ready
4131 3149 setFullHeight();
3150 + trackOriginatingPage();
4132 3151
4133 - // Note: trackOriginatingPage() and loadChatHistory() are now deferred
4134 - // until the user's first interaction via MxChatInstances.ensureSession()
3152 + // Only load chat history if email collection is disabled
3153 + if (mxchatChat.email_collection_enabled !== 'on') {
3154 + // Load history for all instances
3155 + $('.mxchat-chatbot-wrapper').each(function() {
3156 + var botId = $(this).data('bot-id') || 'default';
3157 + loadChatHistory(botId);
3158 + });
3159 + }
4135 3160
4136 3161 // Initialize chat visibility for all instances
4137 3162 $('.mxchat-chatbot-wrapper').each(function() {
4138 3163 var botId = $(this).data('bot-id') || 'default';
@@ -4185,312 +3210,6 @@
4185 3210 }, 2000);
4186 3211 });
4187 3212 }
4188 3213 }
4189 -});
4190 -
4191 -// ============================================================================
4192 -// SATISFACTION RATING (v3.2.6)
4193 -// ============================================================================
4194 -// Per-session 👍/👎 prompt that appears in the chat-box after 60s of user
4195 -// inactivity following a bot reply. One prompt per session, deduped via
4196 -// localStorage. Runs ONLY when the satisfaction_rating_enabled option is on —
4197 -// the option (default off) is authoritative.
4198 -jQuery(function($) {
4199 - if (typeof mxchatChat === 'undefined') return;
4200 - // wp_localize_script stringifies scalars: a PHP boolean false arrives as
4201 - // '' and true as '1', so this must be an explicit-enable allowlist — the
4202 - // old "disabled when exactly false/'off'" check let '' through and the
4203 - // bubble rendered on sites with the option off/unset (plan-4bba64). PHP
4204 - // now emits 'on'/'off' strings; true/'1'/1 keep cached pre-fix HTML
4205 - // (boolean-true localizations) working.
4206 - // NOTE (plan-32db95): this gate reads the INLINE value at DOM ready and is
4207 - // deliberately NOT re-evaluated after the widget's dynamic-settings refresh
4208 - // merges fresh values over mxchatChat (that merge fires on first widget
4209 - // open, after this module has already decided). Re-evaluating would mean
4210 - // restructuring the whole module to late-bind its listeners — not worth it
4211 - // for a prompt that is at worst stale for one page load on a cached page.
4212 - var sre = mxchatChat.satisfaction_rating_enabled;
4213 - if (sre !== 'on' && sre !== true && sre !== '1' && sre !== 1) return;
4214 -
4215 - // wp_localize_script stringifies ints, so accept both number and numeric string.
4216 - var idleRaw = mxchatChat.satisfaction_rating_idle_seconds;
4217 - var idleSeconds = (typeof idleRaw === 'number') ? idleRaw : parseInt(idleRaw, 10);
4218 - if (!isFinite(idleSeconds)) idleSeconds = 60;
4219 - if (idleSeconds < 5) idleSeconds = 5;
4220 - if (idleSeconds > 600) idleSeconds = 600;
4221 - var IDLE_MS = idleSeconds * 1000;
4222 - var MIN_BOT_REPLIES = 2;
4223 - var ratingState = {};
4224 -
4225 - function getState(botId) {
4226 - if (!ratingState[botId]) {
4227 - ratingState[botId] = { idleTimer: null, botReplies: 0, promptShown: false, dismissed: false };
4228 - }
4229 - return ratingState[botId];
4230 - }
4231 -
4232 - function getSessionId(botId) {
4233 - if (typeof MxChatInstances !== 'undefined' && MxChatInstances.getChatSession) {
4234 - return MxChatInstances.getChatSession(botId);
4235 - }
4236 - return null;
4237 - }
4238 -
4239 - function isAlreadyRated(sessionId) {
4240 - if (!sessionId) return false;
4241 - try { return localStorage.getItem('mxchat_rated:' + sessionId) === '1'; } catch (e) { return false; }
4242 - }
4243 -
4244 - function markRated(sessionId) {
4245 - if (!sessionId) return;
4246 - try { localStorage.setItem('mxchat_rated:' + sessionId, '1'); } catch (e) {}
4247 - }
4248 -
4249 - function esc(s) {
4250 - return String(s == null ? '' : s)
4251 - .replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
4252 - .replace(/"/g, '&quot;').replace(/'/g, '&#039;');
4253 - }
4254 -
4255 - // Mirror shouldSkipInlineColors so rating bubbles defer to AI-theme CSS.
4256 - function ratingSkipInlineColors(botId) {
4257 - if (mxchatChat.skip_inline_colors) return true;
4258 - var botAssignments = mxchatChat.bot_theme_assignments || {};
4259 - return botAssignments.hasOwnProperty(botId);
4260 - }
4261 -
4262 - function botBubbleStyleAttr(botId) {
4263 - if (ratingSkipInlineColors(botId)) return '';
4264 - var bg = mxchatChat.bot_message_bg_color;
4265 - var fg = mxchatChat.bot_message_font_color;
4266 - if (!bg && !fg) return '';
4267 - return ' style="background-color: ' + esc(bg || '') + '; color: ' + esc(fg || '') + ';"';
4268 - }
4269 -
4270 - // Reads the rating bubble's actual computed fg+bg (whatever paints it —
4271 - // the inline color pickers OR the mxchat-theme AI customizer's injected CSS)
4272 - // and paints the filled "Send" pill so it fills with the bot font color and
4273 - // labels in the bubble bg. Mirrors mxchatSyncMenuColors(~:1512) for the read.
4274 - // We paint the submit button DIRECTLY (inline longhand) rather than relying
4275 - // on the CSS rule's var()s: Chromium resolves an INHERITED custom property
4276 - // unreliably inside a descendant's `background`, so a bubble-level var would
4277 - // silently fall back to the literal (white-block bug all over again). Inline
4278 - // longhand always wins. Same transparent-guard as the menu so we never paint
4279 - // a see-through value — in that case the CSS literal fallbacks keep it legible.
4280 - function syncRatingBubbleColors(botId) {
4281 - var $chatBox = getChatBoxByBotId(botId);
4282 - if (!$chatBox || !$chatBox.length) return;
4283 - var bubbleEl = $chatBox.find('.mxchat-rating-bot-bubble').last()[0];
4284 - if (!bubbleEl) return;
4285 - var cs = window.getComputedStyle(bubbleEl);
4286 - var fg = cs.color;
4287 - var bg = cs.backgroundColor;
4288 - var hasFg = fg && fg !== 'rgba(0, 0, 0, 0)' && fg !== 'transparent';
4289 - var hasBg = bg && bg !== 'rgba(0, 0, 0, 0)' && bg !== 'transparent';
4290 - // Expose on the bubble too, for any inheriting styles / future use.
4291 - if (hasFg) bubbleEl.style.setProperty('--mxchat-bot-fg', fg);
4292 - if (hasBg) bubbleEl.style.setProperty('--mxchat-bot-bg', bg);
4293 - // Paint the Send pill directly — the part that actually fixes the bug.
4294 - var submitEl = bubbleEl.querySelector('.mxchat-rating-submit');
4295 - if (submitEl) {
4296 - if (hasFg) submitEl.style.backgroundColor = fg; // fill = bot font color
4297 - if (hasBg) submitEl.style.color = bg; // label = bubble background
4298 - }
4299 - }
4300 -
4301 - function copy(key) {
4302 - var c = mxchatChat.satisfaction_rating_copy || {};
4303 - var d = {
4304 - question: 'Was this helpful?',
4305 - helpful: 'Helpful',
4306 - not_helpful: 'Not helpful',
4307 - dismiss: 'Dismiss',
4308 - thanks: 'Thanks! Anything we should improve? (optional)',
4309 - placeholder: 'Tell us what could be better…',
4310 - send: 'Send',
4311 - skip: 'Skip',
4312 - saved: 'Thanks for the feedback.'
4313 - };
4314 - return c[key] || d[key];
4315 - }
4316 -
4317 - function thumbUpSvg() {
4318 - 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>';
4319 - }
4320 - function thumbDownSvg() {
4321 - 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>';
4322 - }
4323 -
4324 - function buildPromptHtml(botId) {
4325 - var styleAttr = botBubbleStyleAttr(botId);
4326 - return ''
4327 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4328 - + '<div class="mxchat-rating-prompt" data-bot-id="' + esc(botId) + '" role="group" aria-label="' + esc(copy('question')) + '">'
4329 - + '<div class="mxchat-rating-question">' + esc(copy('question')) + '</div>'
4330 - + '<div class="mxchat-rating-actions">'
4331 - + '<span class="mxchat-rating-buttons">'
4332 - + '<button type="button" class="mxchat-rating-btn" data-rating="1" aria-label="' + esc(copy('helpful')) + '">' + thumbUpSvg() + '</button>'
4333 - + '<button type="button" class="mxchat-rating-btn" data-rating="-1" aria-label="' + esc(copy('not_helpful')) + '">' + thumbDownSvg() + '</button>'
4334 - + '</span>'
4335 - + '<button type="button" class="mxchat-rating-dismiss" aria-label="' + esc(copy('dismiss')) + '">×</button>'
4336 - + '</div>'
4337 - + '</div>'
4338 - + '</div>';
4339 - }
4340 -
4341 - function buildFeedbackHtml(botId, rating) {
4342 - var styleAttr = botBubbleStyleAttr(botId);
4343 - return ''
4344 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4345 - + '<div class="mxchat-rating-feedback" data-bot-id="' + esc(botId) + '" data-rating="' + esc(String(rating)) + '">'
4346 - + '<div class="mxchat-rating-feedback-label">' + esc(copy('thanks')) + '</div>'
4347 - + '<textarea class="mxchat-rating-feedback-input" maxlength="500" placeholder="' + esc(copy('placeholder')) + '" rows="2"></textarea>'
4348 - + '<div class="mxchat-rating-feedback-actions">'
4349 - + '<button type="button" class="mxchat-rating-skip">' + esc(copy('skip')) + '</button>'
4350 - + '<button type="button" class="mxchat-rating-submit">' + esc(copy('send')) + '</button>'
4351 - + '</div>'
4352 - + '</div>'
4353 - + '</div>';
4354 - }
4355 -
4356 - function buildSavedHtml(botId) {
4357 - var styleAttr = botBubbleStyleAttr(botId);
4358 - return ''
4359 - + '<div class="bot-message mxchat-rating-bot-bubble"' + styleAttr + '>'
4360 - + '<div class="mxchat-rating-saved">' + esc(copy('saved')) + '</div>'
4361 - + '</div>';
4362 - }
4363 -
4364 - function getChatBoxByBotId(botId) {
4365 - var $byId = $('#chat-box-' + botId);
4366 - if ($byId.length) return $byId.first();
4367 - return $('.chat-box').first();
4368 - }
4369 -
4370 - function scrollChatBoxToBottom($chatBox) {
4371 - if (!$chatBox || !$chatBox.length) return;
4372 - $chatBox.scrollTop($chatBox[0].scrollHeight);
4373 - }
4374 -
4375 - function showPrompt(botId) {
4376 - var s = getState(botId);
4377 - if (s.promptShown || s.dismissed) return;
4378 - var sessionId = getSessionId(botId);
4379 - if (!sessionId) return;
4380 - if (isAlreadyRated(sessionId)) { s.promptShown = true; return; }
4381 - var $chatBox = getChatBoxByBotId(botId);
4382 - if (!$chatBox.length) return;
4383 - if ($chatBox.find('.mxchat-rating-prompt').length) { s.promptShown = true; return; }
4384 - $chatBox.append(buildPromptHtml(botId));
4385 - syncRatingBubbleColors(botId);
4386 - s.promptShown = true;
4387 - scrollChatBoxToBottom($chatBox);
4388 - }
4389 -
4390 - function submitRating(botId, rating, feedback) {
4391 - var sessionId = getSessionId(botId);
4392 - if (!sessionId) return;
4393 - $.post(mxchatChat.ajax_url, {
4394 - action: 'mxchat_save_rating',
4395 - session_id: sessionId,
4396 - bot_id: botId,
4397 - rating: rating,
4398 - feedback: feedback || ''
4399 - });
4400 - markRated(sessionId);
4401 - }
4402 -
4403 - function onBotReply(botId) {
4404 - var s = getState(botId);
4405 - s.botReplies += 1;
4406 - if (s.promptShown || s.dismissed) return;
4407 - var sessionId = getSessionId(botId);
4408 - if (sessionId && isAlreadyRated(sessionId)) { s.promptShown = true; return; }
4409 - if (s.botReplies < MIN_BOT_REPLIES) return;
4410 - if (s.idleTimer) clearTimeout(s.idleTimer);
4411 - s.idleTimer = setTimeout(function() { showPrompt(botId); }, IDLE_MS);
4412 - }
4413 -
4414 - function onUserMessage(botId) {
4415 - var s = getState(botId);
4416 - if (s.idleTimer) { clearTimeout(s.idleTimer); s.idleTimer = null; }
4417 - }
4418 -
4419 - function botIdFromChatBox(el) {
4420 - var id = el && el.id ? el.id : '';
4421 - return id.indexOf('chat-box-') === 0 ? id.substring('chat-box-'.length) : 'default';
4422 - }
4423 -
4424 - function setupObserver(chatBox) {
4425 - var botId = botIdFromChatBox(chatBox);
4426 - try {
4427 - var observer = new MutationObserver(function(mutations) {
4428 - mutations.forEach(function(m) {
4429 - for (var i = 0; i < m.addedNodes.length; i++) {
4430 - var node = m.addedNodes[i];
4431 - if (!node || node.nodeType !== 1) continue;
4432 - var $n = $(node);
4433 - if ($n.hasClass('mxchat-rating-bot-bubble') || $n.hasClass('mxchat-rating-prompt') || $n.hasClass('mxchat-rating-feedback') || $n.hasClass('mxchat-rating-saved')) continue;
4434 - 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)
4435 - else if ($n.hasClass('user-message')) onUserMessage(botId);
4436 - }
4437 - });
4438 - });
4439 - observer.observe(chatBox, { childList: true });
4440 - } catch (e) { /* noop */ }
4441 - }
4442 -
4443 - $('.chat-box').each(function() { setupObserver(this); });
4444 -
4445 - $(document).on('click', '.mxchat-rating-btn', function(e) {
4446 - e.preventDefault();
4447 - var $btn = $(this);
4448 - var $prompt = $btn.closest('.mxchat-rating-prompt');
4449 - var $wrap = $btn.closest('.mxchat-rating-bot-bubble');
4450 - var botId = $prompt.data('bot-id') || 'default';
4451 - var rating = parseInt($btn.attr('data-rating'), 10);
4452 - if (rating !== 1 && rating !== -1) return;
4453 - submitRating(botId, rating, '');
4454 - ($wrap.length ? $wrap : $prompt).replaceWith(buildFeedbackHtml(botId, rating));
4455 - syncRatingBubbleColors(botId);
4456 - scrollChatBoxToBottom(getChatBoxByBotId(botId));
4457 - });
4458 -
4459 - $(document).on('click', '.mxchat-rating-dismiss', function(e) {
4460 - e.preventDefault();
4461 - var $prompt = $(this).closest('.mxchat-rating-prompt');
4462 - var $wrap = $(this).closest('.mxchat-rating-bot-bubble');
4463 - var botId = $prompt.data('bot-id') || 'default';
4464 - var s = getState(botId);
4465 - s.dismissed = true;
4466 - markRated(getSessionId(botId));
4467 - ($wrap.length ? $wrap : $prompt).remove();
4468 - });
4469 -
4470 - function closeFeedback($fb) {
4471 - var botId = $fb.data('bot-id') || 'default';
4472 - var $wrap = $fb.closest('.mxchat-rating-bot-bubble');
4473 - ($wrap.length ? $wrap : $fb).replaceWith(buildSavedHtml(botId));
4474 - syncRatingBubbleColors(botId);
4475 - scrollChatBoxToBottom(getChatBoxByBotId(botId));
4476 - }
4477 -
4478 - $(document).on('click', '.mxchat-rating-skip', function(e) {
4479 - e.preventDefault();
4480 - closeFeedback($(this).closest('.mxchat-rating-feedback'));
4481 - });
4482 -
4483 - $(document).on('click', '.mxchat-rating-submit', function(e) {
4484 - e.preventDefault();
4485 - var $fb = $(this).closest('.mxchat-rating-feedback');
4486 - var botId = $fb.data('bot-id') || 'default';
4487 - var rating = parseInt($fb.attr('data-rating'), 10);
4488 - if (rating !== 1 && rating !== -1) { closeFeedback($fb); return; }
4489 - var text = String($fb.find('.mxchat-rating-feedback-input').val() || '').trim();
4490 - if (text !== '') {
4491 - submitRating(botId, rating, text);
4492 - }
4493 - closeFeedback($fb);
4494 - });
4495 3214 });
4496 3215