PageSourceSearch

https://www.unmundoagranel.es/modules/gptchatbox//views/js/front_v23.js

js unmundoagranel.es collected 2026-09-28 06:07:46 UTC 6,513 bytes, 175 lines download raw bytes

1/**
2 *
3 * DISCLAIMER
4 *
5 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
6 * versions in the future. If you wish to customize PrestaShop for your
7 * needs please refer to http://www.prestashop.com for more information.
8 * We offer the best and most useful modules PrestaShop and modifications for your online store.
9 * 
10 * @author    Carlos Ucha
11 * @copyright 2010-2100 Carlos Ucha
12 * @license   see file: LICENSE.txt
13 * This program is not free software and you can't resell and redistribute it
14 *
15 * CONTACT WITH DEVELOPER
16 * [email protected]
17 */
18 
19 $(document).ready(function() {
20  var $chatbox = $('#chatbox');
21  var $chatboxContent = $('#chatbox-content');
22  var $chatboxMessages = $('#chatbox-messages');
23  var $chatboxInput = $('#chatbox-input-text');
24  var $chatboxSendButton = $('#chatbox-send-button');
25  var $chatboxToggleButton = $('#chatbox-toggle');
26  var $chatbotName = $('#chatbotName').val();
27  var $customerName = $('#customerName').val();
28  var acceptButton = document.getElementById('accept-button');
29  var sendButton = document.getElementById('chatbox-send-button');
30  
31  var isAcceptButtonSelected = false;
32
33  $chatboxToggleButton.click(function() {
34    $chatbox.toggleClass('chatbox-closed');
35    $chatbox.toggleClass('open');
36  });
37
38  $chatboxSendButton.click(sendUserMessage);
39
40  function sendUserMessage() {
41        var userMessage = $chatboxInput.val().trim(); // Remove leading/trailing spaces
42        if (!userMessage) { // Check if message is not empty
43            return; // Don't proceed if the message is empty
44        }
45    
46        // Ensure userMessage is safe from XSS attacks
47        userMessage = $("<div>").text(userMessage).html();
48    
49        sendMessage(userMessage, $customerName);
50        $chatboxInput.val('');
51        
52        makeAjaxRequest(userMessage, $('#conversationHistory').val());
53    }
54
55  function makeAjaxRequest(userMessage, history) {
56    $('#waiting-dots').show();
57    $.ajax({
58      url: prestashop.urls.base_url + 'index.php?fc=module&module=gptchatbox&controller=ajax',
59      type: 'POST',
60      dataType: 'json',
61      data: { 
62        message: userMessage,
63        history: history,
64      },
65      success: function(response) {
66        var answer = response.answer;
67        var conversationHistoryJSON = response.conversationHistoryJSON;
68        
69        $('#conversationHistory').val(conversationHistoryJSON);
70        
71        sendMessage(answer, $chatbotName);
72        $('#waiting-dots').hide();
73        
74      },
75      error: function(xhr, status, error) {
76        if (xhr.status === 200) {
77          // Successful AJAX request, but server returned an error
78          var errorMessage = 'Server error occurred: ' + xhr.responseText;
79          // Handle the error message appropriately
80          // ...
81        } else {
82          // Other AJAX errors (e.g., network error, timeout)
83          var errorMessage = 'AJAX request error. Status: ' + xhr.status + ', Error: ' + error;
84          // Handle the error message appropriately
85          // ...
86        }
87        sendMessage(errorMessage, 'chatbot-error');
88        $('#waiting-dots').hide();
89      }
90    });
91  }
92
93  function sendMessage(message, sender) {
94      try {
95        var $messageElement = $('<div></div>').addClass('message');
96        message = message.replace(/\n/g, '<br />');
97        
98        if (sender === $customerName) {
99            //the message is from the user
100          $messageElement.html($customerName + ": " + message);
101          $messageElement.addClass("user");
102        } else {
103            //message from the chatbot
104          var messageWithLinks = linkify($chatbotName + ": " + message);
105          $messageElement.html(messageWithLinks);
106          $messageElement.addClass("response");
107          if(sender === 'chatbot-error'){
108              $messageElement.addClass("chatbot-error");
109          }
110        }
111    
112        $chatboxMessages.append($messageElement);
113        
114        scrollToLatestMessage();
115        
116      } catch (error) {
117        console.error('An error occurred while sending the message:', error);
118      }
119    }
120
121  $('#chatbox-input').keydown(function(event) {
122    if (event.keyCode === 13) { // Check if Enter key was pressed (key code 13)
123      event.preventDefault(); // Prevent the default Enter key behavior (e.g., form submission)
124
125      // Trigger the send button click event
126      if ($("#accept-button").length == 0 || $("#accept-button").hasClass('btn-selected')) {
127            $('#chatbox-send-button').click();
128        }
129    }
130  });
131  
132    $('#accept-button').click(function() {
133        $('#chatbox-send-button').removeAttr('disabled').removeClass('disabled');
134        $(this).addClass('btn-selected');
135        $('#decline-button').removeClass('btn-selected');
136        
137        isAcceptButtonSelected = true;
138    });
139
140    // Optional: When the decline button is clicked, add the 'disabled' attribute back to the send button.
141    $('#decline-button').click(function() {
142        $('#chatbox-send-button').attr('disabled', 'disabled').addClass('disabled');
143        $(this).addClass('btn-selected');
144        $('#accept-button').removeClass('btn-selected');
145        
146        isAcceptButtonSelected = false;
147    });
148    
149});
150
151function linkify(inputText) {
152    var replacedText, replacePattern1, replacePattern2, replacePattern3;
153
154    //URLs starting with http://, https://, or ftp://
155    replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
156    replacedText = inputText.replace(replacePattern1, '<a href="$1" target="_blank">$1</a>');
157
158    //URLs starting with "www." (without // before it, or it'd re-link the ones done above).
159    replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
160    replacedText = replacedText.replace(replacePattern2, '$1<a href="http://$2" target="_blank">$2</a>');
161
162    //Change email addresses to mailto:: links.
163    replacePattern3 = /(([a-zA-Z0-9\-\_\.])+@[a-zA-Z\_]+?(\.[a-zA-Z]{2,6})+)/gim;
164    replacedText = replacedText.replace(replacePattern3, '<a href="mailto:$1">$1</a>');
165
166    return replacedText;
167}
168
169function scrollToLatestMessage() {
170  const chatboxMessages = document.getElementById('chatbox-messages');
171  const lastMessage = chatboxMessages.querySelector('.message:last-of-type');
172  
173  // Scroll to the last message
174  lastMessage.scrollIntoView();
175}

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.