PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 3.2.0
MxChat – AI Chatbot & Content Generation for WordPress v3.2.0
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 +6 -56 3.2.43.2.0 View file →
@@ -60,10 +60,10 @@
60 60 return Object.keys(this.instances);
61 61 },
62 62
63 63 // Session management per bot
64 - // Returns existing session ID from cookie or localStorage (with in-memory fallback),
65 - // or null if none exists. Does NOT create a new session — use ensureSession() for that.
64 + // Returns existing session ID from cookie or localStorage, or null if none exists.
65 + // Does NOT create a new session — use ensureSession() for that.
66 66 getChatSession: function(botId) {
67 67 var cookieName = 'mxchat_session_id_' + botId;
68 68 var storageKey = 'mxchat_session_id_' + botId;
69 69 var sessionId = getCookie(cookieName);
@@ -72,21 +72,8 @@
72 72 if (!sessionId) {
73 73 try { sessionId = localStorage.getItem(storageKey); } catch (e) {}
74 74 }
75 75
76 - // Fallback to in-memory instance when cookie AND localStorage are both blocked
77 - // (Safari ITP, strict tracking prevention, cross-origin iframes with partitioned
78 - // storage). Without this, ensureSession() can generate and store an ID that
79 - // getChatSession() then can't read back, causing null session_ids on send.
80 - if (!sessionId && this.instances[botId] && this.instances[botId].sessionId) {
81 - sessionId = this.instances[botId].sessionId;
82 - }
83 -
84 - // Guard against stored sentinel values that indicate earlier broken writes.
85 - if (sessionId === 'null' || sessionId === 'undefined') {
86 - sessionId = null;
87 - }
88 -
89 76 // Re-sync cookie from localStorage if cookie was lost
90 77 if (sessionId && !getCookie(cookieName)) {
91 78 document.cookie = cookieName + "=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
92 79 }
@@ -612,23 +599,13 @@
612 599
613 600 // Get instance for session start timestamp (used when persistence is OFF)
614 601 var instance = MxChatInstances.get(botId);
615 602
616 - // Guarantee a non-null session_id before the AJAX leaves. ensureSession() is idempotent
617 - // and returns the guaranteed-present session id from the in-memory instance even when
618 - // cookie/localStorage writes are silently blocked by the browser.
619 - var sessionId = MxChatInstances.ensureSession(botId);
620 - if (!sessionId || sessionId === 'null' || sessionId === 'undefined') {
621 - // Last-resort generation to ensure we never POST a null marker.
622 - sessionId = generateSessionId();
623 - MxChatInstances.setChatSession(botId, sessionId);
624 - }
625 -
626 603 // Prepare AJAX data
627 604 const ajaxData = {
628 605 action: 'mxchat_handle_chat_request',
629 606 message: message,
630 - session_id: sessionId,
607 + session_id: getChatSession(botId),
631 608 nonce: mxchatChat.nonce,
632 609 current_page_url: window.location.href,
633 610 current_page_title: document.title,
634 611 bot_id: botId,
@@ -861,22 +838,12 @@
861 838
862 839 // Get instance for session start timestamp (used when persistence is OFF)
863 840 var instance = MxChatInstances.get(botId);
864 841
865 - // Guarantee a non-null session_id before the fetch. FormData.append() stringifies any
866 - // non-string value via String(), so passing `null` would POST the literal string "null"
867 - // and land in the transcripts table as a ghost session. ensureSession() always returns
868 - // a real string even when cookies/localStorage are blocked.
869 - var streamSessionId = MxChatInstances.ensureSession(botId);
870 - if (!streamSessionId || streamSessionId === 'null' || streamSessionId === 'undefined') {
871 - streamSessionId = generateSessionId();
872 - MxChatInstances.setChatSession(botId, streamSessionId);
873 - }
874 -
875 842 const formData = new FormData();
876 843 formData.append('action', 'mxchat_stream_chat');
877 844 formData.append('message', message);
878 - formData.append('session_id', streamSessionId);
845 + formData.append('session_id', getChatSession(botId));
879 846 formData.append('nonce', mxchatChat.nonce);
880 847 formData.append('current_page_url', window.location.href);
881 848 formData.append('current_page_title', document.title);
882 849 formData.append('bot_id', botId);
@@ -976,16 +943,8 @@
976 943
977 944 // Re-enable chat input when stream ends with content
978 945 enableChatInput(botId);
979 946
980 - // Scroll the user's last message to the top now that the
981 - // bot's full reply has rendered (gives max reading room).
982 - var $chatBoxDone = getElement(botId, 'chat-box');
983 - var $lastUserMsgDone = $chatBoxDone.find('.user-message').last();
984 - if ($lastUserMsgDone.length) {
985 - scrollElementToTop($lastUserMsgDone, botId);
986 - }
987 -
988 947 if (callback) {
989 948 callback(accumulatedContent);
990 949 }
991 950 return;
@@ -1008,16 +967,8 @@
1008 967
1009 968 // Re-enable chat input after streaming completes
1010 969 enableChatInput(botId);
1011 970
1012 - // Scroll the user's last message to the top now
1013 - // that the bot's full reply has rendered.
1014 - var $chatBoxStreamDone = getElement(botId, 'chat-box');
1015 - var $lastUserMsgStreamDone = $chatBoxStreamDone.find('.user-message').last();
1016 - if ($lastUserMsgStreamDone.length) {
1017 - scrollElementToTop($lastUserMsgStreamDone, botId);
1018 - }
1019 -
1020 971 if (callback) {
1021 972 callback(accumulatedContent);
1022 973 }
1023 974 return;
@@ -2022,14 +1973,13 @@
2022 1973 requestAnimationFrame(smoothScroll);
2023 1974 }
2024 1975 }
2025 1976
2026 - function scrollElementToTop(element, botId, topOffset) {
1977 + function scrollElementToTop(element, botId) {
2027 1978 botId = botId || 'default';
2028 - topOffset = (typeof topOffset === 'number') ? topOffset : 2;
2029 1979 var chatBox = getElement(botId, 'chat-box');
2030 1980 var elementTop = element.position().top + chatBox.scrollTop();
2031 - chatBox.animate({ scrollTop: Math.max(0, elementTop - topOffset) }, 500);
1981 + chatBox.animate({ scrollTop: elementTop }, 500);
2032 1982 }
2033 1983
2034 1984 function showChatWidget(botId) {
2035 1985 botId = botId || 'default';