// Get all message elements with class 'message'
const messages = document.querySelectorAll('.message');
// Get the href content of the first occurrence of the class 'active'
const hrefContent = document.querySelector('.active').href;
// Loop through each message
messages.forEach(message => {
// Find the element with class 'message-date' within this message
const messageDateElement = message.querySelector('.message-date');
// If such an element was found, extract the date and time from its title attribute using a regular expression
if (messageDateElement) {
const originalString = messageDateElement.getAttribute('title');
const regex = /(\d{2}\/\d{2}\/\d{4} à \d{2}:\d{2}:\d{2})/;
const match = regex.exec(originalString);
// If a match was found, create a new div element and set its inner HTML to the extracted date and time
if (match) {
const newLink = document.createElement('a');
const messageID = message.getAttribute('data-id');
newLink.href = hrefContent + '#message_' + messageID;
// Add CSS styles to the new link element
newlink.style.color = '#79bcee';
newlink.style.fontSize = '0.8125rem';
// Set the inner HTML of the link to the extracted date and time
newLink.innerHTML = match[0];
// Find the element with class 'message-infos' within this message and append the new link element to it
const messageInfosElement = message.querySelector('.message-infos');
if (messageInfosElement) {
messageInfosElement.appendChild(newLink);
}
messageDateElement.remove();
// Add a click event listener to the new link that scrolls to the target message and copies its href attribute to the clipboard
newLink.addEventListener('click', event => {
event.preventDefault();
const href = event.target.getAttribute('href');
const targetElement = document.querySelector(href);
if (targetElement) {
targetElement.scrollIntoView({ behavior: 'smooth' });
navigator.clipboard.writeText(href);
console.log('Link copied to clipboard:', href);
}
});
}
}
});
// Variables for the keyboard event
const textarea = document.querySelector(".textarea");
const button = document.querySelector("input.button");
textarea.addEventListener("keydown", (event) => {
// Check if the pressed key is Enter and the Shift key isn't pressed
if (event.key === "Enter" && !event.shiftKey ) {
// Prevent the default behavior of the Enter key (i.e., inserting a newline)
event.preventDefault();
function makeRequest(url) {
// Create a new XMLHttpRequest object
const xhr = new XMLHttpRequest();
// Define the URL and method of the request
const method = "GET";
// Open the request with the URL and method
xhr.open(method, url);
// Define the callback function to read the response
xhr.onload = function() {
if (xhr.status === 200) {
const response = xhr.responseText;
// console.log(response)
const parser = new DOMParser();
const htmlDocument = parser.parseFromString(response, "text/html"); // Parse the HTML response
let messageCount = htmlDocument.querySelectorAll("div.messages div.message:not(.answer)").length
console.log(htmlDocument.querySelectorAll("div.messages div.message:not(.answer)"))
console.log("Number of text messages:", messageCount);
if (messageCount > currentMessageCount) {
console.log("New message")
let numberOfMessagesToAdd = messageCount - currentMessageCount;
let nodeArray = htmlDocument.querySelectorAll("div.messages div.message:not(.answer)")
let substring = Array.from(nodeArray).slice(messageCount - numberOfMessagesToAdd, messageCount);
// For each elements that we received. We apppend it to the messages
substring.forEach(element => {
document.querySelector('.messages').appendChild(element);
});
currentMessageCount = messageCount;
// If the textarea is visible we scroll
if (isElementVisible(textarea)) {
// execute the action if the element is visible
document.querySelector(".textarea").scrollIntoView({
block: "center",
inline: "nearest",
behavior: "smooth"
});
}
if (currentMessageCount === MESSAGES_PER_PAGES) {
activePage += 1
currentMessageCount = 0
}
}
//console.log(response);
// Replace this with your actual response handling code
}
};