jQuery(document).ready(function($) { // Check if floating chatbot button exists var floatingButton = $('#floating-chatbot-button'); // Retrieve color settings from PHP var userMessageBgColor = mxchatChat.user_message_bg_color; var userMessageFontColor = mxchatChat.user_message_font_color; var botMessageBgColor = mxchatChat.bot_message_bg_color; var botMessageFontColor = mxchatChat.bot_message_font_color; // Retrieve link target setting (passed from PHP through wp_localize_script) var linkTarget = mxchatChat.link_target === 'on' ? '_blank' : '_self'; // Explicitly check for 'on' or 'off' var rateLimitMessage = mxchatChat.rate_limit_message || "Rate limit exceeded. Please try again later."; // Function to handle sending a message function sendMessage() { var message = $('#chat-input').val(); if (message) { appendMessage("user", message); $('#chat-input').val(''); // Show typing indicator appendThinkingMessage(); // Use this instead of appendMessage for the typing indicator scrollToBottom(); // Add this line callMxChat(message, function(response) { // Replace typing indicator with actual response replaceLastMessage("bot", response); }); } } // Function to append a thinking message with animation function appendThinkingMessage() { // Remove any existing thinking dots first $('.thinking-dots').remove(); // Retrieve the bot message font color and background color var botMessageFontColor = mxchatChat.bot_message_font_color; var botMessageBgColor = mxchatChat.bot_message_bg_color; var thinkingHtml = '
' + '
' + '' + '' + '' + '
' + '
'; // Append the thinking dots to the chat container (or within the temporary message div) $("#chat-box").append('
' + thinkingHtml + '
'); scrollToBottom(); } // Trigger send button click when "Enter" key is pressed in the input field $('#chat-input').keypress(function(e) { if (e.which == 13) { e.preventDefault(); $('#send-button').click(); } }); // Handle send button click $('#send-button').click(function() { sendMessage(); }); // Use the linkTarget in your linkify function function linkify(inputText) { // Convert Markdown-style links to HTML links first var markdownLinkPattern = /\[([^\]]+)\]\(([^)]+)\)/g; var replacedText = inputText.replace(markdownLinkPattern, '$1'); // URLs starting with http://, https://, or ftp://, but not already inside an tag var urlPattern = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[--A-Z0-9+&@#\/%=~_|])(?![^<]*<\/a>)/gim; replacedText = replacedText.replace(urlPattern, '$1'); // URLs starting with "www." not already inside an tag var wwwPattern = /(^|[^\/])(www\.[\S]+(\b|$))(?![^<]*<\/a>)/gim; replacedText = replacedText.replace(wwwPattern, '$1$2'); return replacedText; } // Updated appendMessage function function appendMessage(sender, message, isTemporary = false) { var messageClass = sender === "user" ? "user-message" : "bot-message"; var bgColor = sender === "user" ? mxchatChat.user_message_bg_color : mxchatChat.bot_message_bg_color; var fontColor = sender === "user" ? mxchatChat.user_message_font_color : mxchatChat.bot_message_font_color; var messageDiv = $('
').addClass(messageClass).css({ 'background': bgColor, 'color': fontColor }); if (sender === "assistant") { var codeRegex = /```([^]+)```/; // Regex to detect code blocks var match = codeRegex.exec(message); message = linkify(message); if (match) { var beforeCode = message.substring(0, match.index); var codePart = match[1]; // Extracted code content var afterCode = message.substring(match.index + match[0].length); if (beforeCode.trim()) { messageDiv.append($('').html(convertNewlinesToBreaks(beforeCode))); } var codeBlock = $('
').append($('').text(codePart));
                var copyButton = $('