TheVera commited on
Commit
2b4fccf
·
verified ·
1 Parent(s): 4596bc9

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +29 -25
script.js CHANGED
@@ -316,29 +316,31 @@ function sendMessage() {
316
  const language = data.language || 'en';
317
 
318
  // Extract mantra if present in the response
319
- const mantraMatch = botResponse.match(/\[\[(.*?)\]\]/s); // Use [[ and ]] for mantra
320
  let formattedResponse = botResponse; // Initialize formatted response
321
  let category = ""; // Initialize category variable
 
322
 
323
- if (mantraMatch) {
324
- const mantraText = mantraMatch[1].trim();
325
-
326
- // Display mantra in the mantra block
327
- displayMantra(mantraText);
328
-
329
- // Remove mantra from the botResponse to avoid displaying it twice
330
- formattedResponse = botResponse.replace(/\[\[(.*?)\]\]/s, '').trim();
331
- }
332
 
333
- // Format the bot's response for display
334
  const responseDetails = formatBotResponse(formattedResponse);
335
  formattedResponse = responseDetails.formattedResponse;
336
  category = responseDetails.category;
337
 
338
- // Append the remaining bot response without the mantra
339
  appendMessage(category, 'bot-message');
340
  appendMessage(formattedResponse, 'bot-message');
341
 
 
 
 
 
 
342
  // Speak the bot's response (Text-to-Speech)
343
  speakText(formattedResponse, language);
344
 
@@ -380,19 +382,21 @@ function sendMessage() {
380
 
381
  // Manta Block Function
382
 
383
- // Function to display the mantra block with the mantra text
384
- function displayMantra(mantraText) {
385
- let mantraBlock = document.getElementById('mantra-block');
386
- let mantraTextElem = document.getElementById('mantraText');
387
-
388
- // Set the mantra text in the block
389
- mantraTextElem.textContent = mantraText;
390
-
391
- // Show the mantra block
392
- mantraBlock.style.display = 'block';
393
 
394
- // By default, play the mantra once when shown
395
- speakMantra(mantraText, 1);
 
 
396
  }
397
 
398
  // Function to decrease the repeat count
@@ -419,7 +423,7 @@ function speakMantra(mantraText, repeatCount) {
419
  utterance.lang = 'sa-IN'; // Sanskrit
420
  utterance.rate = 0.8; // Slow speech for mantras
421
  utterance.pitch = 1.0; // Natural pitch
422
-
423
  utterance.onend = function() {
424
  if (repeatCount > 1) {
425
  repeatCount--;
 
316
  const language = data.language || 'en';
317
 
318
  // Extract mantra if present in the response
319
+ const mantraMatches = [...botResponse.matchAll(/\[\[(.*?)\]\]/g)]; // Find all occurrences of [[mantra]]
320
  let formattedResponse = botResponse; // Initialize formatted response
321
  let category = ""; // Initialize category variable
322
+ let allMantras = []; // Array to hold all mantra texts
323
 
324
+ mantraMatches.forEach(match => {
325
+ allMantras.push(match[1].trim()); // Collect all mantra texts
326
+ // Remove each mantra from the botResponse to avoid displaying it twice
327
+ formattedResponse = formattedResponse.replace(match[0], '').trim();
328
+ });
 
 
 
 
329
 
330
+ // Append the remaining bot response without the mantras
331
  const responseDetails = formatBotResponse(formattedResponse);
332
  formattedResponse = responseDetails.formattedResponse;
333
  category = responseDetails.category;
334
 
335
+ // Append the bot response
336
  appendMessage(category, 'bot-message');
337
  appendMessage(formattedResponse, 'bot-message');
338
 
339
+ // Display all mantras in the mantra block
340
+ if (allMantras.length > 0) {
341
+ displayMantra(allMantras); // Pass the array of mantras
342
+ }
343
+
344
  // Speak the bot's response (Text-to-Speech)
345
  speakText(formattedResponse, language);
346
 
 
382
 
383
  // Manta Block Function
384
 
385
+ // Function to display the mantra block with the mantra texts
386
+ function displayMantra(mantraTexts) {
387
+ let mantraBlock = document.getElementById('mantra-block');
388
+ let mantraTextElem = document.getElementById('mantraText');
389
+
390
+ // Join all mantras into a single string separated by line breaks or any preferred format
391
+ mantraTextElem.textContent = mantraTexts.join('\n');
392
+
393
+ // Show the mantra block
394
+ mantraBlock.style.display = 'block';
395
 
396
+ // By default, play all mantras when shown
397
+ mantraTexts.forEach(mantra => {
398
+ speakMantra(mantra, 1); // Speak each mantra once
399
+ });
400
  }
401
 
402
  // Function to decrease the repeat count
 
423
  utterance.lang = 'sa-IN'; // Sanskrit
424
  utterance.rate = 0.8; // Slow speech for mantras
425
  utterance.pitch = 1.0; // Natural pitch
426
+
427
  utterance.onend = function() {
428
  if (repeatCount > 1) {
429
  repeatCount--;