Untitled
unknown
javascript
2 years ago
3.9 kB
3
Indexable
// Constants for the time difference threshold (in milliseconds) and message limit const timeDifferenceThreshold = 1000; // Increased to 1 second const messageLimit = 1; // Set this value to 2 (more than 2 messages in 1 second) // Variables to track the latest message and state for the specific chat let chatData = { count: 0, time: 0 }; // Function to track and handle spam messages function banSpammer() { const currentTime = new Date().getTime(); // Select the last p element const latestMessage = document.querySelector('.messages-content p:last-child'); if (latestMessage) { // Select the span element with data-user attribute inside the latest p element const userIdElement = latestMessage.querySelector('span[data-user]'); const userId = userIdElement ? userIdElement.getAttribute('data-user') : null; // Increment the user's message count for the specific chat chatData.count++; // Collect user information const userName = userIdElement ? userIdElement.textContent : 'Unknown User'; if (userId && currentTime - chatData.time < timeDifferenceThreshold) { if (chatData.count > messageLimit) { // Remove all messages of the user if the limit is exceeded for the specific chat removeUserMessages(userId); // Calculate time difference const timeDifference = currentTime - chatData.time; // Log the spam information with id, name, time difference, and count in a single string console.log( `%cSpam information: User ID: ${userId}, User Name: ${userName}, Time Difference: ${timeDifference} ms, Message Count: ${chatData.count}`, 'color: red' ); // Reset user's message count for the specific chat chatData.count = 0; } } else { // Reset the user's message count if the time difference exceeds the threshold for the specific chat chatData.count = 1; // Calculate time difference const timeDifference = currentTime - chatData.time; // Log the spam information with id, name, time difference, and count in a single string console.log( `%cMessage information: User ID: ${userId}, User Name: ${userName}, Time Difference: ${timeDifference} ms, Message Count: ${chatData.count}`, 'color: green' ); } // Update the latest message information for the specific chat chatData.time = currentTime; } } // Function to remove all messages of a user in the specific chat function removeUserMessages(userId) { const userMessages = document.querySelectorAll(`.messages-content span[data-user="${userId}"]`); userMessages.forEach(message => { const pTag = message.closest('p'); if (pTag) { pTag.remove(); } }); } // Initialize Mutation Observer for the specific chat with class "messages-content" function observeChat() { const chatContainer = document.querySelector('.messages-content'); if (chatContainer) { const observer = new MutationObserver(mutations => { mutations.forEach(mutation => { if (mutation.addedNodes && mutation.addedNodes.length > 0) { // New node (message) added const newNode = mutation.addedNodes[0]; if (newNode.tagName === 'P') { // Call the banSpammer function for the new message in the specific chat banSpammer(); } } }); }); // Configure and start the observer for the specific chat const observerConfig = { childList: true, subtree: true }; observer.observe(chatContainer, observerConfig); // Log that Mutation Observer is now observing for the specific chat console.log('Mutation Observer is now observing for the chat.'); } else { console.error('Chat container not found.'); } } // Example: Start observing the chat with class "messages-content" observeChat();
Editor is loading...
Leave a Comment