Untitled

 avatar
unknown
javascript
2 years ago
3.2 kB
5
Indexable
// Constants for the time difference threshold (in milliseconds) and message limit
const timeDifferenceThreshold = 700; // 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(pTag) {
  const currentTime = new Date().getTime();
  const userIdElement = pTag.querySelector("span[data-user]");
  const userId = userIdElement ? userIdElement.getAttribute("data-user") : null;

  if (userId && currentTime - chatData.time < timeDifferenceThreshold) {
    // Increment the user's message count for the specific chat
    chatData.count++;

    console.log(`User ${userId} message count: ${chatData.count}`);

    if (chatData.count > messageLimit) {
      console.log("Spam detected. Initiating action...");

      // Remove all messages of the user if limit is exceeded for the specific chat
      removeUserMessages(userId);

      // Collect user information in an object
      const userName = userIdElement.textContent;

      // Log the spam information
      console.log("Spam information:", {
        user: {
          id: userId,
          name: userName,
        },
        exceededLimit: true,
      });

      // Reset exceededLimit and 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;
  }

  // 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(newNode);
          }
        }
      });
    });

    // 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