Untitled

 avatar
unknown
javascript
2 years ago
3.2 kB
6
Indexable
// Constants for the time difference threshold (in milliseconds) and message limit
const timeDifferenceThreshold = 1000; // Increased to 1 second
const messageLimit = 2; // Set this value to 2 (more than 2 messages in 1 second)

// Variables to track the latest message and state
let latestMessages = {}; // Object to store the latest message time for each user
let exceededLimit = false;

// Function to track and handle spam messages
function banSpammer(pTag) {
  const currentTime = new Date().getTime();
  const userIdElement = pTag.querySelector("span.username [data-user]");
  const userId = userIdElement ? userIdElement.getAttribute("data-user") : null;

  // Initialize the user's message count
  latestMessages[userId] = latestMessages[userId] || { count: 0, time: 0 };

  if (userId && currentTime - latestMessages[userId].time < timeDifferenceThreshold) {
    // Increment the user's message count
    latestMessages[userId].count++;

    console.log(`User ${userId} message count: ${latestMessages[userId].count}`);

    if (latestMessages[userId].count > messageLimit) {
      console.log("Spam detected. Initiating action...");

      // Remove all messages of the user if limit is exceeded
      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,
      });

      // Set exceededLimit to true
      exceededLimit = true;

      // Reset exceededLimit and user's message count after 3 seconds
      setTimeout(() => {
        exceededLimit = false;
        latestMessages[userId].count = 0;
        console.log("Exceeded limit reset to false.");
      }, 3000); // 3000 milliseconds (3 seconds)
    }
  } else {
    // Reset the user's message count if the time difference exceeds the threshold
    latestMessages[userId].count = 1;
  }

  // Update the latest message information
  latestMessages[userId].time = currentTime;
}

// Function to remove all messages of a user
function removeUserMessages(userId) {
  const userMessages = document.querySelectorAll(`span[data-user="${userId}"]`);
  userMessages.forEach(message => {
    const pTag = message.closest("p");
    if (pTag) {
      pTag.remove();
    }
  });
}

// Initialize Mutation Observer
const chatContainer = document.querySelector(".messages-content div");
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
        banSpammer(newNode);
      }
    }
  });
});

// Configure and start the observer
const observerConfig = { childList: true, subtree: true };
observer.observe(chatContainer, observerConfig);

// Log that Mutation Observer is now observing
console.log("Mutation Observer is now observing.");
Editor is loading...
Leave a Comment