PluginProbe
MxChat – AI Chatbot & Content Generation for WordPress / 2.1.5
MxChat – AI Chatbot & Content Generation for WordPress v2.1.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 +1704 -1596 2.0.52.1.5 View file →
@@ -1,1596 +1,1704 @@
1 -jQuery(document).ready(function($) {
2 -//console.log('mxchatChat object:', mxchatChat);
3 -//console.log('Link Target Toggle Value:', mxchatChat.link_target_toggle);
4 -// Add these variables at the top of your chat-script.js file
5 - const toolbarIconColor = mxchatChat.toolbar_icon_color || '#212121';
6 -
7 - // Initialize color settings
8 - var userMessageBgColor = mxchatChat.user_message_bg_color;
9 - var userMessageFontColor = mxchatChat.user_message_font_color;
10 - var botMessageBgColor = mxchatChat.bot_message_bg_color;
11 - var botMessageFontColor = mxchatChat.bot_message_font_color;
12 - // Add live agent message colors
13 - var liveAgentMessageBgColor = mxchatChat.live_agent_message_bg_color;
14 - var liveAgentMessageFontColor = mxchatChat.live_agent_message_font_color;
15 -
16 -
17 - var linkTarget = mxchatChat.link_target_toggle === 'on' ? '_blank' : '_self';
18 - let lastSeenMessageId = '';
19 - let notificationCheckInterval;
20 - let notificationBadge;
21 - // Initialize session ID
22 - var sessionId = getChatSession();
23 -
24 - let pollingInterval; // Variable to store the interval ID
25 - let processedMessageIds = new Set(); // Add this at the top with your other variables
26 -//console.log('Live Agent BG Color:', liveAgentMessageBgColor);
27 -//console.log('Live Agent Font Color:', liveAgentMessageFontColor);
28 - let activePdfFile = null;
29 - let activeWordFile = null;
30 -
31 -
32 -// Function to create and append notification badge
33 -// Function to create and append notification badge
34 -function createNotificationBadge() {
35 - console.log("Creating notification badge...");
36 - const chatButton = document.getElementById('floating-chatbot-button');
37 - console.log("Chat button found:", !!chatButton);
38 -
39 - if (!chatButton) return;
40 -
41 - // Remove any existing badge first
42 - const existingBadge = chatButton.querySelector('.chat-notification-badge');
43 - if (existingBadge) {
44 - console.log("Removing existing badge");
45 - existingBadge.remove();
46 - }
47 -
48 - notificationBadge = document.createElement('div');
49 - notificationBadge.className = 'chat-notification-badge';
50 - notificationBadge.style.cssText = `
51 - display: none;
52 - position: absolute;
53 - top: -5px;
54 - right: -5px;
55 - background-color: red;
56 - color: white;
57 - border-radius: 50%;
58 - padding: 4px 8px;
59 - font-size: 12px;
60 - font-weight: bold;
61 - z-index: 10001;
62 - `;
63 - chatButton.style.position = 'relative';
64 - chatButton.appendChild(notificationBadge);
65 -
66 - console.log("Notification badge created and appended:", {
67 - exists: !!notificationBadge,
68 - parent: notificationBadge?.parentNode?.id,
69 - display: notificationBadge?.style?.display
70 - });
71 -}
72 -
73 -// Function to check for new messages
74 -function checkForNewMessages() {
75 - const sessionId = getChatSession();
76 - const chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
77 -
78 - if (!chatPersistenceEnabled) return;
79 -
80 - $.ajax({
81 - url: mxchatChat.ajax_url,
82 - type: 'POST',
83 - data: {
84 - action: 'mxchat_check_new_messages',
85 - session_id: sessionId,
86 - last_seen_id: lastSeenMessageId,
87 - nonce: mxchatChat.nonce
88 - },
89 - success: function(response) {
90 - if (response.success && response.data.hasNewMessages) {
91 - showNotification();
92 - }
93 - }
94 - });
95 -}
96 -
97 -// Function to show notification
98 -function showNotification() {
99 - const badge = document.getElementById('chat-notification-badge');
100 - if (badge && $('#floating-chatbot').hasClass('hidden')) {
101 - badge.style.display = 'block';
102 - badge.textContent = '1';
103 - }
104 -}
105 -
106 -function hideNotification() {
107 - const badge = document.getElementById('chat-notification-badge');
108 - if (badge) {
109 - badge.style.display = 'none';
110 - }
111 -}
112 -// Function to start notification checking
113 -function startNotificationChecking() {
114 - const chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
115 - if (!chatPersistenceEnabled) return;
116 -
117 - createNotificationBadge();
118 - notificationCheckInterval = setInterval(checkForNewMessages, 30000); // Check every 30 seconds
119 -}
120 -
121 -// Function to stop notification checking
122 -function stopNotificationChecking() {
123 - if (notificationCheckInterval) {
124 - clearInterval(notificationCheckInterval);
125 - }
126 -}
127 -
128 -
129 -
130 -
131 -function getChatSession() {
132 - var sessionId = getCookie('mxchat_session_id');
133 - //console.log("Session ID retrieved from cookie: ", sessionId);
134 -
135 - if (!sessionId) {
136 - sessionId = generateSessionId();
137 - //console.log("Generated new session ID: ", sessionId);
138 - setChatSession(sessionId);
139 - }
140 -
141 - //console.log("Final session ID: ", sessionId);
142 - return sessionId;
143 -}
144 -
145 -function setChatSession(sessionId) {
146 - // Set the cookie with a 24-hour expiration (86400 seconds)
147 - document.cookie = "mxchat_session_id=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
148 -}
149 -
150 -// Get cookie value by name
151 -function getCookie(name) {
152 - let value = "; " + document.cookie;
153 - let parts = value.split("; " + name + "=");
154 - if (parts.length == 2) return parts.pop().split(";").shift();
155 -}
156 -
157 -// Generate a new session ID
158 -function generateSessionId() {
159 - return 'mxchat_chat_' + Math.random().toString(36).substr(2, 9);
160 -}
161 -
162 -// Function to send the message to the chatbot (backend)
163 -function sendMessageToChatbot(message) {
164 - var sessionId = getChatSession(); // Reuse the session ID logic
165 -
166 - // Hide the popular questions section
167 - $('#mxchat-popular-questions').hide();
168 -
169 - // Show thinking indicator (no need to append the user's message again)
170 - appendThinkingMessage();
171 - scrollToBottom();
172 -
173 - //console.log("Sending message to chatbot:", message); // Log the message
174 - //console.log("Session ID:", sessionId); // Log the session ID
175 -
176 - // Call the chatbot using the same call logic as sendMessage
177 - callMxChat(message, function(response) {
178 - // ** Ensure temporary thinking message is removed before adding new response **
179 - $('.temporary-message').remove();
180 -
181 - // Replace thinking indicator with actual response
182 - replaceLastMessage("bot", response);
183 - });
184 -}
185 -
186 -
187 -
188 -
189 -function sendMessage() {
190 - var message = $('#chat-input').val(); // Get value from textarea
191 - if (message) {
192 - appendMessage("user", message); // Append user's message
193 - $('#chat-input').val(''); // Clear the textarea
194 - $('#chat-input').css('height', 'auto'); // Reset height after clearing content
195 -
196 - // Hide the popular questions section
197 - $('#mxchat-popular-questions').hide();
198 -
199 - // Show typing indicator
200 - appendThinkingMessage();
201 - scrollToBottom();
202 -
203 - callMxChat(message, function(response) {
204 - // Replace typing indicator with actual response
205 - replaceLastMessage("bot", response);
206 - });
207 - }
208 -}
209 -
210 -
211 -
212 - // Function to append a thinking message with animation
213 - function appendThinkingMessage() {
214 - // Remove any existing thinking dots first
215 - $('.thinking-dots').remove();
216 -
217 - // Retrieve the bot message font color and background color
218 - var botMessageFontColor = mxchatChat.bot_message_font_color;
219 - var botMessageBgColor = mxchatChat.bot_message_bg_color;
220 -
221 -
222 - var thinkingHtml = '<div class="thinking-dots-container">' +
223 - '<div class="thinking-dots">' +
224 - '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
225 - '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
226 - '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
227 - '</div>' +
228 - '</div>';
229 -
230 - // Append the thinking dots to the chat container (or within the temporary message div)
231 - $("#chat-box").append('<div class="bot-message temporary-message" style="background-color: ' + botMessageBgColor + ';">' + thinkingHtml + '</div>');
232 - scrollToBottom();
233 - }
234 -
235 - // Trigger send button click when "Enter" key is pressed in the textarea
236 - $('#chat-input').keypress(function(e) {
237 - if (e.which == 13 && !e.shiftKey) { // Check if "Enter" is pressed without Shift
238 - e.preventDefault(); // Prevent default "Enter" behavior
239 - $('#send-button').click(); // Trigger send button click
240 - }
241 - });
242 -
243 - // Handle send button click
244 - $('#send-button').click(function() {
245 - sendMessage();
246 - });
247 -
248 - // Handle click on popular questions
249 - $('.mxchat-popular-question').on('click', function () {
250 - var question = $(this).text(); // Get the text of the clicked question
251 -
252 - // Append the question as if the user typed it
253 - appendMessage("user", question);
254 -
255 - // Send the question to the server (backend)
256 - sendMessageToChatbot(question);
257 - });
258 -
259 -
260 -// Add this new function to handle markdown headers
261 -function formatMarkdownHeaders(text) {
262 - // Handle h1 to h6 headers
263 - return text.replace(/^(#{1,6})\s(.+)$/gm, function(match, hashes, content) {
264 - const level = hashes.length;
265 - return `<h${level} class="chat-heading">${content}</h${level}>`;
266 - });
267 -}
268 -
269 -// Update the linkify function to handle both URLs and markdown
270 -// Modified linkify function that only processes URLs in user content
271 -function linkify(inputText) {
272 - if (!inputText) return '';
273 -
274 - // Process markdown headers
275 - let processedText = formatMarkdownHeaders(inputText);
276 -
277 - // Process markdown links
278 - const markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s]+)\)/g;
279 - processedText = processedText.replace(markdownLinkPattern, (match, text, url) => {
280 - const safeUrl = encodeURI(url);
281 - const safeText = sanitizeUserInput(text);
282 - return `<a href="${safeUrl}" target="${linkTarget}">${safeText}</a>`;
283 - });
284 -
285 - // Process standalone URLs
286 - const urlPattern = /(^|[^">])(https?:\/\/[^\s<]+)/gim;
287 - processedText = processedText.replace(urlPattern, (match, prefix, url) => {
288 - const safeUrl = encodeURI(url);
289 - return `${prefix}<a href="${safeUrl}" target="${linkTarget}">${url}</a>`;
290 - });
291 -
292 - // Process www. URLs
293 - const wwwPattern = /(^|[^">])(www\.[\S]+(\b|$))(?![^<]*<\/a>)/gim;
294 - processedText = processedText.replace(wwwPattern, (match, prefix, url) => {
295 - const safeUrl = encodeURI(`http://${url}`);
296 - return `${prefix}<a href="${safeUrl}" target="${linkTarget}">${url}</a>`;
297 - });
298 -
299 - return processedText;
300 -}
301 -
302 -
303 -function scrollElementToTop(element) {
304 - var chatBox = $('#chat-box');
305 - var elementTop = element.position().top + chatBox.scrollTop();
306 - chatBox.animate({ scrollTop: elementTop }, 500);
307 -}
308 -
309 -
310 -// Optimized scrollToBottom function for instant scrolling
311 -function scrollToBottom(instant = false) {
312 - var chatBox = $('#chat-box');
313 - if (instant) {
314 - // Instantly set the scroll position to the bottom
315 - chatBox.scrollTop(chatBox.prop("scrollHeight"));
316 - } else {
317 - // Use requestAnimationFrame for smoother scrolling if needed
318 - let start = null;
319 - const scrollHeight = chatBox.prop("scrollHeight");
320 - const initialScroll = chatBox.scrollTop();
321 - const distance = scrollHeight - initialScroll;
322 - const duration = 500; // Duration in ms
323 -
324 - function smoothScroll(timestamp) {
325 - if (!start) start = timestamp;
326 - const progress = timestamp - start;
327 - const currentScroll = initialScroll + (distance * (progress / duration));
328 - chatBox.scrollTop(currentScroll);
329 -
330 - if (progress < duration) {
331 - requestAnimationFrame(smoothScroll);
332 - } else {
333 - chatBox.scrollTop(scrollHeight); // Ensure it's exactly at the bottom
334 - }
335 - }
336 -
337 - requestAnimationFrame(smoothScroll);
338 - }
339 -}
340 -
341 -
342 - // Function to format text with **bold** inside double asterisks
343 - function formatBoldText(text) {
344 - return text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
345 - }
346 -
347 - // Function to convert newline characters to HTML line breaks and handle paragraph spacing
348 -function convertNewlinesToBreaks(text) {
349 - // Split the text into paragraphs (marked by double newlines or multiple <br> tags)
350 - const paragraphs = text.split(/(?:\n\n|\<br\>\s*\<br\>)/g);
351 -
352 - // Wrap each paragraph in <p> tags
353 - return paragraphs
354 - .map(para => `<p>${para.trim()}</p>`)
355 - .join('');
356 -}
357 - // Copy to clipboard function
358 - // Function to copy text to clipboard
359 - function copyToClipboard(text) {
360 - var tempInput = $('<input>');
361 - $('body').append(tempInput);
362 - tempInput.val(text).select();
363 - document.execCommand('copy');
364 - tempInput.remove();
365 - }
366 -
367 -
368 -function updateChatModeIndicator(mode) {
369 - const indicator = document.getElementById('chat-mode-indicator');
370 - if (indicator) {
371 - indicator.textContent = mode === 'agent' ? 'Live Agent' : 'AI Agent';
372 - }
373 -
374 - // Start or stop polling based on mode
375 - if (mode === 'agent') {
376 - startPolling();
377 - } else {
378 - stopPolling();
379 - }
380 -}
381 -
382 -function callMxChat(message, callback) {
383 - $.ajax({
384 - url: mxchatChat.ajax_url,
385 - type: 'POST',
386 - dataType: 'json',
387 - data: {
388 - action: 'mxchat_handle_chat_request',
389 - message: message,
390 - session_id: getChatSession(),
391 - nonce: mxchatChat.nonce
392 - },
393 - success: function(response) {
394 - // Existing chat mode check
395 - if (response.chat_mode) {
396 - updateChatModeIndicator(response.chat_mode);
397 - }
398 - else if (response.fallbackResponse && response.fallbackResponse.chat_mode) {
399 - updateChatModeIndicator(response.fallbackResponse.chat_mode);
400 - }
401 -
402 - // Add PDF filename handling
403 - if (response.data && response.data.filename) {
404 - showActivePdf(response.data.filename);
405 - activePdfFile = response.data.filename;
406 - }
407 -
408 - // Add redirect check here
409 - if (response.redirect_url) {
410 - let responseText = response.text || '';
411 - if (responseText) {
412 - replaceLastMessage("bot", responseText);
413 - }
414 - setTimeout(() => {
415 - window.location.href = response.redirect_url;
416 - }, 1500);
417 - return;
418 - }
419 -
420 -
421 - // Check for live agent response
422 - if (response.success && response.data && response.data.status === 'waiting_for_agent') {
423 - updateChatModeIndicator('agent');
424 - return;
425 - }
426 -
427 - // Handle other responses
428 - let responseText = response.text || '';
429 - let responseHtml = response.html || '';
430 - let responseMessage = response.message || '';
431 -
432 - if (responseText === 'You are now chatting with the AI chatbot.') {
433 - updateChatModeIndicator('ai');
434 - }
435 -
436 - // Handle the message and show notification if chat is hidden
437 - if (responseText || responseHtml || responseMessage) {
438 - // Update the messages as before
439 - if (responseText && responseHtml) {
440 - replaceLastMessage("bot", responseText, responseHtml);
441 - } else if (responseText) {
442 - replaceLastMessage("bot", responseText);
443 - } else if (responseHtml) {
444 - replaceLastMessage("bot", "", responseHtml);
445 - } else if (responseMessage) {
446 - replaceLastMessage("bot", responseMessage);
447 - }
448 -
449 - // Check if chat is hidden and show notification
450 - if ($('#floating-chatbot').hasClass('hidden')) {
451 - const badge = $('#chat-notification-badge');
452 - if (badge.length) {
453 - badge.show();
454 - }
455 - }
456 - } else {
457 - console.error("Unexpected response format:", response);
458 - replaceLastMessage("bot", "I'm sorry, something went wrong.");
459 - }
460 -
461 - if (response.message_id) {
462 - lastSeenMessageId = response.message_id;
463 - }
464 - },
465 - error: function(xhr, status, error) {
466 - replaceLastMessage("bot", "An unexpected error occurred.");
467 - }
468 - });
469 -}
470 -
471 -// Sanitize only user input
472 -function sanitizeUserInput(text) {
473 - const div = document.createElement('div');
474 - div.textContent = text;
475 - return div.innerHTML;
476 -}
477 -
478 -// Modified appendMessage function that only sanitizes user content
479 -function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false) {
480 - try {
481 - // Determine styles based on sender type
482 - let messageClass, bgColor, fontColor;
483 -
484 - if (sender === "user") {
485 - messageClass = "user-message";
486 - bgColor = userMessageBgColor;
487 - fontColor = userMessageFontColor;
488 - // Only sanitize user input
489 - messageText = sanitizeUserInput(messageText);
490 - } else if (sender === "agent") {
491 - messageClass = "agent-message";
492 - bgColor = liveAgentMessageBgColor;
493 - fontColor = liveAgentMessageFontColor;
494 - } else {
495 - messageClass = "bot-message";
496 - bgColor = botMessageBgColor;
497 - fontColor = botMessageFontColor;
498 - }
499 -
500 - const messageDiv = $('<div>')
501 - .addClass(messageClass)
502 - .css({
503 - 'background': bgColor,
504 - 'color': fontColor,
505 - 'margin-bottom': '1em'
506 - });
507 -
508 - // Process the message content based on sender
509 - let fullMessage;
510 - if (sender === "user") {
511 - // For user messages, apply linkify after sanitization
512 - fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(messageText))));
513 - } else {
514 - // For bot/agent messages, preserve HTML
515 - fullMessage = messageText;
516 - }
517 -
518 - // Add images if provided
519 - if (images && images.length > 0) {
520 - fullMessage += '<div class="image-gallery">';
521 - images.forEach(img => {
522 - // Ensure image URLs and titles are properly escaped
523 - const safeTitle = sanitizeUserInput(img.title);
524 - const safeUrl = encodeURI(img.image_url);
525 - const safeThumbnail = encodeURI(img.thumbnail_url);
526 -
527 - fullMessage += `
528 - <div style="margin-bottom: 10px;">
529 - <strong>${safeTitle}</strong><br>
530 - <a href="${safeUrl}" target="_blank">
531 - <img src="${safeThumbnail}" alt="${safeTitle}" style="max-width: 100px; height: auto; margin: 5px;" />
532 - </a>
533 - </div>`;
534 - });
535 - fullMessage += '</div>';
536 - }
537 -
538 - // Append HTML content if provided
539 - if (messageHtml && sender !== "user") {
540 - fullMessage += '<br><br>' + messageHtml;
541 - }
542 -
543 - messageDiv.html(fullMessage);
544 -
545 - if (isTemporary) {
546 - messageDiv.addClass('temporary-message');
547 - }
548 -
549 - messageDiv.hide().appendTo('#chat-box').fadeIn(300, function() {
550 - if (sender === "bot") {
551 - const lastUserMessage = $('#chat-box').find('.user-message').last();
552 - if (lastUserMessage.length) {
553 - scrollElementToTop(lastUserMessage);
554 - }
555 - }
556 - });
557 -
558 - if (messageText.id) {
559 - lastSeenMessageId = messageText.id;
560 - hideNotification();
561 - }
562 - } catch (error) {
563 - console.error("Error rendering message:", error);
564 - }
565 -}
566 -
567 -
568 -function replaceLastMessage(sender, responseText, responseHtml = '', images = []) {
569 - var messageClass = sender === "user" ? "user-message" : sender === "agent" ? "agent-message" : "bot-message";
570 - var lastMessageDiv = $('#chat-box').find('.bot-message.temporary-message, .agent-message.temporary-message').last();
571 -
572 - // Determine styles
573 - let bgColor, fontColor;
574 - if (sender === "user") {
575 - bgColor = userMessageBgColor;
576 - fontColor = userMessageFontColor;
577 - } else if (sender === "agent") {
578 - bgColor = liveAgentMessageBgColor;
579 - fontColor = liveAgentMessageFontColor;
580 - } else {
581 - bgColor = botMessageBgColor;
582 - fontColor = botMessageFontColor;
583 - }
584 -
585 - var fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(responseText))));
586 - if (responseHtml) {
587 - fullMessage += '<br><br>' + responseHtml;
588 - }
589 -
590 - if (images.length > 0) {
591 - fullMessage += '<div class="image-gallery">';
592 - images.forEach(img => {
593 - fullMessage += `
594 - <div style="margin-bottom: 10px;">
595 - <strong>${img.title}</strong><br>
596 - <a href="${img.image_url}" target="_blank">
597 - <img src="${img.thumbnail_url}" alt="${img.title}" style="max-width: 100px; height: auto; margin: 5px;" />
598 - </a>
599 - </div>`;
600 - });
601 - fullMessage += '</div>';
602 - }
603 -
604 - if (lastMessageDiv.length) {
605 - lastMessageDiv.fadeOut(200, function() {
606 - $(this)
607 - .html(fullMessage)
608 - .removeClass('bot-message user-message')
609 - .addClass(messageClass)
610 - .css({
611 - 'background-color': bgColor,
612 - 'color': fontColor,
613 - })
614 - .removeClass('temporary-message')
615 - .fadeIn(200, function() {
616 - if (sender === "bot" || sender === "agent") {
617 - const lastUserMessage = $('#chat-box').find('.user-message').last();
618 - if (lastUserMessage.length) {
619 - scrollElementToTop(lastUserMessage);
620 - }
621 - // Show notification if chat is hidden
622 - if ($('#floating-chatbot').hasClass('hidden')) {
623 - showNotification();
624 - }
625 - }
626 - });
627 - });
628 - } else {
629 - appendMessage(sender, responseText, responseHtml, images);
630 - }
631 -}
632 -
633 -
634 -function startPolling() {
635 - // Clear any existing interval first
636 - stopPolling();
637 - // Start new polling interval
638 - pollingInterval = setInterval(checkForAgentMessages, 5000);
639 - //console.log("Started agent message polling");
640 -}
641 -
642 -function stopPolling() {
643 - if (pollingInterval) {
644 - clearInterval(pollingInterval);
645 - pollingInterval = null;
646 - //console.log("Stopped agent message polling");
647 - }
648 -}
649 -
650 -
651 -// Update your checkForAgentMessages function
652 -function checkForAgentMessages() {
653 - const sessionId = getChatSession();
654 - $.ajax({
655 - url: mxchatChat.ajax_url,
656 - type: 'POST',
657 - dataType: 'json',
658 - data: {
659 - action: 'mxchat_fetch_new_messages',
660 - session_id: sessionId,
661 - last_seen_id: lastSeenMessageId,
662 - nonce: mxchatChat.nonce
663 - },
664 - success: function (response) {
665 - if (response.success && response.data?.new_messages) {
666 - let hasNewMessage = false;
667 -
668 - response.data.new_messages.forEach(function (message) {
669 - if (message.role === "agent" && !processedMessageIds.has(message.id)) {
670 - hasNewMessage = true;
671 - replaceLastMessage("agent", message.content);
672 - lastSeenMessageId = message.id;
673 - processedMessageIds.add(message.id);
674 - }
675 - });
676 -
677 - if (hasNewMessage && $('#floating-chatbot').hasClass('hidden')) {
678 - showNotification();
679 - }
680 -
681 - scrollToBottom(true);
682 - }
683 - },
684 - error: function (xhr, status, error) {
685 - console.error("Polling error:", xhr, status, error);
686 - }
687 - });
688 -}
689 -function loadChatHistory() {
690 - var sessionId = getChatSession();
691 - var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
692 -
693 - if (chatPersistenceEnabled && sessionId) {
694 - $.ajax({
695 - url: mxchatChat.ajax_url,
696 - type: 'POST',
697 - dataType: 'json',
698 - data: {
699 - action: 'mxchat_fetch_conversation_history',
700 - session_id: sessionId
701 - },
702 - success: function(response) {
703 - if (response.success && response.data && Array.isArray(response.data.conversation)) {
704 -
705 -
706 - var $chatBox = $('#chat-box');
707 - var $fragment = $(document.createDocumentFragment());
708 - let highestMessageId = lastSeenMessageId;
709 -
710 - if (response.data.chat_mode) {
711 - updateChatModeIndicator(response.data.chat_mode);
712 - }
713 -
714 - $.each(response.data.conversation, function(index, message) {
715 - // Skip agent messages if persistence is off
716 - if (!chatPersistenceEnabled && message.role === 'agent') {
717 - return;
718 - }
719 -
720 - var messageClass, messageBgColor, messageFontColor;
721 -
722 - switch (message.role) {
723 - case 'user':
724 - messageClass = 'user-message';
725 - messageBgColor = userMessageBgColor;
726 - messageFontColor = userMessageFontColor;
727 - break;
728 - case 'agent':
729 - messageClass = 'agent-message';
730 - messageBgColor = liveAgentMessageBgColor;
731 - messageFontColor = liveAgentMessageFontColor;
732 - break;
733 - default:
734 - messageClass = 'bot-message';
735 - messageBgColor = botMessageBgColor;
736 - messageFontColor = botMessageFontColor;
737 - break;
738 - }
739 -
740 - var messageElement = $('<div>').addClass(messageClass)
741 - .css({
742 - 'background': messageBgColor,
743 - 'color': messageFontColor
744 - });
745 -
746 - var content = message.content;
747 - content = content.replace(/\\'/g, "'").replace(/\\"/g, '"');
748 - content = decodeHTMLEntities(content);
749 -
750 - if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) {
751 - messageElement.html(content);
752 - } else {
753 - var formattedContent = linkify(
754 - formatBoldText(
755 - convertNewlinesToBreaks(formatCodeBlocks(content))
756 - )
757 - );
758 - messageElement.html(formattedContent);
759 - }
760 -
761 - $fragment.append(messageElement);
762 -
763 - // In loadChatHistory, change this part:
764 - if (message.id) {
765 - highestMessageId = Math.max(highestMessageId, message.id);
766 - processedMessageIds.add(message.id); // Add all message IDs to processed set
767 - }
768 - });
769 -
770 - $chatBox.append($fragment);
771 - scrollToBottom(true);
772 -
773 - if (response.data.conversation.length > 0) {
774 - $('#mxchat-popular-questions').hide();
775 - }
776 -
777 - // Update lastSeenMessageId after history loads
778 - lastSeenMessageId = highestMessageId;
779 -
780 - // Only update chat mode if persistence is enabled
781 - if (chatPersistenceEnabled && response.data.conversation.length > 0) {
782 - var lastMessage = response.data.conversation[response.data.conversation.length - 1];
783 - if (lastMessage.role === 'agent') {
784 - updateChatModeIndicator('agent');
785 - }
786 - }
787 - } else {
788 - console.warn("No conversation history found.");
789 - }
790 - },
791 - error: function(xhr, status, error) {
792 - console.error("Error loading chat history:", status, error);
793 - appendMessage("bot", "Unable to load chat history.");
794 - }
795 - });
796 - } else {
797 - console.warn("Chat persistence is disabled or no session ID found. Not loading history.");
798 - }
799 -}
800 -
801 -// Function to decode HTML entities
802 -function decodeHTMLEntities(text) {
803 - var textArea = document.createElement('textarea');
804 - textArea.innerHTML = text;
805 - return textArea.value;
806 -}
807 -
808 -
809 -// Update formatCodeBlocks function
810 -function formatCodeBlocks(text) {
811 - // First handle raw PHP tags
812 - text = text.replace(/(<\?php[\s\S]*?\?>)/g, (match) => {
813 - return `<pre><code class="language-php">${escapeHtml(match)}</code></pre>`;
814 - });
815 -
816 - // Then handle code blocks with backticks
817 - text = text.replace(/```php5?\n([\s\S]+?)```/gi, (match, code) => {
818 - return `<pre><code class="language-php">${escapeHtml(code)}</code></pre>`;
819 - });
820 -
821 - return text;
822 -}
823 -
824 -// Update escapeHtml function to preserve existing code blocks
825 -function escapeHtml(unsafe) {
826 - // First check if it's already a code block
827 - if (unsafe.includes('<pre><code') || unsafe.includes('</code></pre>')) {
828 - return unsafe;
829 - }
830 -
831 - return unsafe
832 - .replace(/&/g, "&amp;")
833 - .replace(/</g, "&lt;")
834 - .replace(/>/g, "&gt;")
835 - .replace(/"/g, "&quot;")
836 - .replace(/'/g, "&#039;");
837 -}
838 -// Utility function to escape HTML
839 -function escapeHtml(unsafe) {
840 - return unsafe
841 - .replace(/&/g, "&amp;")
842 - .replace(/</g, "&lt;")
843 - .replace(/>/g, "&gt;")
844 - .replace(/"/g, "&quot;")
845 - .replace(/'/g, "&#039;");
846 -}
847 -
848 -
849 -
850 -
851 -// Function to convert newlines, skipping preformatted text
852 -function convertNewlinesToBreaks(text) {
853 - // Split while preserving code blocks
854 - return text.split(/(<pre\b[^>]*>[\s\S]*?<\/pre>)/g).map(part => {
855 - if (part.startsWith('<pre')) return part;
856 - return part.replace(/(^|[^>])\n/g, '$1<br>');
857 - }).join('');
858 -}
859 -
860 -
861 -
862 -
863 - // Helper function to check if a string is an image HTML
864 - function isImageHtml(str) {
865 - return str.startsWith('<img') && str.endsWith('>');
866 - }
867 -
868 - // Function to remove thinking dots
869 - function removeThinkingDots() {
870 - $('.thinking-dots').closest('.temporary-message').remove();
871 - }
872 -
873 - function isMobile() {
874 - // This can be a simple check, or more sophisticated detection of mobile devices
875 - return window.innerWidth <= 768; // Example threshold for mobile devices
876 - }
877 -
878 - function disableScroll() {
879 - if (isMobile()) {
880 - $('body').css('overflow', 'hidden');
881 - }
882 - }
883 -
884 - function enableScroll() {
885 - if (isMobile()) {
886 - $('body').css('overflow', '');
887 - }
888 - }
889 -
890 -// Pre-chat dismissal check function (wrapped in a function for reuse)
891 - function checkPreChatDismissal() {
892 - $.ajax({
893 - url: mxchatChat.ajax_url,
894 - type: 'POST',
895 - data: {
896 - action: 'mxchat_check_pre_chat_message_status',
897 - _ajax_nonce: mxchatChat.nonce
898 - },
899 - success: function(response) {
900 - if (response.success && !response.data.dismissed) {
901 - $('#pre-chat-message').fadeIn(250);
902 - } else {
903 - $('#pre-chat-message').hide();
904 - }
905 - },
906 - error: function() {
907 - console.error('Failed to check pre-chat message dismissal status.');
908 - }
909 - });
910 - }
911 -
912 - // Function to show the chatbot widget
913 -function showChatWidget() {
914 - // First ensure display is set
915 - $('#floating-chatbot-button').css('display', 'flex');
916 - // Then handle the fade
917 - $('#floating-chatbot-button').fadeTo(500, 1);
918 - // Force visibility
919 - $('#floating-chatbot-button').removeClass('hidden');
920 - //console.log('Showing widget');
921 -}
922 -
923 -// Function to hide the chatbot widget
924 -function hideChatWidget() {
925 - $('#floating-chatbot-button').css('display', 'none');
926 - $('#floating-chatbot-button').addClass('hidden');
927 - //console.log('Hiding widget');
928 -}
929 -
930 -function initializeChatVisibility() {
931 - //console.log('Initializing chat visibility');
932 - const complianzEnabled = mxchatChat.complianz_toggle === 'on' ||
933 - mxchatChat.complianz_toggle === '1' ||
934 - mxchatChat.complianz_toggle === 1;
935 -
936 - if (complianzEnabled && typeof cmplz_has_consent === "function" && typeof complianz !== 'undefined') {
937 - // Initial check
938 - checkConsentAndShowChat();
939 -
940 - // Listen for consent changes
941 - $(document).on('cmplz_status_change', function(event) {
942 - //console.log('Status change detected');
943 - checkConsentAndShowChat();
944 - });
945 - } else {
946 - // If Complianz is not enabled, always show
947 - $('#floating-chatbot-button')
948 - .css('display', 'flex')
949 - .removeClass('hidden no-consent')
950 - .fadeTo(500, 1);
951 -
952 - // Also check pre-chat message when Complianz is not enabled
953 - checkPreChatDismissal();
954 - }
955 -}
956 -
957 -
958 -
959 -function checkConsentAndShowChat() {
960 - var consentStatus = cmplz_has_consent('marketing');
961 - var consentType = complianz.consenttype;
962 -
963 - //console.log('Checking consent:', {status: consentStatus,type: consentType});
964 -
965 - let $widget = $('#floating-chatbot-button');
966 - let $chatbot = $('#floating-chatbot');
967 - let $preChat = $('#pre-chat-message');
968 -
969 - if (consentStatus === true) {
970 - //console.log('Consent granted - showing widget');
971 - $widget
972 - .removeClass('no-consent')
973 - .css('display', 'flex')
974 - .removeClass('hidden')
975 - .fadeTo(500, 1);
976 - $chatbot.removeClass('no-consent');
977 -
978 - // Show pre-chat message if not dismissed
979 - checkPreChatDismissal();
980 - } else {
981 - //console.log('No consent - hiding widget');
982 - $widget
983 - .addClass('no-consent')
984 - .fadeTo(500, 0, function() {
985 - $(this)
986 - .css('display', 'none')
987 - .addClass('hidden');
988 - });
989 - $chatbot.addClass('no-consent');
990 -
991 - // Hide pre-chat message when no consent
992 - $preChat.hide();
993 - }
994 -}
995 -
996 - // Function to dismiss pre-chat message for 24 hours
997 - function handlePreChatDismissal() {
998 - $('#pre-chat-message').fadeOut(200);
999 - $.ajax({
1000 - url: mxchatChat.ajax_url,
1001 - type: 'POST',
1002 - data: {
1003 - action: 'mxchat_dismiss_pre_chat_message',
1004 - _ajax_nonce: mxchatChat.nonce
1005 - },
1006 - success: function() {
1007 - $('#pre-chat-message').hide();
1008 - },
1009 - error: function() {
1010 - console.error('Failed to dismiss pre-chat message.');
1011 - }
1012 - });
1013 - }
1014 -
1015 - // Handle pre-chat message dismissal on button click
1016 - $(document).on('click', '.close-pre-chat-message', function(e) {
1017 - e.stopPropagation();
1018 - handlePreChatDismissal();
1019 - });
1020 -
1021 - // Toggle chatbot visibility on floating button click
1022 - $(document).on('click', '#floating-chatbot-button', function() {
1023 - var chatbot = $('#floating-chatbot');
1024 - if (chatbot.hasClass('hidden')) {
1025 - chatbot.removeClass('hidden').addClass('visible');
1026 - $(this).addClass('hidden');
1027 - $('#chat-notification-badge').hide(); // Hide notification when opening chat
1028 - disableScroll();
1029 - $('#pre-chat-message').fadeOut(250);
1030 - } else {
1031 - chatbot.removeClass('visible').addClass('hidden');
1032 - $(this).removeClass('hidden');
1033 - enableScroll();
1034 - checkPreChatDismissal();
1035 - }
1036 - });
1037 -
1038 - $(document).on('click', '#exit-chat-button', function() {
1039 - $('#floating-chatbot').addClass('hidden').removeClass('visible');
1040 - $('#floating-chatbot-button').removeClass('hidden');
1041 - enableScroll();
1042 - });
1043 -
1044 - // Close pre-chat message on click
1045 - $(document).on('click', '.close-pre-chat-message', function(e) {
1046 - e.stopPropagation(); // Prevent triggering the parent .pre-chat-message click
1047 - $('#pre-chat-message').fadeOut(200, function() {
1048 - $(this).remove();
1049 - });
1050 - });
1051 -
1052 - // Open chatbot when pre-chat message is clicked
1053 - $(document).on('click', '#pre-chat-message', function() {
1054 - var chatbot = $('#floating-chatbot');
1055 - if (chatbot.hasClass('hidden')) {
1056 - chatbot.removeClass('hidden').addClass('visible');
1057 - $('#floating-chatbot-button').addClass('hidden');
1058 - $('#pre-chat-message').fadeOut(250); // Hide pre-chat message
1059 - disableScroll(); // Disable scroll when chatbot opens
1060 - }
1061 - });
1062 -
1063 - // If the chatbot is initially hidden, ensure the button is visible
1064 - if ($('#floating-chatbot').hasClass('hidden')) {
1065 - $('#floating-chatbot-button').removeClass('hidden');
1066 - }
1067 -
1068 - function setFullHeight() {
1069 - var vh = $(window).innerHeight() * 0.01;
1070 - $(':root').css('--vh', vh + 'px');
1071 - }
1072 -
1073 - // Set the height when the page loads
1074 -
1075 -
1076 - // Set the height on resize and orientation change events
1077 - $(window).on('resize orientationchange', function() {
1078 - setFullHeight();
1079 - });
1080 -
1081 -
1082 - // Now handle the close button to dismiss the pre-chat message for 24 hours
1083 - var closeButton = document.querySelector('.close-pre-chat-message');
1084 - if (closeButton) {
1085 - closeButton.addEventListener('click', function() {
1086 - $('#pre-chat-message').fadeOut(200); // Hide the message
1087 -
1088 - // Send an AJAX request to set the transient flag for 24 hours
1089 - $.ajax({
1090 - url: mxchatChat.ajax_url,
1091 - type: 'POST',
1092 - data: {
1093 - action: 'mxchat_dismiss_pre_chat_message',
1094 - _ajax_nonce: mxchatChat.nonce
1095 - },
1096 - success: function() {
1097 - //console.log('Pre-chat message dismissed for 24 hours.');
1098 -
1099 - // Ensure the message is hidden after dismissal
1100 - $('#pre-chat-message').hide();
1101 - },
1102 - error: function() {
1103 - //console.error('Failed to dismiss pre-chat message.');
1104 - }
1105 - });
1106 - });
1107 - }
1108 -
1109 -
1110 -
1111 -
1112 -// Event listener for Add to Cart button
1113 -$(document).on('click', '.mxchat-add-to-cart-button', function() {
1114 - var productId = $(this).data('product-id');
1115 - // Add a special prefix to indicate this is from button
1116 - appendMessage("user", "add to cart");
1117 - sendMessageToChatbot("!addtocart"); // Special command to indicate button click
1118 -});
1119 -
1120 -
1121 -if (document.getElementById('pdf-upload-btn')) {
1122 - document.getElementById('pdf-upload-btn').addEventListener('click', function() {
1123 - document.getElementById('pdf-upload').click();
1124 - });
1125 -}
1126 -
1127 -if (document.getElementById('word-upload-btn')) {
1128 - document.getElementById('word-upload-btn').addEventListener('click', function() {
1129 - document.getElementById('word-upload').click();
1130 - });
1131 -}
1132 -
1133 -// PDF file input change handler
1134 -document.getElementById('pdf-upload').addEventListener('change', async function(e) {
1135 - const file = e.target.files[0];
1136 -
1137 - if (!file || file.type !== 'application/pdf') {
1138 - alert('Please select a valid PDF file.');
1139 - return;
1140 - }
1141 -
1142 - if (!sessionId) {
1143 - console.error('No session ID found');
1144 - alert('Error: No session ID found');
1145 - return;
1146 - }
1147 -
1148 - if (!mxchatChat || !mxchatChat.ajax_url || !mxchatChat.nonce) {
1149 - console.error('mxchatChat not properly configured:', mxchatChat);
1150 - alert('Error: Ajax configuration missing');
1151 - return;
1152 - }
1153 -
1154 - // Disable buttons and show loading state
1155 - const uploadBtn = document.getElementById('pdf-upload-btn');
1156 - const sendBtn = document.getElementById('send-button');
1157 - const originalBtnContent = uploadBtn.innerHTML;
1158 -
1159 - try {
1160 - const formData = new FormData();
1161 - formData.append('action', 'mxchat_upload_pdf');
1162 - formData.append('pdf_file', file);
1163 - formData.append('session_id', sessionId);
1164 - formData.append('nonce', mxchatChat.nonce);
1165 -
1166 - uploadBtn.disabled = true;
1167 - sendBtn.disabled = true;
1168 - uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
1169 - <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
1170 - </svg>`;
1171 -
1172 - const response = await fetch(mxchatChat.ajax_url, {
1173 - method: 'POST',
1174 - body: formData
1175 - });
1176 -
1177 - const data = await response.json();
1178 -
1179 - if (data.success) {
1180 - // Hide popular questions if they exist
1181 - const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
1182 - if (popularQuestionsContainer) {
1183 - popularQuestionsContainer.style.display = 'none';
1184 - }
1185 -
1186 - // Show the active PDF name
1187 - showActivePdf(data.data.filename);
1188 -
1189 - appendMessage('bot', data.data.message);
1190 - scrollToBottom();
1191 - activePdfFile = data.data.filename;
1192 - } else {
1193 - console.error('Upload failed:', data.data);
1194 - alert('Failed to upload PDF. Please try again.');
1195 - }
1196 - } catch (error) {
1197 - console.error('Upload error:', error);
1198 - alert('Error uploading file. Please try again.');
1199 - } finally {
1200 - uploadBtn.disabled = false;
1201 - sendBtn.disabled = false;
1202 - uploadBtn.innerHTML = originalBtnContent;
1203 - this.value = ''; // Reset file input
1204 - }
1205 -});
1206 -
1207 -// Word file input change handler
1208 -document.getElementById('word-upload').addEventListener('change', async function(e) {
1209 - const file = e.target.files[0];
1210 -
1211 - if (!file || file.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
1212 - alert('Please select a valid Word document (.docx).');
1213 - return;
1214 - }
1215 -
1216 - if (!sessionId) {
1217 - console.error('No session ID found');
1218 - alert('Error: No session ID found');
1219 - return;
1220 - }
1221 -
1222 - // Disable buttons and show loading state
1223 - const uploadBtn = document.getElementById('word-upload-btn');
1224 - const sendBtn = document.getElementById('send-button');
1225 - const originalBtnContent = uploadBtn.innerHTML;
1226 -
1227 - try {
1228 - const formData = new FormData();
1229 - formData.append('action', 'mxchat_upload_word');
1230 - formData.append('word_file', file);
1231 - formData.append('session_id', sessionId);
1232 - formData.append('nonce', mxchatChat.nonce);
1233 -
1234 - uploadBtn.disabled = true;
1235 - sendBtn.disabled = true;
1236 - uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
1237 - <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
1238 - </svg>`;
1239 -
1240 - const response = await fetch(mxchatChat.ajax_url, {
1241 - method: 'POST',
1242 - body: formData
1243 - });
1244 -
1245 - const data = await response.json();
1246 -
1247 - if (data.success) {
1248 - // Hide popular questions if they exist
1249 - const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
1250 - if (popularQuestionsContainer) {
1251 - popularQuestionsContainer.style.display = 'none';
1252 - }
1253 -
1254 - // Show the active Word document name
1255 - showActiveWord(data.data.filename);
1256 -
1257 - appendMessage('bot', data.data.message);
1258 - scrollToBottom();
1259 - activeWordFile = data.data.filename;
1260 - } else {
1261 - console.error('Upload failed:', data.data);
1262 - alert('Failed to upload Word document. Please try again.');
1263 - }
1264 - } catch (error) {
1265 - console.error('Upload error:', error);
1266 - alert('Error uploading file. Please try again.');
1267 - } finally {
1268 - uploadBtn.disabled = false;
1269 - sendBtn.disabled = false;
1270 - uploadBtn.innerHTML = originalBtnContent;
1271 - this.value = ''; // Reset file input
1272 - }
1273 -});
1274 -
1275 -// Function to show active PDF name in toolbar
1276 -function showActivePdf(filename) {
1277 - const container = document.getElementById('active-pdf-container');
1278 - const nameElement = document.getElementById('active-pdf-name');
1279 -
1280 - if (!container || !nameElement) {
1281 - console.error('PDF container elements not found');
1282 - return;
1283 - }
1284 -
1285 - nameElement.textContent = filename;
1286 - container.style.display = 'flex';
1287 -}
1288 -
1289 -// Function to show active Word document name in toolbar
1290 -function showActiveWord(filename) {
1291 - const container = document.getElementById('active-word-container');
1292 - const nameElement = document.getElementById('active-word-name');
1293 -
1294 - if (!container || !nameElement) {
1295 - console.error('Word document container elements not found');
1296 - return;
1297 - }
1298 -
1299 - nameElement.textContent = filename;
1300 - container.style.display = 'flex';
1301 -}
1302 -
1303 -// Function to remove active PDF
1304 -function removeActivePdf() {
1305 - const container = document.getElementById('active-pdf-container');
1306 - const nameElement = document.getElementById('active-pdf-name');
1307 -
1308 - if (!container || !nameElement || !activePdfFile) return;
1309 -
1310 - fetch(mxchatChat.ajax_url, {
1311 - method: 'POST',
1312 - headers: {
1313 - 'Content-Type': 'application/x-www-form-urlencoded',
1314 - },
1315 - body: new URLSearchParams({
1316 - 'action': 'mxchat_remove_pdf',
1317 - 'session_id': sessionId,
1318 - 'nonce': mxchatChat.nonce
1319 - })
1320 - })
1321 - .then(response => response.json())
1322 - .then(data => {
1323 - if (data.success) {
1324 - container.style.display = 'none';
1325 - nameElement.textContent = '';
1326 - activePdfFile = null;
1327 - appendMessage('bot', 'PDF removed.');
1328 - }
1329 - })
1330 - .catch(error => {
1331 - console.error('Error removing PDF:', error);
1332 - });
1333 -}
1334 -
1335 -// Function to remove active Word document
1336 -function removeActiveWord() {
1337 - const container = document.getElementById('active-word-container');
1338 - const nameElement = document.getElementById('active-word-name');
1339 -
1340 - if (!container || !nameElement || !activeWordFile) return;
1341 -
1342 - fetch(mxchatChat.ajax_url, {
1343 - method: 'POST',
1344 - headers: {
1345 - 'Content-Type': 'application/x-www-form-urlencoded',
1346 - },
1347 - body: new URLSearchParams({
1348 - 'action': 'mxchat_remove_word',
1349 - 'session_id': sessionId,
1350 - 'nonce': mxchatChat.nonce
1351 - })
1352 - })
1353 - .then(response => response.json())
1354 - .then(data => {
1355 - if (data.success) {
1356 - container.style.display = 'none';
1357 - nameElement.textContent = '';
1358 - activeWordFile = null;
1359 - appendMessage('bot', 'Word document removed.');
1360 - }
1361 - })
1362 - .catch(error => {
1363 - console.error('Error removing Word document:', error);
1364 - });
1365 -}
1366 -
1367 -// Add remove button click handlers
1368 -document.getElementById('remove-pdf-btn')?.addEventListener('click', function(e) {
1369 - e.preventDefault();
1370 - e.stopPropagation();
1371 - removeActivePdf();
1372 -});
1373 -
1374 -document.getElementById('remove-word-btn')?.addEventListener('click', function(e) {
1375 - e.preventDefault();
1376 - e.stopPropagation();
1377 - removeActiveWord();
1378 -});
1379 -
1380 -// Check initial document status
1381 -function checkInitialDocumentStatus() {
1382 - if (!sessionId) return;
1383 -
1384 - // Check PDF status
1385 - fetch(mxchatChat.ajax_url, {
1386 - method: 'POST',
1387 - headers: {
1388 - 'Content-Type': 'application/x-www-form-urlencoded',
1389 - },
1390 - body: new URLSearchParams({
1391 - 'action': 'mxchat_check_pdf_status',
1392 - 'session_id': sessionId,
1393 - 'nonce': mxchatChat.nonce
1394 - })
1395 - })
1396 - .then(response => response.json())
1397 - .then(data => {
1398 - if (data.success && data.data.filename) {
1399 - showActivePdf(data.data.filename);
1400 - activePdfFile = data.data.filename;
1401 - }
1402 - })
1403 - .catch(error => {
1404 - console.error('Error checking PDF status:', error);
1405 - });
1406 -
1407 - // Check Word document status
1408 - fetch(mxchatChat.ajax_url, {
1409 - method: 'POST',
1410 - headers: {
1411 - 'Content-Type': 'application/x-www-form-urlencoded',
1412 - },
1413 - body: new URLSearchParams({
1414 - 'action': 'mxchat_check_word_status',
1415 - 'session_id': sessionId,
1416 - 'nonce': mxchatChat.nonce
1417 - })
1418 - })
1419 - .then(response => response.json())
1420 - .then(data => {
1421 - if (data.success && data.data.filename) {
1422 - showActiveWord(data.data.filename);
1423 - activeWordFile = data.data.filename;
1424 - }
1425 - })
1426 - .catch(error => {
1427 - console.error('Error checking Word document status:', error);
1428 - });
1429 -}
1430 -
1431 -// Apply toolbar settings
1432 -if (mxchatChat.chat_toolbar_toggle === 'on') {
1433 - $('.chat-toolbar').show();
1434 -} else {
1435 - $('.chat-toolbar').hide();
1436 -}
1437 -
1438 -// Initialize on page load
1439 -document.addEventListener('DOMContentLoaded', function() {
1440 - checkInitialDocumentStatus();
1441 -});
1442 -
1443 -// Style all toolbar elements
1444 -const toolbarElements = [
1445 - '#mxchat-chatbot .toolbar-btn svg',
1446 - '#mxchat-chatbot .active-pdf-name',
1447 - '#mxchat-chatbot .active-word-name',
1448 - '#mxchat-chatbot .remove-pdf-btn svg',
1449 - '#mxchat-chatbot .remove-word-btn svg'
1450 -];
1451 -$(toolbarElements.join(', ')).css({
1452 - 'fill': toolbarIconColor,
1453 - 'color': toolbarIconColor
1454 -});
1455 -
1456 -
1457 -
1458 -// Ensure essential elements are defined
1459 -const emailForm = document.getElementById('email-collection-form');
1460 -const emailBlocker = document.getElementById('email-blocker');
1461 -const chatbotWrapper = document.getElementById('chat-container');
1462 -
1463 -if (emailForm && emailBlocker && chatbotWrapper) {
1464 - // Check if email exists for the current session
1465 - function checkSessionAndEmail() {
1466 - const sessionId = getChatSession();
1467 - //console.log("[DEBUG JS] checkSessionAndEmail -> sessionId:", sessionId);
1468 -
1469 - fetch(mxchatChat.ajax_url, {
1470 - method: 'POST',
1471 - headers: {
1472 - 'Content-Type': 'application/x-www-form-urlencoded',
1473 - },
1474 - body: new URLSearchParams({
1475 - action: 'mxchat_check_email_provided',
1476 - session_id: sessionId,
1477 - nonce: mxchatChat.nonce,
1478 - }),
1479 - })
1480 - .then((response) => response.json())
1481 - .then((data) => {
1482 - //console.log("[DEBUG JS] mxchat_check_email_provided response:", data);
1483 -
1484 - if (data.success) {
1485 - if (data.data.logged_in) {
1486 - //console.log("[DEBUG JS] User is logged in. Hiding email form.");
1487 - emailBlocker.style.display = 'none';
1488 - chatbotWrapper.style.display = 'flex';
1489 - } else if (data.data.email) {
1490 - //console.log("[DEBUG JS] Email found for session. Hiding email form.");
1491 - emailBlocker.style.display = 'none';
1492 - chatbotWrapper.style.display = 'flex';
1493 - } else {
1494 - //console.log("[DEBUG JS] No email provided. Showing email form.");
1495 - emailBlocker.style.display = 'flex';
1496 - chatbotWrapper.style.display = 'none';
1497 - }
1498 - } else {
1499 - //console.log("[DEBUG JS] Error or no data received. Showing email form.");
1500 - emailBlocker.style.display = 'flex';
1501 - chatbotWrapper.style.display = 'none';
1502 - }
1503 - })
1504 - .catch((error) => {
1505 - // console.error("[DEBUG JS] Fetch error -> forcing email form visible:", error);
1506 - emailBlocker.style.display = 'flex';
1507 - chatbotWrapper.style.display = 'none';
1508 - });
1509 -}
1510 -
1511 -
1512 -
1513 - // Handle email form submission
1514 - emailForm.addEventListener('submit', function (event) {
1515 - event.preventDefault();
1516 - const userEmail = document.getElementById('user-email').value;
1517 - const sessionId = getChatSession();
1518 -
1519 - if (userEmail) {
1520 - fetch(mxchatChat.ajax_url, {
1521 - method: 'POST',
1522 - headers: {
1523 - 'Content-Type': 'application/x-www-form-urlencoded',
1524 - },
1525 - body: new URLSearchParams({
1526 - action: 'mxchat_handle_save_email_and_response',
1527 - email: userEmail,
1528 - session_id: sessionId,
1529 - nonce: mxchatChat.nonce,
1530 - }),
1531 - })
1532 - .then((response) => response.json())
1533 - .then((data) => {
1534 - //console.log('Backend response:', data);
1535 - if (data.success) {
1536 - //console.log('Email saved successfully:', userEmail);
1537 - emailBlocker.style.display = 'none';
1538 - chatbotWrapper.style.display = 'flex';
1539 -
1540 - // Optionally handle bot response
1541 - if (data.message) {
1542 - appendMessage('bot', data.message);
1543 - scrollToBottom();
1544 - }
1545 - } else {
1546 - console.error('Error saving email:', data.message || 'Unknown error');
1547 - }
1548 - })
1549 - .catch((error) => {
1550 - console.error('AJAX error:', error);
1551 - });
1552 - }
1553 - });
1554 -
1555 - // Check session and email status on page load
1556 - checkSessionAndEmail();
1557 -} else {
1558 - console.error('Essential elements for email handling are missing.');
1559 -}
1560 -
1561 -
1562 -// Initialize when document is ready
1563 -$(document).ready(function() {
1564 - setFullHeight();
1565 - initializeChatVisibility();
1566 - loadChatHistory();
1567 -
1568 -});
1569 -
1570 -});
1571 -
1572 -// Event listener for copy button
1573 -document.addEventListener("click", (e) => {
1574 - if (e.target.classList.contains("mxchat-copy-button")) {
1575 - const copyButton = e.target;
1576 - const codeBlock = copyButton
1577 - .closest(".mxchat-code-block-container")
1578 - .querySelector(".mxchat-code-block code");
1579 -
1580 - if (codeBlock) {
1581 - // Preserve formatting using innerText
1582 - navigator.clipboard.writeText(codeBlock.innerText).then(() => {
1583 - copyButton.textContent = "Copied!";
1584 - copyButton.setAttribute("aria-label", "Copied to clipboard");
1585 -
1586 - setTimeout(() => {
1587 - copyButton.textContent = "Copy";
1588 - copyButton.setAttribute("aria-label", "Copy to clipboard");
1589 - }, 2000);
1590 - });
1591 - }
1592 - }
1593 -});
1594 -
1595 -
1596 -
1 +jQuery(document).ready(function($) {
2 +//console.log('mxchatChat object:', mxchatChat);
3 +//console.log('Link Target Toggle Value:', mxchatChat.link_target_toggle);
4 +// Add these variables at the top of your chat-script.js file
5 + const toolbarIconColor = mxchatChat.toolbar_icon_color || '#212121';
6 +
7 + // Initialize color settings
8 + var userMessageBgColor = mxchatChat.user_message_bg_color;
9 + var userMessageFontColor = mxchatChat.user_message_font_color;
10 + var botMessageBgColor = mxchatChat.bot_message_bg_color;
11 + var botMessageFontColor = mxchatChat.bot_message_font_color;
12 + // Add live agent message colors
13 + var liveAgentMessageBgColor = mxchatChat.live_agent_message_bg_color;
14 + var liveAgentMessageFontColor = mxchatChat.live_agent_message_font_color;
15 +
16 +
17 + var linkTarget = mxchatChat.link_target_toggle === 'on' ? '_blank' : '_self';
18 + let lastSeenMessageId = '';
19 + let notificationCheckInterval;
20 + let notificationBadge;
21 + // Initialize session ID
22 + var sessionId = getChatSession();
23 +
24 + let pollingInterval; // Variable to store the interval ID
25 + let processedMessageIds = new Set(); // Add this at the top with your other variables
26 +//console.log('Live Agent BG Color:', liveAgentMessageBgColor);
27 +//console.log('Live Agent Font Color:', liveAgentMessageFontColor);
28 + let activePdfFile = null;
29 + let activeWordFile = null;
30 +
31 +
32 +// Function to create and append notification badge
33 +// Function to create and append notification badge
34 +function createNotificationBadge() {
35 + console.log("Creating notification badge...");
36 + const chatButton = document.getElementById('floating-chatbot-button');
37 + console.log("Chat button found:", !!chatButton);
38 +
39 + if (!chatButton) return;
40 +
41 + // Remove any existing badge first
42 + const existingBadge = chatButton.querySelector('.chat-notification-badge');
43 + if (existingBadge) {
44 + console.log("Removing existing badge");
45 + existingBadge.remove();
46 + }
47 +
48 + notificationBadge = document.createElement('div');
49 + notificationBadge.className = 'chat-notification-badge';
50 + notificationBadge.style.cssText = `
51 + display: none;
52 + position: absolute;
53 + top: -5px;
54 + right: -5px;
55 + background-color: red;
56 + color: white;
57 + border-radius: 50%;
58 + padding: 4px 8px;
59 + font-size: 12px;
60 + font-weight: bold;
61 + z-index: 10001;
62 + `;
63 + chatButton.style.position = 'relative';
64 + chatButton.appendChild(notificationBadge);
65 +
66 + console.log("Notification badge created and appended:", {
67 + exists: !!notificationBadge,
68 + parent: notificationBadge?.parentNode?.id,
69 + display: notificationBadge?.style?.display
70 + });
71 +}
72 +
73 +// Function to check for new messages
74 +function checkForNewMessages() {
75 + const sessionId = getChatSession();
76 + const chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
77 +
78 + if (!chatPersistenceEnabled) return;
79 +
80 + $.ajax({
81 + url: mxchatChat.ajax_url,
82 + type: 'POST',
83 + data: {
84 + action: 'mxchat_check_new_messages',
85 + session_id: sessionId,
86 + last_seen_id: lastSeenMessageId,
87 + nonce: mxchatChat.nonce
88 + },
89 + success: function(response) {
90 + if (response.success && response.data.hasNewMessages) {
91 + showNotification();
92 + }
93 + }
94 + });
95 +}
96 +
97 +// Function to show notification
98 +function showNotification() {
99 + const badge = document.getElementById('chat-notification-badge');
100 + if (badge && $('#floating-chatbot').hasClass('hidden')) {
101 + badge.style.display = 'block';
102 + badge.textContent = '1';
103 + }
104 +}
105 +
106 +function hideNotification() {
107 + const badge = document.getElementById('chat-notification-badge');
108 + if (badge) {
109 + badge.style.display = 'none';
110 + }
111 +}
112 +// Function to start notification checking
113 +function startNotificationChecking() {
114 + const chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
115 + if (!chatPersistenceEnabled) return;
116 +
117 + createNotificationBadge();
118 + notificationCheckInterval = setInterval(checkForNewMessages, 30000); // Check every 30 seconds
119 +}
120 +
121 +// Function to stop notification checking
122 +function stopNotificationChecking() {
123 + if (notificationCheckInterval) {
124 + clearInterval(notificationCheckInterval);
125 + }
126 +}
127 +
128 +
129 +
130 +
131 +function getChatSession() {
132 + var sessionId = getCookie('mxchat_session_id');
133 + //console.log("Session ID retrieved from cookie: ", sessionId);
134 +
135 + if (!sessionId) {
136 + sessionId = generateSessionId();
137 + //console.log("Generated new session ID: ", sessionId);
138 + setChatSession(sessionId);
139 + }
140 +
141 + //console.log("Final session ID: ", sessionId);
142 + return sessionId;
143 +}
144 +
145 +function setChatSession(sessionId) {
146 + // Set the cookie with a 24-hour expiration (86400 seconds)
147 + document.cookie = "mxchat_session_id=" + sessionId + "; path=/; max-age=86400; SameSite=Lax";
148 +}
149 +
150 +// Get cookie value by name
151 +function getCookie(name) {
152 + let value = "; " + document.cookie;
153 + let parts = value.split("; " + name + "=");
154 + if (parts.length == 2) return parts.pop().split(";").shift();
155 +}
156 +
157 +// Generate a new session ID
158 +function generateSessionId() {
159 + return 'mxchat_chat_' + Math.random().toString(36).substr(2, 9);
160 +}
161 +
162 +// Function to send the message to the chatbot (backend)
163 +function sendMessageToChatbot(message) {
164 + var sessionId = getChatSession(); // Reuse the session ID logic
165 +
166 + // Hide the popular questions section
167 + $('#mxchat-popular-questions').hide();
168 +
169 + // Show thinking indicator (no need to append the user's message again)
170 + appendThinkingMessage();
171 + scrollToBottom();
172 +
173 + //console.log("Sending message to chatbot:", message); // Log the message
174 + //console.log("Session ID:", sessionId); // Log the session ID
175 +
176 + // Call the chatbot using the same call logic as sendMessage
177 + callMxChat(message, function(response) {
178 + // ** Ensure temporary thinking message is removed before adding new response **
179 + $('.temporary-message').remove();
180 +
181 + // Replace thinking indicator with actual response
182 + replaceLastMessage("bot", response);
183 + });
184 +}
185 +
186 +
187 +
188 +
189 +function sendMessage() {
190 + var message = $('#chat-input').val(); // Get value from textarea
191 + if (message) {
192 + appendMessage("user", message); // Append user's message
193 + $('#chat-input').val(''); // Clear the textarea
194 + $('#chat-input').css('height', 'auto'); // Reset height after clearing content
195 +
196 + // Hide the popular questions section
197 + $('#mxchat-popular-questions').hide();
198 +
199 + // Show typing indicator
200 + appendThinkingMessage();
201 + scrollToBottom();
202 +
203 + callMxChat(message, function(response) {
204 + // Replace typing indicator with actual response
205 + replaceLastMessage("bot", response);
206 + });
207 + }
208 +}
209 +
210 +
211 +
212 + // Function to append a thinking message with animation
213 + function appendThinkingMessage() {
214 + // Remove any existing thinking dots first
215 + $('.thinking-dots').remove();
216 +
217 + // Retrieve the bot message font color and background color
218 + var botMessageFontColor = mxchatChat.bot_message_font_color;
219 + var botMessageBgColor = mxchatChat.bot_message_bg_color;
220 +
221 +
222 + var thinkingHtml = '<div class="thinking-dots-container">' +
223 + '<div class="thinking-dots">' +
224 + '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
225 + '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
226 + '<span class="dot" style="background-color: ' + botMessageFontColor + ';"></span>' +
227 + '</div>' +
228 + '</div>';
229 +
230 + // Append the thinking dots to the chat container (or within the temporary message div)
231 + $("#chat-box").append('<div class="bot-message temporary-message" style="background-color: ' + botMessageBgColor + ';">' + thinkingHtml + '</div>');
232 + scrollToBottom();
233 + }
234 +
235 + // Trigger send button click when "Enter" key is pressed in the textarea
236 + $('#chat-input').keypress(function(e) {
237 + if (e.which == 13 && !e.shiftKey) { // Check if "Enter" is pressed without Shift
238 + e.preventDefault(); // Prevent default "Enter" behavior
239 + $('#send-button').click(); // Trigger send button click
240 + }
241 + });
242 +
243 + // Handle send button click
244 + $('#send-button').click(function() {
245 + sendMessage();
246 + });
247 +
248 + // Handle click on popular questions
249 + $('.mxchat-popular-question').on('click', function () {
250 + var question = $(this).text(); // Get the text of the clicked question
251 +
252 + // Append the question as if the user typed it
253 + appendMessage("user", question);
254 +
255 + // Send the question to the server (backend)
256 + sendMessageToChatbot(question);
257 + });
258 +
259 +
260 +// Add this new function to handle markdown headers
261 +function formatMarkdownHeaders(text) {
262 + // Handle h1 to h6 headers
263 + return text.replace(/^(#{1,6})\s(.+)$/gm, function(match, hashes, content) {
264 + const level = hashes.length;
265 + return `<h${level} class="chat-heading">${content}</h${level}>`;
266 + });
267 +}
268 +
269 +// Update the linkify function to handle URLs, markdown, and phone numbers
270 +function linkify(inputText) {
271 + if (!inputText) return '';
272 +
273 + // Process markdown headers
274 + let processedText = formatMarkdownHeaders(inputText);
275 +
276 + // Process markdown links
277 + const markdownLinkPattern = /\[([^\]]+)\]\((https?:\/\/[^\s]+)\)/g;
278 + processedText = processedText.replace(markdownLinkPattern, (match, text, url) => {
279 + const safeUrl = encodeURI(url);
280 + const safeText = sanitizeUserInput(text);
281 + return `<a href="${safeUrl}" target="${linkTarget}">${safeText}</a>`;
282 + });
283 +
284 + // Process phone numbers (tel:)
285 + const phonePattern = /\[([^\]]+)\]\((tel:[\d+]+)\)/g;
286 + processedText = processedText.replace(phonePattern, (match, text, phone) => {
287 + const safePhone = encodeURI(phone);
288 + const safeText = sanitizeUserInput(text);
289 + return `<a href="${safePhone}">${safeText}</a>`;
290 + });
291 +
292 + // Process standalone URLs
293 + const urlPattern = /(^|[^">])(https?:\/\/[^\s<]+)/gim;
294 + processedText = processedText.replace(urlPattern, (match, prefix, url) => {
295 + const safeUrl = encodeURI(url);
296 + return `${prefix}<a href="${safeUrl}" target="${linkTarget}">${url}</a>`;
297 + });
298 +
299 + // Process www. URLs
300 + const wwwPattern = /(^|[^">])(www\.[\S]+(\b|$))(?![^<]*<\/a>)/gim;
301 + processedText = processedText.replace(wwwPattern, (match, prefix, url) => {
302 + const safeUrl = encodeURI(`http://${url}`);
303 + return `${prefix}<a href="${safeUrl}" target="${linkTarget}">${url}</a>`;
304 + });
305 +
306 + return processedText;
307 +}
308 +
309 +function scrollElementToTop(element) {
310 + var chatBox = $('#chat-box');
311 + var elementTop = element.position().top + chatBox.scrollTop();
312 + chatBox.animate({ scrollTop: elementTop }, 500);
313 +}
314 +
315 +
316 +// Optimized scrollToBottom function for instant scrolling
317 +function scrollToBottom(instant = false) {
318 + var chatBox = $('#chat-box');
319 + if (instant) {
320 + // Instantly set the scroll position to the bottom
321 + chatBox.scrollTop(chatBox.prop("scrollHeight"));
322 + } else {
323 + // Use requestAnimationFrame for smoother scrolling if needed
324 + let start = null;
325 + const scrollHeight = chatBox.prop("scrollHeight");
326 + const initialScroll = chatBox.scrollTop();
327 + const distance = scrollHeight - initialScroll;
328 + const duration = 500; // Duration in ms
329 +
330 + function smoothScroll(timestamp) {
331 + if (!start) start = timestamp;
332 + const progress = timestamp - start;
333 + const currentScroll = initialScroll + (distance * (progress / duration));
334 + chatBox.scrollTop(currentScroll);
335 +
336 + if (progress < duration) {
337 + requestAnimationFrame(smoothScroll);
338 + } else {
339 + chatBox.scrollTop(scrollHeight); // Ensure it's exactly at the bottom
340 + }
341 + }
342 +
343 + requestAnimationFrame(smoothScroll);
344 + }
345 +}
346 +
347 +
348 + // Function to format text with **bold** inside double asterisks
349 + function formatBoldText(text) {
350 + return text.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>');
351 + }
352 +
353 + // Function to convert newline characters to HTML line breaks and handle paragraph spacing
354 +function convertNewlinesToBreaks(text) {
355 + // Split the text into paragraphs (marked by double newlines or multiple <br> tags)
356 + const paragraphs = text.split(/(?:\n\n|\<br\>\s*\<br\>)/g);
357 +
358 + // Wrap each paragraph in <p> tags
359 + return paragraphs
360 + .map(para => `<p>${para.trim()}</p>`)
361 + .join('');
362 +}
363 + // Copy to clipboard function
364 + // Function to copy text to clipboard
365 + function copyToClipboard(text) {
366 + var tempInput = $('<input>');
367 + $('body').append(tempInput);
368 + tempInput.val(text).select();
369 + document.execCommand('copy');
370 + tempInput.remove();
371 + }
372 +
373 +
374 +function updateChatModeIndicator(mode) {
375 + const indicator = document.getElementById('chat-mode-indicator');
376 + if (indicator) {
377 + // For Live Agent, keep as is; for AI mode, use the customized text
378 + if (mode === 'agent') {
379 + indicator.textContent = 'Live Agent';
380 + } else {
381 + // Get the custom AI agent text from a data attribute we'll add to the element
382 + const customAiText = indicator.getAttribute('data-ai-text') || 'AI Agent';
383 + indicator.textContent = customAiText;
384 + }
385 + }
386 + // Start or stop polling based on mode
387 + if (mode === 'agent') {
388 + startPolling();
389 + } else {
390 + stopPolling();
391 + }
392 +}
393 +
394 +function callMxChat(message, callback) {
395 + $.ajax({
396 + url: mxchatChat.ajax_url,
397 + type: 'POST',
398 + dataType: 'json',
399 + data: {
400 + action: 'mxchat_handle_chat_request',
401 + message: message,
402 + session_id: getChatSession(),
403 + nonce: mxchatChat.nonce
404 + },
405 + success: function(response) {
406 + // Log the full response for debugging
407 + console.log("API Response:", response);
408 +
409 + // First check if this is a successful response by looking for text, html, or message fields
410 + // This preserves compatibility with your server response format
411 + if (response.text !== undefined || response.html !== undefined || response.message !== undefined ||
412 + (response.success === true && response.data && response.data.status === 'waiting_for_agent')) {
413 +
414 + // Handle successful response - this is your original success handling code
415 +
416 + // Existing chat mode check
417 + if (response.chat_mode) {
418 + updateChatModeIndicator(response.chat_mode);
419 + }
420 + else if (response.fallbackResponse && response.fallbackResponse.chat_mode) {
421 + updateChatModeIndicator(response.fallbackResponse.chat_mode);
422 + }
423 +
424 + // Add PDF filename handling
425 + if (response.data && response.data.filename) {
426 + showActivePdf(response.data.filename);
427 + activePdfFile = response.data.filename;
428 + }
429 +
430 + // Add redirect check here
431 + if (response.redirect_url) {
432 + let responseText = response.text || '';
433 + if (responseText) {
434 + replaceLastMessage("bot", responseText);
435 + }
436 + setTimeout(() => {
437 + window.location.href = response.redirect_url;
438 + }, 1500);
439 + return;
440 + }
441 +
442 + // Check for live agent response
443 + if (response.success && response.data && response.data.status === 'waiting_for_agent') {
444 + updateChatModeIndicator('agent');
445 + return;
446 + }
447 +
448 + // Handle other responses
449 + let responseText = response.text || '';
450 + let responseHtml = response.html || '';
451 + let responseMessage = response.message || '';
452 +
453 + if (responseText === 'You are now chatting with the AI chatbot.') {
454 + updateChatModeIndicator('ai');
455 + }
456 +
457 + // Handle the message and show notification if chat is hidden
458 + if (responseText || responseHtml || responseMessage) {
459 + // Update the messages as before
460 + if (responseText && responseHtml) {
461 + replaceLastMessage("bot", responseText, responseHtml);
462 + } else if (responseText) {
463 + replaceLastMessage("bot", responseText);
464 + } else if (responseHtml) {
465 + replaceLastMessage("bot", "", responseHtml);
466 + } else if (responseMessage) {
467 + replaceLastMessage("bot", responseMessage);
468 + }
469 +
470 + // Check if chat is hidden and show notification
471 + if ($('#floating-chatbot').hasClass('hidden')) {
472 + const badge = $('#chat-notification-badge');
473 + if (badge.length) {
474 + badge.show();
475 + }
476 + }
477 + } else {
478 + console.error("Unexpected response format:", response);
479 + replaceLastMessage("bot", "I received an empty response. Please try again or contact support if this persists.");
480 + }
481 +
482 + if (response.message_id) {
483 + lastSeenMessageId = response.message_id;
484 + }
485 +
486 + return;
487 + }
488 +
489 + // If we got here, it's likely an error response
490 + // Now we can check for error conditions with our robust error handling
491 +
492 + let errorMessage = "";
493 + let errorCode = "";
494 +
495 + // Check various possible error locations in the response
496 + if (response.data && response.data.error_message) {
497 + errorMessage = response.data.error_message;
498 + errorCode = response.data.error_code || "";
499 + } else if (response.error_message) {
500 + errorMessage = response.error_message;
501 + errorCode = response.error_code || "";
502 + } else if (response.message) {
503 + errorMessage = response.message;
504 + } else if (typeof response.data === 'string') {
505 + errorMessage = response.data;
506 + } else if (!response.success) {
507 + // Explicit check for success: false without other error info
508 + errorMessage = "An error occurred. Please try again or contact support.";
509 + } else {
510 + // Fallback for any other unexpected response format
511 + errorMessage = "Unexpected response received. Please try again or contact support.";
512 + }
513 +
514 + // Log the error with code for debugging
515 + console.log("Response data:", response.data);
516 + console.error("API Error:", errorMessage, "Code:", errorCode);
517 +
518 + // Format user-friendly error message
519 + let displayMessage = errorMessage;
520 +
521 + // Customize message for admin users
522 + if (mxchatChat.is_admin) {
523 + // For admin users, show more technical details including error code
524 + displayMessage = errorMessage + (errorCode ? " (Error code: " + errorCode + ")" : "");
525 + }
526 +
527 + replaceLastMessage("bot", displayMessage);
528 + },
529 + error: function(xhr, status, error) {
530 + console.error("AJAX Error:", status, error);
531 + console.log("Response Text:", xhr.responseText);
532 +
533 + let errorMessage = "An unexpected error occurred.";
534 +
535 + // Try to parse the response if it's JSON
536 + try {
537 + const responseJson = JSON.parse(xhr.responseText);
538 + console.log("Parsed error response:", responseJson);
539 +
540 + if (responseJson.data && responseJson.data.error_message) {
541 + errorMessage = responseJson.data.error_message;
542 + } else if (responseJson.message) {
543 + errorMessage = responseJson.message;
544 + }
545 + } catch (e) {
546 + // Not JSON or parsing failed, use HTTP status based messages
547 + if (xhr.status === 0) {
548 + errorMessage = "Network error: Please check your internet connection.";
549 + } else if (xhr.status === 403) {
550 + errorMessage = "Access denied: Your session may have expired. Please refresh the page.";
551 + } else if (xhr.status === 404) {
552 + errorMessage = "API endpoint not found. Please contact support.";
553 + } else if (xhr.status === 429) {
554 + errorMessage = "Too many requests. Please try again in a moment.";
555 + } else if (xhr.status >= 500) {
556 + errorMessage = "Server error: The server encountered an issue. Please try again later.";
557 + }
558 + }
559 +
560 + replaceLastMessage("bot", errorMessage);
561 + }
562 + });
563 +}
564 +// Sanitize only user input
565 +function sanitizeUserInput(text) {
566 + const div = document.createElement('div');
567 + div.textContent = text;
568 + return div.innerHTML;
569 +}
570 +
571 +function appendMessage(sender, messageText = '', messageHtml = '', images = [], isTemporary = false) {
572 + try {
573 + // Determine styles based on sender type
574 + let messageClass, bgColor, fontColor;
575 +
576 + if (sender === "user") {
577 + messageClass = "user-message";
578 + bgColor = userMessageBgColor;
579 + fontColor = userMessageFontColor;
580 + // Only sanitize user input
581 + messageText = sanitizeUserInput(messageText);
582 + } else if (sender === "agent") {
583 + messageClass = "agent-message";
584 + bgColor = liveAgentMessageBgColor;
585 + fontColor = liveAgentMessageFontColor;
586 + } else {
587 + messageClass = "bot-message";
588 + bgColor = botMessageBgColor;
589 + fontColor = botMessageFontColor;
590 + }
591 +
592 + const messageDiv = $('<div>')
593 + .addClass(messageClass)
594 + .attr('dir', 'auto') // Add dir="auto" for automatic text direction
595 + .css({
596 + 'background': bgColor,
597 + 'color': fontColor,
598 + 'margin-bottom': '1em'
599 + });
600 +
601 + // Process the message content based on sender
602 + let fullMessage;
603 + if (sender === "user") {
604 + // For user messages, apply linkify after sanitization
605 + fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(messageText))));
606 + } else {
607 + // For bot/agent messages, preserve HTML
608 + fullMessage = messageText;
609 + }
610 +
611 + // Add images if provided
612 + if (images && images.length > 0) {
613 + fullMessage += '<div class="image-gallery" dir="auto">'; // Add dir="auto" to image gallery
614 + images.forEach(img => {
615 + // Ensure image URLs and titles are properly escaped
616 + const safeTitle = sanitizeUserInput(img.title);
617 + const safeUrl = encodeURI(img.image_url);
618 + const safeThumbnail = encodeURI(img.thumbnail_url);
619 +
620 + fullMessage += `
621 + <div style="margin-bottom: 10px;">
622 + <strong>${safeTitle}</strong><br>
623 + <a href="${safeUrl}" target="_blank">
624 + <img src="${safeThumbnail}" alt="${safeTitle}" style="max-width: 100px; height: auto; margin: 5px;" />
625 + </a>
626 + </div>`;
627 + });
628 + fullMessage += '</div>';
629 + }
630 +
631 + // Append HTML content if provided
632 + if (messageHtml && sender !== "user") {
633 + fullMessage += '<br><br>' + messageHtml;
634 + }
635 +
636 + messageDiv.html(fullMessage);
637 +
638 + if (isTemporary) {
639 + messageDiv.addClass('temporary-message');
640 + }
641 +
642 + messageDiv.hide().appendTo('#chat-box').fadeIn(300, function() {
643 + if (sender === "bot") {
644 + const lastUserMessage = $('#chat-box').find('.user-message').last();
645 + if (lastUserMessage.length) {
646 + scrollElementToTop(lastUserMessage);
647 + }
648 + }
649 + });
650 +
651 + if (messageText.id) {
652 + lastSeenMessageId = messageText.id;
653 + hideNotification();
654 + }
655 + } catch (error) {
656 + console.error("Error rendering message:", error);
657 + }
658 +}
659 +
660 +function replaceLastMessage(sender, responseText, responseHtml = '', images = []) {
661 + var messageClass = sender === "user" ? "user-message" : sender === "agent" ? "agent-message" : "bot-message";
662 + var lastMessageDiv = $('#chat-box').find('.bot-message.temporary-message, .agent-message.temporary-message').last();
663 +
664 + // Determine styles
665 + let bgColor, fontColor;
666 + if (sender === "user") {
667 + bgColor = userMessageBgColor;
668 + fontColor = userMessageFontColor;
669 + } else if (sender === "agent") {
670 + bgColor = liveAgentMessageBgColor;
671 + fontColor = liveAgentMessageFontColor;
672 + } else {
673 + bgColor = botMessageBgColor;
674 + fontColor = botMessageFontColor;
675 + }
676 +
677 + var fullMessage = linkify(formatBoldText(convertNewlinesToBreaks(formatCodeBlocks(responseText))));
678 + if (responseHtml) {
679 + fullMessage += '<br><br>' + responseHtml;
680 + }
681 +
682 + if (images.length > 0) {
683 + fullMessage += '<div class="image-gallery" dir="auto">'; // Add dir="auto" to image gallery
684 + images.forEach(img => {
685 + fullMessage += `
686 + <div style="margin-bottom: 10px;">
687 + <strong>${img.title}</strong><br>
688 + <a href="${img.image_url}" target="_blank">
689 + <img src="${img.thumbnail_url}" alt="${img.title}" style="max-width: 100px; height: auto; margin: 5px;" />
690 + </a>
691 + </div>`;
692 + });
693 + fullMessage += '</div>';
694 + }
695 +
696 + if (lastMessageDiv.length) {
697 + lastMessageDiv.fadeOut(200, function() {
698 + $(this)
699 + .html(fullMessage)
700 + .removeClass('bot-message user-message')
701 + .addClass(messageClass)
702 + .attr('dir', 'auto') // Add dir="auto" for automatic text direction
703 + .css({
704 + 'background-color': bgColor,
705 + 'color': fontColor,
706 + })
707 + .removeClass('temporary-message')
708 + .fadeIn(200, function() {
709 + if (sender === "bot" || sender === "agent") {
710 + const lastUserMessage = $('#chat-box').find('.user-message').last();
711 + if (lastUserMessage.length) {
712 + scrollElementToTop(lastUserMessage);
713 + }
714 + // Show notification if chat is hidden
715 + if ($('#floating-chatbot').hasClass('hidden')) {
716 + showNotification();
717 + }
718 + }
719 + });
720 + });
721 + } else {
722 + appendMessage(sender, responseText, responseHtml, images);
723 + }
724 +}
725 +
726 +
727 +
728 +function startPolling() {
729 + // Clear any existing interval first
730 + stopPolling();
731 + // Start new polling interval
732 + pollingInterval = setInterval(checkForAgentMessages, 5000);
733 + //console.log("Started agent message polling");
734 +}
735 +
736 +function stopPolling() {
737 + if (pollingInterval) {
738 + clearInterval(pollingInterval);
739 + pollingInterval = null;
740 + //console.log("Stopped agent message polling");
741 + }
742 +}
743 +
744 +
745 +// Update your checkForAgentMessages function
746 +function checkForAgentMessages() {
747 + const sessionId = getChatSession();
748 + $.ajax({
749 + url: mxchatChat.ajax_url,
750 + type: 'POST',
751 + dataType: 'json',
752 + data: {
753 + action: 'mxchat_fetch_new_messages',
754 + session_id: sessionId,
755 + last_seen_id: lastSeenMessageId,
756 + nonce: mxchatChat.nonce
757 + },
758 + success: function (response) {
759 + if (response.success && response.data?.new_messages) {
760 + let hasNewMessage = false;
761 +
762 + response.data.new_messages.forEach(function (message) {
763 + if (message.role === "agent" && !processedMessageIds.has(message.id)) {
764 + hasNewMessage = true;
765 + replaceLastMessage("agent", message.content);
766 + lastSeenMessageId = message.id;
767 + processedMessageIds.add(message.id);
768 + }
769 + });
770 +
771 + if (hasNewMessage && $('#floating-chatbot').hasClass('hidden')) {
772 + showNotification();
773 + }
774 +
775 + scrollToBottom(true);
776 + }
777 + },
778 + error: function (xhr, status, error) {
779 + console.error("Polling error:", xhr, status, error);
780 + }
781 + });
782 +}
783 +
784 +
785 +
786 +function loadChatHistory() {
787 + var sessionId = getChatSession();
788 + var chatPersistenceEnabled = mxchatChat.chat_persistence_toggle === 'on';
789 +
790 + if (chatPersistenceEnabled && sessionId) {
791 + $.ajax({
792 + url: mxchatChat.ajax_url,
793 + type: 'POST',
794 + dataType: 'json',
795 + data: {
796 + action: 'mxchat_fetch_conversation_history',
797 + session_id: sessionId
798 + },
799 + success: function(response) {
800 + if (response.success && response.data && Array.isArray(response.data.conversation)) {
801 +
802 +
803 + var $chatBox = $('#chat-box');
804 + var $fragment = $(document.createDocumentFragment());
805 + let highestMessageId = lastSeenMessageId;
806 +
807 + if (response.data.chat_mode) {
808 + updateChatModeIndicator(response.data.chat_mode);
809 + }
810 +
811 + $.each(response.data.conversation, function(index, message) {
812 + // Skip agent messages if persistence is off
813 + if (!chatPersistenceEnabled && message.role === 'agent') {
814 + return;
815 + }
816 +
817 + var messageClass, messageBgColor, messageFontColor;
818 +
819 + switch (message.role) {
820 + case 'user':
821 + messageClass = 'user-message';
822 + messageBgColor = userMessageBgColor;
823 + messageFontColor = userMessageFontColor;
824 + break;
825 + case 'agent':
826 + messageClass = 'agent-message';
827 + messageBgColor = liveAgentMessageBgColor;
828 + messageFontColor = liveAgentMessageFontColor;
829 + break;
830 + default:
831 + messageClass = 'bot-message';
832 + messageBgColor = botMessageBgColor;
833 + messageFontColor = botMessageFontColor;
834 + break;
835 + }
836 +
837 + var messageElement = $('<div>').addClass(messageClass)
838 + .css({
839 + 'background': messageBgColor,
840 + 'color': messageFontColor
841 + });
842 +
843 + var content = message.content;
844 + content = content.replace(/\\'/g, "'").replace(/\\"/g, '"');
845 + content = decodeHTMLEntities(content);
846 +
847 + if (content.includes("mxchat-product-card") || content.includes("mxchat-image-gallery")) {
848 + messageElement.html(content);
849 + } else {
850 + var formattedContent = linkify(
851 + formatBoldText(
852 + convertNewlinesToBreaks(formatCodeBlocks(content))
853 + )
854 + );
855 + messageElement.html(formattedContent);
856 + }
857 +
858 + $fragment.append(messageElement);
859 +
860 + // In loadChatHistory, change this part:
861 + if (message.id) {
862 + highestMessageId = Math.max(highestMessageId, message.id);
863 + processedMessageIds.add(message.id); // Add all message IDs to processed set
864 + }
865 + });
866 +
867 + $chatBox.append($fragment);
868 + scrollToBottom(true);
869 +
870 + if (response.data.conversation.length > 0) {
871 + $('#mxchat-popular-questions').hide();
872 + }
873 +
874 + // Update lastSeenMessageId after history loads
875 + lastSeenMessageId = highestMessageId;
876 +
877 + // Only update chat mode if persistence is enabled
878 + if (chatPersistenceEnabled && response.data.conversation.length > 0) {
879 + var lastMessage = response.data.conversation[response.data.conversation.length - 1];
880 + if (lastMessage.role === 'agent') {
881 + updateChatModeIndicator('agent');
882 + }
883 + }
884 + } else {
885 + console.warn("No conversation history found.");
886 + }
887 + },
888 + error: function(xhr, status, error) {
889 + console.error("Error loading chat history:", status, error);
890 + appendMessage("bot", "Unable to load chat history.");
891 + }
892 + });
893 + } else {
894 + console.warn("Chat persistence is disabled or no session ID found. Not loading history.");
895 + }
896 +}
897 +
898 +// Function to decode HTML entities
899 +function decodeHTMLEntities(text) {
900 + var textArea = document.createElement('textarea');
901 + textArea.innerHTML = text;
902 + return textArea.value;
903 +}
904 +
905 +
906 +// Update formatCodeBlocks function
907 +function formatCodeBlocks(text) {
908 + // First handle raw PHP tags
909 + text = text.replace(/(<\?php[\s\S]*?\?>)/g, (match) => {
910 + return `<pre><code class="language-php">${escapeHtml(match)}</code></pre>`;
911 + });
912 +
913 + // Then handle code blocks with backticks
914 + text = text.replace(/```php5?\n([\s\S]+?)```/gi, (match, code) => {
915 + return `<pre><code class="language-php">${escapeHtml(code)}</code></pre>`;
916 + });
917 +
918 + return text;
919 +}
920 +
921 +// Update escapeHtml function to preserve existing code blocks
922 +function escapeHtml(unsafe) {
923 + // First check if it's already a code block
924 + if (unsafe.includes('<pre><code') || unsafe.includes('</code></pre>')) {
925 + return unsafe;
926 + }
927 +
928 + return unsafe
929 + .replace(/&/g, "&amp;")
930 + .replace(/</g, "&lt;")
931 + .replace(/>/g, "&gt;")
932 + .replace(/"/g, "&quot;")
933 + .replace(/'/g, "&#039;");
934 +}
935 +// Utility function to escape HTML
936 +function escapeHtml(unsafe) {
937 + return unsafe
938 + .replace(/&/g, "&amp;")
939 + .replace(/</g, "&lt;")
940 + .replace(/>/g, "&gt;")
941 + .replace(/"/g, "&quot;")
942 + .replace(/'/g, "&#039;");
943 +}
944 +
945 +
946 +
947 +
948 +// Function to convert newlines, skipping preformatted text
949 +function convertNewlinesToBreaks(text) {
950 + // Split while preserving code blocks
951 + return text.split(/(<pre\b[^>]*>[\s\S]*?<\/pre>)/g).map(part => {
952 + if (part.startsWith('<pre')) return part;
953 + return part.replace(/(^|[^>])\n/g, '$1<br>');
954 + }).join('');
955 +}
956 +
957 +
958 +
959 +
960 + // Helper function to check if a string is an image HTML
961 + function isImageHtml(str) {
962 + return str.startsWith('<img') && str.endsWith('>');
963 + }
964 +
965 + // Function to remove thinking dots
966 + function removeThinkingDots() {
967 + $('.thinking-dots').closest('.temporary-message').remove();
968 + }
969 +
970 + function isMobile() {
971 + // This can be a simple check, or more sophisticated detection of mobile devices
972 + return window.innerWidth <= 768; // Example threshold for mobile devices
973 + }
974 +
975 + function disableScroll() {
976 + if (isMobile()) {
977 + $('body').css('overflow', 'hidden');
978 + }
979 + }
980 +
981 + function enableScroll() {
982 + if (isMobile()) {
983 + $('body').css('overflow', '');
984 + }
985 + }
986 +
987 +// Pre-chat dismissal check function (wrapped in a function for reuse)
988 + function checkPreChatDismissal() {
989 + $.ajax({
990 + url: mxchatChat.ajax_url,
991 + type: 'POST',
992 + data: {
993 + action: 'mxchat_check_pre_chat_message_status',
994 + _ajax_nonce: mxchatChat.nonce
995 + },
996 + success: function(response) {
997 + if (response.success && !response.data.dismissed) {
998 + $('#pre-chat-message').fadeIn(250);
999 + } else {
1000 + $('#pre-chat-message').hide();
1001 + }
1002 + },
1003 + error: function() {
1004 + console.error('Failed to check pre-chat message dismissal status.');
1005 + }
1006 + });
1007 + }
1008 +
1009 + // Function to show the chatbot widget
1010 +function showChatWidget() {
1011 + // First ensure display is set
1012 + $('#floating-chatbot-button').css('display', 'flex');
1013 + // Then handle the fade
1014 + $('#floating-chatbot-button').fadeTo(500, 1);
1015 + // Force visibility
1016 + $('#floating-chatbot-button').removeClass('hidden');
1017 + //console.log('Showing widget');
1018 +}
1019 +
1020 +// Function to hide the chatbot widget
1021 +function hideChatWidget() {
1022 + $('#floating-chatbot-button').css('display', 'none');
1023 + $('#floating-chatbot-button').addClass('hidden');
1024 + //console.log('Hiding widget');
1025 +}
1026 +
1027 +function initializeChatVisibility() {
1028 + //console.log('Initializing chat visibility');
1029 + const complianzEnabled = mxchatChat.complianz_toggle === 'on' ||
1030 + mxchatChat.complianz_toggle === '1' ||
1031 + mxchatChat.complianz_toggle === 1;
1032 +
1033 + if (complianzEnabled && typeof cmplz_has_consent === "function" && typeof complianz !== 'undefined') {
1034 + // Initial check
1035 + checkConsentAndShowChat();
1036 +
1037 + // Listen for consent changes
1038 + $(document).on('cmplz_status_change', function(event) {
1039 + //console.log('Status change detected');
1040 + checkConsentAndShowChat();
1041 + });
1042 + } else {
1043 + // If Complianz is not enabled, always show
1044 + $('#floating-chatbot-button')
1045 + .css('display', 'flex')
1046 + .removeClass('hidden no-consent')
1047 + .fadeTo(500, 1);
1048 +
1049 + // Also check pre-chat message when Complianz is not enabled
1050 + checkPreChatDismissal();
1051 + }
1052 +}
1053 +
1054 +
1055 +
1056 +function checkConsentAndShowChat() {
1057 + var consentStatus = cmplz_has_consent('marketing');
1058 + var consentType = complianz.consenttype;
1059 +
1060 + //console.log('Checking consent:', {status: consentStatus,type: consentType});
1061 +
1062 + let $widget = $('#floating-chatbot-button');
1063 + let $chatbot = $('#floating-chatbot');
1064 + let $preChat = $('#pre-chat-message');
1065 +
1066 + if (consentStatus === true) {
1067 + //console.log('Consent granted - showing widget');
1068 + $widget
1069 + .removeClass('no-consent')
1070 + .css('display', 'flex')
1071 + .removeClass('hidden')
1072 + .fadeTo(500, 1);
1073 + $chatbot.removeClass('no-consent');
1074 +
1075 + // Show pre-chat message if not dismissed
1076 + checkPreChatDismissal();
1077 + } else {
1078 + //console.log('No consent - hiding widget');
1079 + $widget
1080 + .addClass('no-consent')
1081 + .fadeTo(500, 0, function() {
1082 + $(this)
1083 + .css('display', 'none')
1084 + .addClass('hidden');
1085 + });
1086 + $chatbot.addClass('no-consent');
1087 +
1088 + // Hide pre-chat message when no consent
1089 + $preChat.hide();
1090 + }
1091 +}
1092 +
1093 + // Function to dismiss pre-chat message for 24 hours
1094 + function handlePreChatDismissal() {
1095 + $('#pre-chat-message').fadeOut(200);
1096 + $.ajax({
1097 + url: mxchatChat.ajax_url,
1098 + type: 'POST',
1099 + data: {
1100 + action: 'mxchat_dismiss_pre_chat_message',
1101 + _ajax_nonce: mxchatChat.nonce
1102 + },
1103 + success: function() {
1104 + $('#pre-chat-message').hide();
1105 + },
1106 + error: function() {
1107 + console.error('Failed to dismiss pre-chat message.');
1108 + }
1109 + });
1110 + }
1111 +
1112 + // Handle pre-chat message dismissal on button click
1113 + $(document).on('click', '.close-pre-chat-message', function(e) {
1114 + e.stopPropagation();
1115 + handlePreChatDismissal();
1116 + });
1117 +
1118 + // Toggle chatbot visibility on floating button click
1119 + $(document).on('click', '#floating-chatbot-button', function() {
1120 + var chatbot = $('#floating-chatbot');
1121 + if (chatbot.hasClass('hidden')) {
1122 + chatbot.removeClass('hidden').addClass('visible');
1123 + $(this).addClass('hidden');
1124 + $('#chat-notification-badge').hide(); // Hide notification when opening chat
1125 + disableScroll();
1126 + $('#pre-chat-message').fadeOut(250);
1127 + } else {
1128 + chatbot.removeClass('visible').addClass('hidden');
1129 + $(this).removeClass('hidden');
1130 + enableScroll();
1131 + checkPreChatDismissal();
1132 + }
1133 + });
1134 +
1135 + $(document).on('click', '#exit-chat-button', function() {
1136 + $('#floating-chatbot').addClass('hidden').removeClass('visible');
1137 + $('#floating-chatbot-button').removeClass('hidden');
1138 + enableScroll();
1139 + });
1140 +
1141 + // Close pre-chat message on click
1142 + $(document).on('click', '.close-pre-chat-message', function(e) {
1143 + e.stopPropagation(); // Prevent triggering the parent .pre-chat-message click
1144 + $('#pre-chat-message').fadeOut(200, function() {
1145 + $(this).remove();
1146 + });
1147 + });
1148 +
1149 + // Open chatbot when pre-chat message is clicked
1150 + $(document).on('click', '#pre-chat-message', function() {
1151 + var chatbot = $('#floating-chatbot');
1152 + if (chatbot.hasClass('hidden')) {
1153 + chatbot.removeClass('hidden').addClass('visible');
1154 + $('#floating-chatbot-button').addClass('hidden');
1155 + $('#pre-chat-message').fadeOut(250); // Hide pre-chat message
1156 + disableScroll(); // Disable scroll when chatbot opens
1157 + }
1158 + });
1159 +
1160 + // If the chatbot is initially hidden, ensure the button is visible
1161 + if ($('#floating-chatbot').hasClass('hidden')) {
1162 + $('#floating-chatbot-button').removeClass('hidden');
1163 + }
1164 +
1165 + function setFullHeight() {
1166 + var vh = $(window).innerHeight() * 0.01;
1167 + $(':root').css('--vh', vh + 'px');
1168 + }
1169 +
1170 + // Set the height when the page loads
1171 +
1172 +
1173 + // Set the height on resize and orientation change events
1174 + $(window).on('resize orientationchange', function() {
1175 + setFullHeight();
1176 + });
1177 +
1178 +
1179 + // Now handle the close button to dismiss the pre-chat message for 24 hours
1180 + var closeButton = document.querySelector('.close-pre-chat-message');
1181 + if (closeButton) {
1182 + closeButton.addEventListener('click', function() {
1183 + $('#pre-chat-message').fadeOut(200); // Hide the message
1184 +
1185 + // Send an AJAX request to set the transient flag for 24 hours
1186 + $.ajax({
1187 + url: mxchatChat.ajax_url,
1188 + type: 'POST',
1189 + data: {
1190 + action: 'mxchat_dismiss_pre_chat_message',
1191 + _ajax_nonce: mxchatChat.nonce
1192 + },
1193 + success: function() {
1194 + //console.log('Pre-chat message dismissed for 24 hours.');
1195 +
1196 + // Ensure the message is hidden after dismissal
1197 + $('#pre-chat-message').hide();
1198 + },
1199 + error: function() {
1200 + //console.error('Failed to dismiss pre-chat message.');
1201 + }
1202 + });
1203 + });
1204 + }
1205 +
1206 +
1207 +
1208 +
1209 +// Event listener for Add to Cart button
1210 +$(document).on('click', '.mxchat-add-to-cart-button', function() {
1211 + var productId = $(this).data('product-id');
1212 + // Add a special prefix to indicate this is from button
1213 + appendMessage("user", "add to cart");
1214 + sendMessageToChatbot("!addtocart"); // Special command to indicate button click
1215 +});
1216 +
1217 +
1218 +if (document.getElementById('pdf-upload-btn')) {
1219 + document.getElementById('pdf-upload-btn').addEventListener('click', function() {
1220 + document.getElementById('pdf-upload').click();
1221 + });
1222 +}
1223 +
1224 +if (document.getElementById('word-upload-btn')) {
1225 + document.getElementById('word-upload-btn').addEventListener('click', function() {
1226 + document.getElementById('word-upload').click();
1227 + });
1228 +}
1229 +
1230 +function addSafeEventListener(elementId, eventType, handler) {
1231 + const element = document.getElementById(elementId);
1232 + if (element) {
1233 + element.addEventListener(eventType, handler);
1234 + }
1235 +}
1236 +
1237 +
1238 +// PDF file input change handler
1239 +addSafeEventListener('pdf-upload', 'change', async function(e) {
1240 + const file = e.target.files[0];
1241 +
1242 + if (!file || file.type !== 'application/pdf') {
1243 + alert('Please select a valid PDF file.');
1244 + return;
1245 + }
1246 +
1247 + if (!sessionId) {
1248 + console.error('No session ID found');
1249 + alert('Error: No session ID found');
1250 + return;
1251 + }
1252 +
1253 + if (!mxchatChat || !mxchatChat.ajax_url || !mxchatChat.nonce) {
1254 + console.error('mxchatChat not properly configured:', mxchatChat);
1255 + alert('Error: Ajax configuration missing');
1256 + return;
1257 + }
1258 +
1259 + // Disable buttons and show loading state
1260 + const uploadBtn = document.getElementById('pdf-upload-btn');
1261 + const sendBtn = document.getElementById('send-button');
1262 + const originalBtnContent = uploadBtn.innerHTML;
1263 +
1264 + try {
1265 + const formData = new FormData();
1266 + formData.append('action', 'mxchat_upload_pdf');
1267 + formData.append('pdf_file', file);
1268 + formData.append('session_id', sessionId);
1269 + formData.append('nonce', mxchatChat.nonce);
1270 +
1271 + uploadBtn.disabled = true;
1272 + sendBtn.disabled = true;
1273 + uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
1274 + <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
1275 + </svg>`;
1276 +
1277 + const response = await fetch(mxchatChat.ajax_url, {
1278 + method: 'POST',
1279 + body: formData
1280 + });
1281 +
1282 + const data = await response.json();
1283 +
1284 + if (data.success) {
1285 + // Hide popular questions if they exist
1286 + const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
1287 + if (popularQuestionsContainer) {
1288 + popularQuestionsContainer.style.display = 'none';
1289 + }
1290 +
1291 + // Show the active PDF name
1292 + showActivePdf(data.data.filename);
1293 +
1294 + appendMessage('bot', data.data.message);
1295 + scrollToBottom();
1296 + activePdfFile = data.data.filename;
1297 + } else {
1298 + console.error('Upload failed:', data.data);
1299 + alert('Failed to upload PDF. Please try again.');
1300 + }
1301 + } catch (error) {
1302 + console.error('Upload error:', error);
1303 + alert('Error uploading file. Please try again.');
1304 + } finally {
1305 + uploadBtn.disabled = false;
1306 + sendBtn.disabled = false;
1307 + uploadBtn.innerHTML = originalBtnContent;
1308 + this.value = ''; // Reset file input
1309 + }
1310 +});
1311 +
1312 +// Word file input change handler
1313 +addSafeEventListener('word-upload', 'change', async function(e) {
1314 + const file = e.target.files[0];
1315 +
1316 + if (!file || file.type !== 'application/vnd.openxmlformats-officedocument.wordprocessingml.document') {
1317 + alert('Please select a valid Word document (.docx).');
1318 + return;
1319 + }
1320 +
1321 + if (!sessionId) {
1322 + console.error('No session ID found');
1323 + alert('Error: No session ID found');
1324 + return;
1325 + }
1326 +
1327 + // Disable buttons and show loading state
1328 + const uploadBtn = document.getElementById('word-upload-btn');
1329 + const sendBtn = document.getElementById('send-button');
1330 + const originalBtnContent = uploadBtn.innerHTML;
1331 +
1332 + try {
1333 + const formData = new FormData();
1334 + formData.append('action', 'mxchat_upload_word');
1335 + formData.append('word_file', file);
1336 + formData.append('session_id', sessionId);
1337 + formData.append('nonce', mxchatChat.nonce);
1338 +
1339 + uploadBtn.disabled = true;
1340 + sendBtn.disabled = true;
1341 + uploadBtn.innerHTML = `<svg class="spinner" viewBox="0 0 50 50">
1342 + <circle cx="25" cy="25" r="20" fill="none" stroke-width="5"></circle>
1343 + </svg>`;
1344 +
1345 + const response = await fetch(mxchatChat.ajax_url, {
1346 + method: 'POST',
1347 + body: formData
1348 + });
1349 +
1350 + const data = await response.json();
1351 +
1352 + if (data.success) {
1353 + // Hide popular questions if they exist
1354 + const popularQuestionsContainer = document.getElementById('mxchat-popular-questions');
1355 + if (popularQuestionsContainer) {
1356 + popularQuestionsContainer.style.display = 'none';
1357 + }
1358 +
1359 + // Show the active Word document name
1360 + showActiveWord(data.data.filename);
1361 +
1362 + appendMessage('bot', data.data.message);
1363 + scrollToBottom();
1364 + activeWordFile = data.data.filename;
1365 + } else {
1366 + console.error('Upload failed:', data.data);
1367 + alert('Failed to upload Word document. Please try again.');
1368 + }
1369 + } catch (error) {
1370 + console.error('Upload error:', error);
1371 + alert('Error uploading file. Please try again.');
1372 + } finally {
1373 + uploadBtn.disabled = false;
1374 + sendBtn.disabled = false;
1375 + uploadBtn.innerHTML = originalBtnContent;
1376 + this.value = ''; // Reset file input
1377 + }
1378 +});
1379 +
1380 +// Function to show active PDF name in toolbar
1381 +function showActivePdf(filename) {
1382 + const container = document.getElementById('active-pdf-container');
1383 + const nameElement = document.getElementById('active-pdf-name');
1384 +
1385 + if (!container || !nameElement) {
1386 + console.error('PDF container elements not found');
1387 + return;
1388 + }
1389 +
1390 + nameElement.textContent = filename;
1391 + container.style.display = 'flex';
1392 +}
1393 +
1394 +// Function to show active Word document name in toolbar
1395 +function showActiveWord(filename) {
1396 + const container = document.getElementById('active-word-container');
1397 + const nameElement = document.getElementById('active-word-name');
1398 +
1399 + if (!container || !nameElement) {
1400 + console.error('Word document container elements not found');
1401 + return;
1402 + }
1403 +
1404 + nameElement.textContent = filename;
1405 + container.style.display = 'flex';
1406 +}
1407 +
1408 +// Function to remove active PDF
1409 +function removeActivePdf() {
1410 + const container = document.getElementById('active-pdf-container');
1411 + const nameElement = document.getElementById('active-pdf-name');
1412 +
1413 + if (!container || !nameElement || !activePdfFile) return;
1414 +
1415 + fetch(mxchatChat.ajax_url, {
1416 + method: 'POST',
1417 + headers: {
1418 + 'Content-Type': 'application/x-www-form-urlencoded',
1419 + },
1420 + body: new URLSearchParams({
1421 + 'action': 'mxchat_remove_pdf',
1422 + 'session_id': sessionId,
1423 + 'nonce': mxchatChat.nonce
1424 + })
1425 + })
1426 + .then(response => response.json())
1427 + .then(data => {
1428 + if (data.success) {
1429 + container.style.display = 'none';
1430 + nameElement.textContent = '';
1431 + activePdfFile = null;
1432 + appendMessage('bot', 'PDF removed.');
1433 + }
1434 + })
1435 + .catch(error => {
1436 + console.error('Error removing PDF:', error);
1437 + });
1438 +}
1439 +
1440 +// Function to remove active Word document
1441 +function removeActiveWord() {
1442 + const container = document.getElementById('active-word-container');
1443 + const nameElement = document.getElementById('active-word-name');
1444 +
1445 + if (!container || !nameElement || !activeWordFile) return;
1446 +
1447 + fetch(mxchatChat.ajax_url, {
1448 + method: 'POST',
1449 + headers: {
1450 + 'Content-Type': 'application/x-www-form-urlencoded',
1451 + },
1452 + body: new URLSearchParams({
1453 + 'action': 'mxchat_remove_word',
1454 + 'session_id': sessionId,
1455 + 'nonce': mxchatChat.nonce
1456 + })
1457 + })
1458 + .then(response => response.json())
1459 + .then(data => {
1460 + if (data.success) {
1461 + container.style.display = 'none';
1462 + nameElement.textContent = '';
1463 + activeWordFile = null;
1464 + appendMessage('bot', 'Word document removed.');
1465 + }
1466 + })
1467 + .catch(error => {
1468 + console.error('Error removing Word document:', error);
1469 + });
1470 +}
1471 +
1472 +// Add remove button click handlers
1473 +document.getElementById('remove-pdf-btn')?.addEventListener('click', function(e) {
1474 + e.preventDefault();
1475 + e.stopPropagation();
1476 + removeActivePdf();
1477 +});
1478 +
1479 +document.getElementById('remove-word-btn')?.addEventListener('click', function(e) {
1480 + e.preventDefault();
1481 + e.stopPropagation();
1482 + removeActiveWord();
1483 +});
1484 +
1485 +// Check initial document status
1486 +function checkInitialDocumentStatus() {
1487 + if (!sessionId) return;
1488 +
1489 + // Check PDF status
1490 + fetch(mxchatChat.ajax_url, {
1491 + method: 'POST',
1492 + headers: {
1493 + 'Content-Type': 'application/x-www-form-urlencoded',
1494 + },
1495 + body: new URLSearchParams({
1496 + 'action': 'mxchat_check_pdf_status',
1497 + 'session_id': sessionId,
1498 + 'nonce': mxchatChat.nonce
1499 + })
1500 + })
1501 + .then(response => response.json())
1502 + .then(data => {
1503 + if (data.success && data.data.filename) {
1504 + showActivePdf(data.data.filename);
1505 + activePdfFile = data.data.filename;
1506 + }
1507 + })
1508 + .catch(error => {
1509 + console.error('Error checking PDF status:', error);
1510 + });
1511 +
1512 + // Check Word document status
1513 + fetch(mxchatChat.ajax_url, {
1514 + method: 'POST',
1515 + headers: {
1516 + 'Content-Type': 'application/x-www-form-urlencoded',
1517 + },
1518 + body: new URLSearchParams({
1519 + 'action': 'mxchat_check_word_status',
1520 + 'session_id': sessionId,
1521 + 'nonce': mxchatChat.nonce
1522 + })
1523 + })
1524 + .then(response => response.json())
1525 + .then(data => {
1526 + if (data.success && data.data.filename) {
1527 + showActiveWord(data.data.filename);
1528 + activeWordFile = data.data.filename;
1529 + }
1530 + })
1531 + .catch(error => {
1532 + console.error('Error checking Word document status:', error);
1533 + });
1534 +}
1535 +
1536 +// Apply toolbar settings
1537 +if (mxchatChat.chat_toolbar_toggle === 'on') {
1538 + $('.chat-toolbar').show();
1539 +} else {
1540 + $('.chat-toolbar').hide();
1541 +}
1542 +
1543 +// Initialize on page load
1544 +document.addEventListener('DOMContentLoaded', function() {
1545 + checkInitialDocumentStatus();
1546 +});
1547 +
1548 +const toolbarElements = [
1549 + '#mxchat-chatbot .toolbar-btn svg',
1550 + '#mxchat-chatbot .active-pdf-name',
1551 + '#mxchat-chatbot .active-word-name',
1552 + '#mxchat-chatbot .remove-pdf-btn svg',
1553 + '#mxchat-chatbot .remove-word-btn svg',
1554 + '#mxchat-chatbot .toolbar-perplexity svg'
1555 +];
1556 +
1557 +toolbarElements.forEach(selector => {
1558 + $(selector).css({
1559 + 'fill': toolbarIconColor,
1560 + 'stroke': toolbarIconColor,
1561 + 'color': toolbarIconColor
1562 + });
1563 +});
1564 +
1565 +
1566 +// Ensure essential elements are defined
1567 +const emailForm = document.getElementById('email-collection-form');
1568 +const emailBlocker = document.getElementById('email-blocker');
1569 +const chatbotWrapper = document.getElementById('chat-container');
1570 +
1571 +// Only show error if email collection is enabled but elements are missing
1572 +if (emailForm && emailBlocker && chatbotWrapper) {
1573 + // Check if email exists for the current session
1574 + function checkSessionAndEmail() {
1575 + const sessionId = getChatSession();
1576 + //console.log("[DEBUG JS] checkSessionAndEmail -> sessionId:", sessionId);
1577 +
1578 + fetch(mxchatChat.ajax_url, {
1579 + method: 'POST',
1580 + headers: {
1581 + 'Content-Type': 'application/x-www-form-urlencoded',
1582 + },
1583 + body: new URLSearchParams({
1584 + action: 'mxchat_check_email_provided',
1585 + session_id: sessionId,
1586 + nonce: mxchatChat.nonce,
1587 + }),
1588 + })
1589 + .then((response) => response.json())
1590 + .then((data) => {
1591 + //console.log("[DEBUG JS] mxchat_check_email_provided response:", data);
1592 +
1593 + if (data.success) {
1594 + if (data.data.logged_in) {
1595 + //console.log("[DEBUG JS] User is logged in. Hiding email form.");
1596 + emailBlocker.style.display = 'none';
1597 + chatbotWrapper.style.display = 'flex';
1598 + } else if (data.data.email) {
1599 + //console.log("[DEBUG JS] Email found for session. Hiding email form.");
1600 + emailBlocker.style.display = 'none';
1601 + chatbotWrapper.style.display = 'flex';
1602 + } else {
1603 + //console.log("[DEBUG JS] No email provided. Showing email form.");
1604 + emailBlocker.style.display = 'flex';
1605 + chatbotWrapper.style.display = 'none';
1606 + }
1607 + } else {
1608 + //console.log("[DEBUG JS] Error or no data received. Showing email form.");
1609 + emailBlocker.style.display = 'flex';
1610 + chatbotWrapper.style.display = 'none';
1611 + }
1612 + })
1613 + .catch((error) => {
1614 + // console.error("[DEBUG JS] Fetch error -> forcing email form visible:", error);
1615 + emailBlocker.style.display = 'flex';
1616 + chatbotWrapper.style.display = 'none';
1617 + });
1618 + }
1619 +
1620 + // Handle email form submission
1621 + emailForm.addEventListener('submit', function (event) {
1622 + event.preventDefault();
1623 + const userEmail = document.getElementById('user-email').value;
1624 + const sessionId = getChatSession();
1625 +
1626 + if (userEmail) {
1627 + fetch(mxchatChat.ajax_url, {
1628 + method: 'POST',
1629 + headers: {
1630 + 'Content-Type': 'application/x-www-form-urlencoded',
1631 + },
1632 + body: new URLSearchParams({
1633 + action: 'mxchat_handle_save_email_and_response',
1634 + email: userEmail,
1635 + session_id: sessionId,
1636 + nonce: mxchatChat.nonce,
1637 + }),
1638 + })
1639 + .then((response) => response.json())
1640 + .then((data) => {
1641 + //console.log('Backend response:', data);
1642 + if (data.success) {
1643 + //console.log('Email saved successfully:', userEmail);
1644 + emailBlocker.style.display = 'none';
1645 + chatbotWrapper.style.display = 'flex';
1646 +
1647 + // Optionally handle bot response
1648 + if (data.message) {
1649 + appendMessage('bot', data.message);
1650 + scrollToBottom();
1651 + }
1652 + } else {
1653 + console.error('Error saving email:', data.message || 'Unknown error');
1654 + }
1655 + })
1656 + .catch((error) => {
1657 + console.error('AJAX error:', error);
1658 + });
1659 + }
1660 + });
1661 +
1662 + // Check session and email status on page load
1663 + checkSessionAndEmail();
1664 +} else if (mxchatChat.email_collection_enabled) {
1665 + // Only show error if email collection is enabled but elements are missing
1666 + console.error('Essential elements for email handling are missing.');
1667 +}
1668 +
1669 +
1670 +// Initialize when document is ready
1671 +$(document).ready(function() {
1672 + setFullHeight();
1673 + initializeChatVisibility();
1674 + loadChatHistory();
1675 +
1676 +});
1677 +
1678 +});
1679 +
1680 +// Event listener for copy button
1681 +document.addEventListener("click", (e) => {
1682 + if (e.target.classList.contains("mxchat-copy-button")) {
1683 + const copyButton = e.target;
1684 + const codeBlock = copyButton
1685 + .closest(".mxchat-code-block-container")
1686 + .querySelector(".mxchat-code-block code");
1687 +
1688 + if (codeBlock) {
1689 + // Preserve formatting using innerText
1690 + navigator.clipboard.writeText(codeBlock.innerText).then(() => {
1691 + copyButton.textContent = "Copied!";
1692 + copyButton.setAttribute("aria-label", "Copied to clipboard");
1693 +
1694 + setTimeout(() => {
1695 + copyButton.textContent = "Copy";
1696 + copyButton.setAttribute("aria-label", "Copy to clipboard");
1697 + }, 2000);
1698 + });
1699 + }
1700 + }
1701 +});
1702 +
1703 +
1704 +