Untitled
unknown
javascript
2 years ago
2.8 kB
3
Indexable
// Constants for the time difference threshold (in milliseconds) and message limit
const timeDifferenceThreshold = 2000;
const messageLimit = 1;
// Object to track user-specific data
let userChatData = {};
// Function to track and handle spam messages
function banSpammer() {
const currentTime = new Date().getTime();
// Select the last p element in the chat
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;
if (userId) {
// Initialize user-specific data if not already present
if (!userChatData[userId]) {
userChatData[userId] = { count: 0, time: currentTime }; // Initialize time with current time
}
// Collect user information
const userName = userIdElement ? userIdElement.textContent : 'Unknown User';
// Calculate time difference
const timeDifference = currentTime - userChatData[userId].time;
if (timeDifference < timeDifferenceThreshold) {
// Increment the user's message count for the chat
userChatData[userId].count++;
if (userChatData[userId].count > messageLimit) {
// Remove all messages of the user if the limit is exceeded
const userMessages = document.querySelectorAll(`.messages-content span[data-user="${userId}"]`);
userMessages.forEach(message => {
const pTag = message.closest('p');
if (pTag) {
pTag.remove();
}
});
// Log the spam information with id, name, time difference, and count in a single string
console.log(
`%cSpam info: User ID: ${userId}, Name: ${userName}, Time Diff: ${timeDifference} ms, Msg Count: ${userChatData[userId].count}`,
'color: red'
);
// Reset user's message count
userChatData[userId].count = 0;
}
} else {
// Update the user's time and reset the count if the time difference exceeds the threshold
userChatData[userId].time = currentTime;
userChatData[userId].count = 1;
// Log the message information with id, name, time difference, and count in a single string
console.log(
`%cMsg info: User ID: ${userId}, Name: ${userName}, Time Diff: ${timeDifference} ms, Msg Count: ${userChatData[userId].count}`,
'color: green'
);
}
}
}
}Editor is loading...
Leave a Comment